Prev: mat into excel
Next: Creating new variables in a loop
From: SM on 15 May 2010 16:29 Hi everybody, I have created an algorithm to which fits a model to the set of data in a following way. My data is stored in a mXn matrix for instance Matrix A: A=[0,0.33,2.63,3.92,5.53,30.93,24.45,59.39,76.32,112.82,105.47,192.93,171.14,208.58,156.63,174.04,198.59,167.08,106.71,105.96,84.47,64.81,53.97,24.14,24.73,16.56,14.03,5.85,6.24,4.19,3.905;... 0.48,0.081,0.35,1.21,2.38,2.75,3.75,3.68,5.875,10.25,9.39,12.11,14.659,15.71,20.86,16.32,16.399,14.992,13.74,14.714,5.89,4.24,4.10,4.20,0.99,1.20,2.11,1.15,0,0.82,0.25] ; I want to fit the model which is defined as myfun: function F = myfun (xx,xdata) F = x(1).*exp(-((xdata-x(2)).^2./(2*x(3).^2))); to the data in following way. 1- Take data of row j (here j=1) (A(1,:)); 2- Take data of certain column (assigned by block0) 3- fit the model 4- plot the data and the fitted model 5- shift the block according to the shift step value 6- repeat step 3 and 4 7- last block in row 1 8- Iterate to the next row. 9- Now, combine the data from row 1 and 2 (I used reshape to do this) 10- repeat steps 2 to 8. To combine the data I used reshape, and since I wanna plot the data in a single plot, I also calculate a new x each time which depends on j. The problem is when the algorithm iterates through j, it fails to fit the model. Actually, I guess the problem is due to the combining the data. but I can not fix it. I attach my code here. Any help is appreciated. %%%% The code is: A=[0,0.33,2.63,3.92,5.53,30.93,24.45,59.39,76.32,112.82,105.47,192.93,171.14,208.58,156.63,174.04,198.59,167.08,106.71,105.96,84.47,64.81,53.97,24.14,24.73,16.56,14.03,5.85,6.24,4.19,3.905;... 0.48,0.081,0.35,1.21,2.38,2.75,3.75,3.68,5.875,10.25,9.39,12.11,14.659,15.71,20.86,16.32,16.399,14.992,13.74,14.714,5.89,4.24,4.10,4.20,0.99,1.20,2.11,1.15,0,0.82,0.25] ; t=size(A,1); l=1:1:size(A,2); lold=l; block_0=5; for j=1:t if j==1 l0=sort(unique(lold)); lold=l0; else B=l+(j-1)/j; ft0=sort(([lold B])) lold=l0 end xdata=lold; Anew=reshape(A(1:j,:),1,[]); v=ones(size(Anew,1),size(Anew,2)); v(1:j,:)=j; block=j*block_0; sm= size(Anew,1); sn=size(Anew,2); inloc = 1; %1 sigma=block/2; WidthX =2*block; shiftstep=j; % j timestep=1; intime=1; WidthY=1; figure (1) plot3(xdata,v,Anew,'*') hold for i=inloc:shiftstep:sn-WidthX x0 = [5, (i+10), 2]; xx=lold(1,i:i+WidthX-1); yy=Anew(1,i:i+WidthX-1); [x,resnorm] = lsqcurvefit(@myfun,x0,xx,yy); cmap = colormap; u=j.*ones(1,length(xx)); if -WidthX <=x(3)&& x(3)<=WidthX ymodel=x(1)*exp((-(xx-x(2)).^2)/(2.*x(3).^2)); plot3(xx,u,x(1).*exp(-((xx-x(2)).^2./(2*x(3).^2))),'Color',[rand(1),rand(1),rand(1)]); hold on l_block=x(2) time_block=j model_param=[x(1) x(2) x(3)] end end end
From: SM on 16 May 2010 11:11 Actually, I tried to figure out the problem, but apparently the problem is due to the value of 'block'!!! I update the block by multiplying the value into the j, while itterating. but fitting the model is not working! Any idea, how I could assign this value?
|
Pages: 1 Prev: mat into excel Next: Creating new variables in a loop |