Why it is not fitted? How i can plot exponential fit?

19 Ansichten (letzte 30 Tage)
Seda Sen
Seda Sen am 10 Mär. 2018
Beantwortet: John D'Errico am 10 Mär. 2018
%y=C*e^(Ax) of linear form Y=MX+N
clear all
close all
clc
%Data for S function
x=[3.247973266 3.155032229 3.226084116 4.075948225 3.954965731 4.143202225 5.410125725 5.457264063 5.386434753 6.093085327 6.109878771];
y=[42.625 42.625 42.625 37.29705 37.29705 37.29705 31.9689 31.9689 31.9689 29.304825 26.64075];
K=length(x);
L=length(y);
for i=1:K
Y(1,i)=log(y(1,i));
X(1,i)=x(1,i);
end
sumx2=sum(X.^2);
sumx=sum(X);
sumxy=sum(X.*Y);
sumy=sum(Y);
O=[sumx2 sumx;sumx K];
P=[sumxy;sumy];
m=inv(O).*P;
M=m(1,1);
N=m(2,1);
C=exp(N);
A=M;
%N=((sumy.*sumx2)-(sumx.*sumxy))./((sumx2.*K)+(sumx.*sumx));
%M=(sumxy-N.*sumx)./(sumx2);
plot(x,y,'o')
hold on
%f=C.*exp(A.*x);
%plot(f)
I wrote a matlab code for curve fitting. But it is not work. How i develop the code? Could you help me?

Antworten (2)

John D'Errico
John D'Errico am 10 Mär. 2018
Never write basic code to solve a problem that has already been solved and written by people who are experts at it. In the case of curve fitting, there are MANY ways to solve this problem in virtually one line, and that do so more efficiently and more accurately that you did.
So if you have the curve fitting toolbox, then it is as simple as:
mdl = fittype('exp1')
mdl =
General model Exp1:
mdl(a,b,x) = a*exp(b*x)
fittedmdl = fit(x',y',mdl)
fittedmdl =
General model Exp1:
fittedmdl(x) = a*exp(b*x)
Coefficients (with 95% confidence bounds):
a = 65.91 (61.41, 70.4)
b = -0.1373 (-0.1528, -0.1217)
plot(x,y,'ro')
hold on
plot(fittedmdl)
If you are doing any modeling at all, the CFT is well worth the investment.
If you lack the CFT, then the stats toolbox has regress, or nlinfit. Regress can solve the linearized form that you used.
If you lack that TB, then the optimization Toolbox will do it, using lsqnonlin, or lsqcurvefit.
Finally, if you lack any toolbox at all, then STILL don't write it yourself!
close
P1 = polyfit(x,log(y),1);
C = exp(P1(2));
A = P1(1);
plot(x,y,'ro',x,C*exp(A*x),'b-')
And if you refuse to use something as simple as polyfit, then STILL DON'T write the code the way you did.
P1 = [x(:),ones(numel(x),1)]\log(y(:))
P1 =
-0.137994352412978
4.191285976189
C = exp(P1(2));
A = P1(1);
That your data barely supports a negative exponential model is something I would add. As well, you appear to have a data problem at that last point. That likely causes you to mis-estimate your parameters.
So, if you exclude that possibly spurious point, we see:
fittedmdl = fit(x(1:end-1)',y(1:end-1)',mdl)
fittedmdl =
General model Exp1:
fittedmdl(x) = a*exp(b*x)
Coefficients (with 95% confidence bounds):
a = 63.95 (61.06, 66.84)
b = -0.129 (-0.1396, -0.1184)
plot(x,y,'ro')
hold on
plot(fittedmdl)

Image Analyst
Image Analyst am 10 Mär. 2018
See attached demo. Adapt as needed.

Community Treasure Hunt

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

Start Hunting!

Translated by