Selasa, 17 April 2012

regresi polinom

%regresi polinom
clear; clc;

%input data
fin=fopen('data.dat');
data=textscan(fin,'%f %f','r');
fclose(fin);

x=data{1};
Y=data{2};

G1=[ones(length(x),1) x];
G2=[ones(length(x),1) x x.^2];
G3=[ones(length(x),1) x x.^2 x.^3];
G4=[ones(length(x),1) x x.^2 x.^3 x.^4];


m1=inv(G1'*G1)*G1'*Y
m2=inv(G2'*G2)*G2'*Y
m3=inv(G3'*G3)*G3'*Y
m4=inv(G4'*G4)*G4'*Y

X=(2:0.1:11);

Y1=G1*m1;
Y2=G2*m2;
Y3=G3*m3;
Y4=G4*m4;

Y1_2=m1(1,1)+m1(2,1)*x;
Y2_2=m2(1,1)+m2(2,1)*x+m2(3,1)*(x).^2;
Y3_2=m3(1,1)+m3(2,1)*x+m3(3,1)*(x).^2+m3(4,1)*(x).^4;
Y4_2=m4(1,1)+m4(2,1)*x+m4(3,1)*(x).^2+m4(4,1)*(x).^4+m4(5,1)*(x).^5;

Erms1=sqrt((sum((Y1-Y).^2))/length(x))
Erms2=sqrt((sum((Y2-Y).^2))/length(x))
Erms3=sqrt((sum((Y3-Y).^2))/length(x))
Erms4=sqrt((sum((Y4-Y).^2))/length(x))

figure(1)
plot(x,Y,'ok','markerfacecolor','k')
hold on
xlabel('x');Ylabel('Y');
Ylim([0 8])

figure(2)
plot(x,Y,'ok','markerfacecolor','k')
hold on
xlabel('x');Ylabel('Y');
Ylim([0 8])

figure(3)
plot(x,Y,'ok','markerfacecolor','k')
hold on
xlabel('x');Ylabel('Y');
Ylim([0 8])

figure(4)
plot(x,Y,'ok','markerfacecolor','k')
hold on
xlabel('x');Ylabel('Y');
Ylim([0 8])







2 komentar:

alexander felix taufan parera mengatakan...

keren banget, angga... yah, sangat membantulah buat tugas komputasi geophysics nih.. ^.^

Angga Vertika Diansari mengatakan...

iya donks... :D sebenernya ini juga bwt q sendiri jg kalo sewaktu2 butuh tinggal copy aja di blogs, lex.......... :D

Posting Komentar

next page