random equation fitting to data set and finding constant parameters

I have x and y coordinates and I want to fit an equation:
y=a*exp(x^b - 2^b)
to the data set and thus finding parameters a and b. Please help me through it.

 Accepted Answer

First make a function that you'll use to fit like this:
function val = myfunc(par_fit,x,y)
% par_fit = [a b]
val = norm(y - par_fit(1)*exp(x.^2-2^par_fit(2)));
Now, find the parameters like:
my_par = fminsearch(@(par_fit) myfunc(par_fit,x,y),rand(1,2));

35 Comments

Hey...thanks for the response...
I am new to matlab...could you please explain the steps so that i can try accordingly..plz
Thanks
Explain the steps, in what sense? Didn't I do that?
function val = myfunc(par_fit,x,y)
% par_fit = [a b]
signal=load('mnr.txt');
x=signal(:,1);
y=signal(:,2);
for i=1:length(x)
val = norm(y(i) - par_fit(1)*(exp((x(i))^(par_fit(2)) - (2)^(par_fit(2)))));
%Now, find the parameters like:
my_par = fminsearch(@(par_fit) myfunc(par_fit,x,y),rand(1,2));
end
I did like this and i got the error saying : Input argument "par_fit" is undefined.
Okay, I get it. Open a script file and copy:
function val = myfunc(par_fit,x,y)
% par_fit = [a b]
val = norm(y - par_fit(1)*exp(x.^2-2^par_fit(2)));
Save this file as myfunc.m
Now on command window, type
my_par = fminsearch(@(par_fit) myfunc(par_fit,x,y),rand(1,2));
For more details on using fminsearch ( http://www.mathworks.com/help/matlab/ref/fminsearch.html )
okay...the error is gone...but result is not what i wanted
I have x axis values=x y axis values=y
and equation of line to be fit on the plot of data is suppose
d=a*exp(x^b - 2^b)
where a and b i have to find...
so please help according to this
hey bruno... could you please explain how above problem can be solved by fit function??
I am attaching two files. Download them in the matlab folder (both in the same folder) and run ToRun once you have loaded x and y.
Your function is y = f(x).
What you are trying is to find the parameters so that (y_you_have - f(x)) is overall minimum (in a least square). This is what you're trying to minimize using fminsearch.
fminsearch tries to minimize the objective value. norm() is like sum((y_you_have - f(x))^2). This should be close to 0 when a and b values are obtained. I hope this is good enough.
Thanks again amit... I will try now and give you the update
hey amit...
i just tried on my data set.. the values what this is giving are very less than what are desired...what could be the reason??
also I have to plot d vs x ( where d=a*exp(x^b - 2^b) ) i.e fitted equation over the data set... so what should i do??
One the command window in matlab, type:
d = my_par(1)*exp(x.^2-2^my_par(2));
plot(x,y,x,d,'r');
in the figure .. blue is the original data set...and red is d vs x...
it does not seem to be a good fit.. :(
what could be done to improve it?
here is the figure...sorryy...i forgot to attach in the previous msg...
Actually my bad:
function val = myfunc(par_fit,x,y)
% par_fit = [a b]
val = norm(y - par_fit(1)*exp(x.^par_fit(2)-2^par_fit(2))); % This was a mistake previously
I made a mistake in reading your equation. Try this. Copy the code in myfunc.m, that I gave you previously.
And for plotting:
d = my_par(1)*exp(x.^my_par(2)-2^my_par(2));
plot(x,y,x,d,'r');
equation i corrected... it was just x in place of 2 in first part of bracket...after correcting i got above results... :(
i am getting a value suppose 1 which actually should come 5 and am getting b value suppose 1...it should actually be 2...
so m getting very less values than the correct one....same is evident from the figure also..so what could have gone wrong??
I don't think it was x in place of 2. It was par_fit(2) instead of 2.
ha i did that ... i rewrote the equation... but still...same thing persists..
is there any other method to do this problem???? or what can be corrected in this method itself????
is there any chance that that equation is not the right equation for your data set?
If you upload the data, I can give it a try.
hey amit
i will give you the data for tested results so that we can cross verify:
4.8000 1.5800
8.0000 1.1500
14.5000 0.9100
22.0000 0.2600
37.0000 0 so 1st column is x axis and second is y
the equation which we have to fit is: a*(x.^(-1/b)) - (37^(-1/b)))
1) find a and b
2) then plot the data and also the fit equation vs x on the same plot to see the fitting
the correct values of parameters are: a=5.47 and b=1.91
please let me know if any othe information is required.
thanks
i have attached the .jpg file where equation is shown clearly for above data...because changing the brackets slightly is changing the value of parameters also...so please check n help me out
When I plot your equation with the parameters you gave me, I get a very different result than your data points. How would you expect to fit the curve?
but they are published results...okay i will give you another data set
4.8 0.67
8.0 0.55
14.5 0.09
22.0 0.11
37.0 0.00
and for this equation is same and b value is 1.75..(dont know the a value)
and also could you please send me how you wrote that equation??? want to verify if i am using right brackets or not
par_fit(1)*exp(x.^(-1/par_fit(2))-37^(-1/par_fit(2)))
no...there is no exponential...plz see the .jpg file i sent to you...the equation is clear there..
i wrote the equation like this:
a*((x.^(-1/b)) - (37^(-1/b)))
i dont know if its correct or not
I corrected my mistake in terms of equation. I get very high numbers for a and b. Both values fit the data to some extent (fminsearch fit is better).
No data (from experiments) are perfect. That why one has to be careful when fitting curves. You have limited number of data here (5 points) and your function is highly nonlinear. In estimating parameter, unless data points shows those features due to nonlinearity clearly, you cannot expect a unique parameter estimation. For example, I can fit a linear curve through your data points, and they will fit more or less, alright.
ohh..fit luks good...i dint get this... how did you do this??? could you please send me the .m files you made...and also how to plot..?? also what a and b values are you getting??
i just showed your plot to my professor and he said it seems fine...and asked me to try on other data sets...and verify...
thanks a lot amit.. could you please once explain how you did the above whole thing...including everything...as m a learner as per MATLAB is concerned...
Thanks :)
I did exactly what I told you earlier, just changed it to the new equation you mentioned.
I get values for a and b as, 5.55e7 and 7.12e7. Very Very high from what you said!!
I posted that plot because I wanted to show you that with limited number of data, you cannot estimate parameters for a very nonlinear function. You have to be very careful, especially in research, on how to determine parameters and then trust it.
okay...i will follow previous instructions carefully...maybe i have done something wrong...
and a big thanks to u amit...u were of great help :) will contact u if m stuck again somewher else thanks
one more thing...what i found after googling is that in such cases u have to give a specific range for 1 of the parameter... so any idea about that..??
like in above equation if i deliberately want that the b value should lie betweem 0.2 and 2 and then find a and b...how can i do that???

Sign in to comment.

More Answers (1)

You might also try FMINSPLEAS. It can take advantage of the fact that y has a linear dependence on one of the parameters 'a'.

8 Comments

Hey matt... thanks for the reply...
could you please explain how to use FMINSPLEAS please...
Once you've downloaded it, you can get a description of its use like any other MATLAB function
>> help fminspleas
Moreover, the help documentation has an example (Example 1) very similar to yours.
hey matt...i am new to matlab...so could you please explain according to my problem...
I have x axis values=x
y axis values=y
and equation of line to be fit on the plot of data is suppose
d=a*exp(x^b - 2^b)
where a and b i have to find...
so please help according to this
If you're new to MATLAB, it makes more sense to start with Amit's solution, which is simpler and which he has given you explicitly.
okay... i tried it...but the parameter values are not what i wanted.. so what could have gone wrong..??? also could you please explain the steps of the method which amit told.. i.e what does norm() step is doing...??? why are we subtracting from y???
norm(y - par_fit(1)*exp(x.^2-2^par_fit(2)))
measures the distance between the vector y of given curve samples and the vector
par_fit(1)*exp(x.^2-2^par_fit(2))
of fitted curve samples.
fminsearch tries to find the par_fit(1) and par_fit(2) that minimizes this distance, giving best agreement between y and your parametric curve model.
for some reason the variable par_fit is undefined error doesn't go away. How did you solve that error?

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 22 Jan 2014

Commented:

on 21 Oct 2017

Community Treasure Hunt

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

Start Hunting!