I have a program which make a linear fit on a data. I want to use the slope of this linear fit on another data where I fit exponential (a*exp(b*x)) as b. Part of my program which do this is as below:
%%SECTION 1:THIS PART FIT LINEAR FIT ON THE PLOT.
[FTLx,FTLy]=ginput(2); indexL=find(B(:,1)>=FTLx(1) & B(:,1)<=FTLx(2)); NewXL=B(B(:,1)>=FTLx(1) & B(:,1)<=FTLx(2)); NewYL=B(indexL,2); P=polyfit(NewXL,log(NewYL),1); LFit=P(1)*NewXL+P(2); slope=P(1) %THIS slope I USE IN THE NEXT SECTION
hold on figure(3) subplot(1,2,1),plot(NewXL,LFit,'-b',NewX,log(NewY),':or') %%SECTION 2: THIS SECTION FITS EXPONENTIAL... THIS SECTION USE 'slope' FROM THE PREVIOUS SECTION
options = fitoptions; options.Normal = 'on'; ft=fittype('a*exp(slope*x)','independent','x','dependent','y');%THIS 'slope' SEEMS NOT TAKING THE VALUE FROM THE PREVIOUS SECTION!! :( :( :(
[fitresult, gof]=fit(NewX,NewY,ft,options) figure(3) subplot(1,2,2), plot(fitresult,'-b',NewX,NewY,':or') title('Exponential Fit in the selected range')
Here in the second section, I am using 'slope' from the first section. But it seems to taking the value of 'slope' from the first section! Is there anything wrong? How can I correct it?
Best Answer