how to fit a function on my dara

조회 수: 3 (최근 30일)
tasneem haikal
tasneem haikal 2020년 6월 3일
댓글: Rena Berman 2020년 10월 12일
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
Rena Berman
Rena Berman 2020년 10월 12일
(Answers Dev) Restored edit

댓글을 달려면 로그인하십시오.

답변 (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