how to fit a function on my dara

2 次查看(过去 30 天)
x=[0.001; 0.05; 0.1; 0.25; 0.5 ;0.8; 1; 4.5; 7; 10]
y= [-3.92E+08; -3.75E+08; -3.72E+08; -3.62E+08; -3.47E+08; -3.28E+08; -3.16E+08; -1.36E+08; -2.93E+07; 7.19E+07]
fo = fitoptions('Method','NonlinearLeastSquares',...
'Lower',[0,0],...
'Upper',[Inf,Inf],...
'StartPoint',[0.001 -3.92E+08]);
ft = ft = fittype('a*(1-2*exp(-x/(b)))','coefficient',{'a','b'},'options',fo);
[fitobject,gof] = fit(x,y,ft);
a=fitobject.a;
b=fitobject.b;
r2=gof.rsquare;
x2=linspace(0,10,10);
y2=((fitobject.a)*(1 - (2*exp(-x2/(fitobject.b)))));
plot(x,y,'*');
hold on
plot(x2,y2,'-')
ax=gca;
ax.FontSize=14;
ax.FontWeight='bold';
i write this code i want to that the red line to be up my points but i get a straight line !
i write a function and i want to plot this function on my data , i think that the problem on y2,but i dont how to write it in another way
  3 个评论
Rik
Rik 2020-6-4
Question body from Google Cache (the attached image was not stored in the cache):
x=[0.001; 0.05; 0.1; 0.25; 0.5 ;0.8; 1; 4.5; 7; 10]
y= [-3.92E+08; -3.75E+08; -3.72E+08; -3.62E+08; -3.47E+08; -3.28E+08; -3.16E+08; -1.36E+08; -2.93E+07; 7.19E+07]
fo = fitoptions('Method','NonlinearLeastSquares',...
'Lower',[0,0],...
'Upper',[Inf,Inf],...
'StartPoint',[0.001 -3.92E+08]);
ft = fittype('a*exp(b*x)','coefficient',{'a','b'},'options',fo);
[fitobject,gof] = fit(x,y,ft);
a=fitobject.a;
b=fitobject.b;
r2=gof.rsquare;
x2=linspace(0,10,10);
y2=(fitobject.a * exp(fitobject.b*x2));
plot(x,y,'*');
hold on
plot(x2,y2,'-')
ax=gca;
ax.FontSize=14;
ax.FontWeight='bold';
i write this code i want to that the red line to be up my points but i get a straight line !
i write a function and i want to plot this function on my data , i think that the problem on y2,but i dont how to write it in another way

请先登录,再进行评论。

回答(1 个)

John D'Errico
John D'Errico 2020-6-3
编辑:John D'Errico 2020-6-3
When you get an error, don't just tell us you got an error. Paste in the complete error text. At least, you showed a picture of the error. A picture may be worth a thouand words, but real numbers? They are worth far more.
When you give us text, we can copy text, and then paste it into MATLAB, instead, in order to help you, we would need to type in your data ourselves. Luckily, you did at least show a picture of the error, which in this case, is sufficient to know that you needed to TRANSPOSE those vectors.
x and y need to be COLUMN vectors for fit to work properly. You created them as ROW vectors. What was the error message? "X must be a matrix with one or two columns." In your case, x must be a vector, so a matrix with ONE column. x as you created it was a vector with one row, and 10 columns.
Transpose both X and Y so they are column vectors in order to make fit work properly.

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by