how to fit a function on my dara

2 views (last 30 days)
tasneem haikal
tasneem haikal on 3 Jun 2020
Commented: Rena Berman on 12 Oct 2020
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 Comments
Rik
Rik on 4 Jun 2020
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
Rena Berman
Rena Berman on 12 Oct 2020
(Answers Dev) Restored edit

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 3 Jun 2020
Edited: John D'Errico on 3 Jun 2020
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.

Community Treasure Hunt

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

Start Hunting!