Exponential/Power curve fitting from data

17 views (last 30 days)
Mamed
Mamed on 24 Oct 2011
Hey
I would like some help on finding some unknown constants in an equation, from some data i have.
The function I have is the following
y = k1 * d1^(k2/2) * d2^(k3/2) * d3^k4
from measurements i have [y d1 d2 d3] and i need to know [k1 k2 k3 k4].
I tried to solve this function in excel using the LOGEST function and it solves for
y = b * m1^x1 * m2^x2...etc
where mn is the unknowns.
What i did there was to set exp(ln) the function to get it the way i needed to be and it solved it just fine.
What i want to do is the same thing except in matlab. I'm kinda lost on what function to use and so on.
I was looking HERE but i don't know if its the best way to go about solving it.
Because it requires start values and only one in-value (x) while i have 3 different values.
This is as far as i came before i got stuck
% k = [k1 k2 k3 k4]
% d = [data1 data2 data3]
% y
% y and d known and in a list
modelfun = @(k,d)
k(1)*exp(p(1)*k(2))*exp(p(2)*k(3))*exp(p(3)*k(4))
start = [2 0.2 1.5 -.5]
coef = nlinfit(???,y,modelfun,start)
the thing is i wanna be able to plot y against any of those three.
Thanks
Sorry for the bad english

Answers (4)

Mamed
Mamed on 25 Oct 2011
Hello
I solved it by myself. Here is the answer if anyone is interested later on.
clc,clear
% y d1 d2 d3
A = [
55 535 1269 1498
103 1268 2387 1500
101 1055 3468 2999
106 983 3844 3000
67 541 2352 3000
100 983 3942 3750
107 955 4663 4499
84 561 3497 4500
];
y = A(:,1);
d1 = A(:,2);
d2 = A(:,3);
d3 = A(:,4);
Starting=[2 0.2 1.5 -0.5]';
options=optimset('Display','iter');
Estimates=fminsearch(@myfit,Starting,options,[d1 d2 d3],y);
% To check the fit
plot(d3,y,'*')
hold on
plot(n,Estimates(1)*d1.^(0.5*Estimates(2)).*d2.^(0.5*Estimates(3)).*d3.^Estimates(4),'r *')
and the function myfit
function sse=myfit(params,A,Actual_Output)
d1 = A(:,1);
d2 = A(:,2);
d3 = A(:,3);
k1 = params(1);
k2 = params(2);
k3 = params(3);
k4 = params(4);
Fitted_Curve=k1.*d1.^(0.5*k2).*d2.^(0.5*k3).*d3.^k4
Error_Vector=Fitted_Curve - Actual_Output;
sse=sum(Error_Vector.^2);
this works fine, is there any other better way to solve this if i have alot of data?

Daniel Shub
Daniel Shub on 25 Oct 2011
I think if you log transform everything you can eliminate the fminsearch and treat the problem as optimizing a linear equation. Basically rewrite
y = k1 * d1^(k2/2) * d2^(k3/2) * d3^k4
as
log(y) = log(k1)+(k2/2)*d1+(k3/2)d2+(k4)*d3;
and solve for log(k1), k2, k3, k4 with techniques designed for linear equations.

Mamed
Mamed on 25 Oct 2011
Hi thanks for answering. I did as you suggested. I created another function and tried to solve it in a linear fashion.
This is the added code ( inserted before "%To check fitting" )
options = optimset('Largescale','off');
xlin=lsqnonlin(@fit_simp,Starting,[],[],options,[d1 d2 d3],y);
est = Estimates(1)*d1.^(0.5*Estimates(2)).*d2.^(0.5*Estimates(3)).*d3.^Estimates(4);
lin = exp(xlin(1))*d1.^(0.5*xlin(2)).*d2.^(0.5*xlin(3)).*d3.^xlin(4);
% To check the fit
plot(n,y,'k *')
hold on
plot(n,est,'r *')
hold on
plot(n,lin,'g o')
here i check the error differences
error = [abs(y-est) abs(y-lin)]
sumerr = sum(error)
sumerr =
12.2281 12.2896
And the error is BIGGER with the linear model than it is with the nonlinear model, am i doing something wrong in the error correction?
And the new function is
function diff = fit_simp(para,A,Y)
k1 = para(1);
k2 = para(2);
k3 = para(3);
k4 = para(4);
d1 = A(:,1);
d2 = A(:,2);
d3 = A(:,3);
d1l = log(d1);
d2l = log(d2);
d3l = log(d3);
ekv = log(k1) + k2*d1l/2 + k3*d2l/2 + k4*d3l;
diff = ekv - log(Y);
thanks
  2 Comments
Daniel Shub
Daniel Shub on 25 Oct 2011
Don't do a fitting procedure (e.g., lsqnonlin). In this form, all you need to do is a linear regression. You can explicitly solve for the optimum parameters.
Mamed
Mamed on 2 Nov 2011
But I dont understand why I get better result with the nonlinear than with the linear.
According to wikipedia
In LLSQ the solution is unique, but in NLLSQ there may be multiple minima in the sum of squares.
Which means that if I linearize then we should have 1 unique value which should be equal or better than that of the non-linear result.
If it isnt does that mean that when we linearize then we get 1 unique solution but that solution is not the optimum?
It would be great if someone could sheed some light on this.
Thank you

Sign in to comment.


Mamed
Mamed on 17 Jan 2012
How do i use a linear regression when all the linear fittings are already predefined? ie polyfit or poly val. I wanna be able to fit with the function that I have. What should i use instead of lsqnonlin?
Also: I have used nlinfit and it gives a worse fit than fminsearch which is really strange as it should be the other way around.
But both give the second parameter as negative which means that the temperature is getting lower with increased copper losses. Which is really crazy and i am out of ideas. Any help please.
  1 Comment
Walter Roberson
Walter Roberson on 17 Jan 2012
I think you should probably start a new Question for this topic.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!