To take parameter of a fit from one section to use in another fit in the next section.

1 次查看(过去 30 天)
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?
  1 个评论
dpb
dpb 2014-5-21
I've never actually used the wrapper stuff but looks like perhaps if you were to wrap in an anonymous function --
ft = fittype( @(a,x) a*exp(slope*x), 'problem', 'slope' );
f1 = fit(NewX, NewY, ft, 'problem', slope);
might do the trick...

请先登录,再进行评论。

采纳的回答

Sean de Wolski
Sean de Wolski 2014-5-21
% Sample data
x = (1:10).';
y = cumsum(rand(10,1));
% First fit
ft = fittype(@(m,b,x)m.*x+b);
fr = fit(x,y,ft)
% Second fit
ft2 = fittype(@(a,x)a*exp(fr.m*x));
fr2 = fit(x,y,ft2)
Using anonymous functions. You could do this with strings too but it would involve some kludge with sprintf / num2str etc.
  2 个评论
aneps
aneps 2014-5-22
Thanks... but i cannot plot the fit.. following is the code I used. I am not sure where is the mistake to plot
ft1=fittype(@(m,b,x)m.*x+b);
fr1=fit(NewXL,NewYL,ft1);
%slope=fr1.m
hold on
figure(3)
subplot(1,2,1),plot(NewXL,fr1,'-b',NewX,NewY,':or')
Could you pls check the last line where I try to plot the fit. It is showing error while I run the code.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by