Contoh Interpolasi Regresi
y=a0+a1(x)+a2(x^2)
Data:
x y
-------
3 1.6
4 3.6
5 4.4
7 3.4
Aloritma pada MATLAP
x=[3;4;5;7];
y=[1.6;3.6;4.4;3.4];
m=4;
sumx=0;sumx2=0;sumx3=0;sumx4=0;sumx5=0;sumx6=0;
sumy=0;sumxy=0;sumx2y=0;sumx3y=0;
for i=1:m
sumx=sumx+x(i);
sumy=sumy+y(i);
sumxy=sumxy+x(i)*y(i);
sumx2=sumx2+x(i).^2;
sumx3=sumx3+x(i).^3;
sumx4=sumx4+x(i).^4;
sumx2y=sumx2y+x(i).^2*y(i);
sumx5=sumx5+x(i).^5;
sumx6=sumx6+x(i).^6;
sumx3y=sumx3y+x(i).^3*y(i)
end
A=[m sumx sumx2 sumx3; sumx sumx2 sumx3 sumx4; sumx2 sumx3 sumx4 sumx5;sumx3 sumx4 sumx5 sumx6 ]
b=[sumy sumxy sumx2y sumx3y]
dim=size(A);
n=dim(1);
for i=1:n;
A(i,n+1)=b(i);
end
%Proses OBE dan Subtitusi Mundur
for i=1:n-1
for j=i+1:n
m=A(j,i)/A(i,i);
for k=1:n+1
A(j,k)=A(j,k)-m*A(i,k)
A(j,i)=0;
end
end
end
xx(n)=A(n,n+1)/A(n,n);
for i= n-1:-1:1
S=0;
for k= i+1:n;
S = S + A(i,k)*xx(k);
end
xx(i)=(A(i,n+1)-S)/A(i,i);
end
nilai_a3=xx(4)
nilai_a2=xx(3)
nilai_a1=xx(2)
nilai_a0=xx(1)
%Proses pembuatan hasil plot
x=[3;4;5;7];
y=[1.6;3.6;4.4;3.4];
m=4;
fx=xx(4)*(x(1:m)).^3+xx(3)*(x(1:m)).^2+xx(2)*x(1:m)+xx(1);
E=((sum(abs(fx(m)-y(m))^2)/n)^2);
nilai_fx=fx
Root_Mean_Square_Error=E
plot(x,y,'r-*')
hold on
plot(x(1:m),fx,'g-*')
grid on
Tidak ada komentar:
Posting Komentar