linear regression - MATLAB Fitting Function -


i trying fit line data without using polyfit , polyval. got on how implement , have gotten work simple sin function. however, when applied function trying fit, not work. here code:

clear  clc  lb=0.001; %lowerbound of data  ub=10; %upperbound of data  step=.1; %step-size through data  a=.03;  la=1482/120000; %1482 speed of sound in water , 120khz ep1=.02; ep2=.1; x=lb:step:ub; r_sq_des=0.90; %desired value of r^2 fit of data without noise present    i=1;   x=lb:step:ub       g(i,1)= abs(sin((a/la)*pi*x*(sqrt(1+(1/x)^2)-1)));      n(i,1)=2*rand()-1;       ghat(i,1)=(1+ep1*n(i,1))*g(i,1)+ep2*n(i,1);       r(i,1)=x;       i=i+1;  end   x=r; y=g; v=[x.^0]; vfit=[x.^0];   i=1:1:1000     v = [x.^i v];     c = v \ y;      vfit = [x.^i vfit];     yfit=vfit*c;      plot(x,y,'o',x,yfit,'--')      drawnow     pause end 

the first 2 sections defining variables , function. second loop making fit. can see, have pause after every nth order in order see fit.

as dennis mentioned, different set of basis functions might better. can improve polynomial fit qr factorisation, rather \ solve matrix equation. badly conditioned problem no matter however, , using smooth basis functions wont allow accurately reproduce sharp corners in actual function.

clear close clc  lb=0.001; %lowerbound of data  ub=10; %upperbound of data  step=.1; %step-size through data  a=.03;  la=1482/120000; %1482 speed of sound in water , 120khz ep1=.02; ep2=.1; x=logspace(log10(lb),log10(ub),100)'; r_sq_des=0.90; %desired value of r^2 fit of data without noise present   y=abs(sin(a/la*pi*x.*(sqrt(1+(1./x).^2)-1))); n=2*rand(size(x))-1; ghat=(1+ep1*n).*y+ep2*n;  v=[x.^0];  xs=(lb:.01:ub)'; vfit=[xs.^0];  i=1:1:20%length(x)-1     v = [x.^i v];     vfit = [xs.^i vfit];     [q,r]=qr(v,0);     c = r\(q'*y);     yfit=vfit*c;      plot(x,y,'o',xs,yfit)     axis([0 10 0 1])     drawnow     pause end 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -