- You will see updates in your activity feed.
- You may receive emails, depending on your notification preferences.

- 42 questions asked
- 1 answer
- 0 accepted answers
- reputation: 0

- 42 questions asked
- 1 answer
- 0 accepted answers
- reputation: 0

89 views (last 30 days)

89 views (last 30 days)

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.

- 12 questions asked
- 241 answers
- 119 accepted answers
- reputation: 595

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));

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

Amit, could you also use `fit` with a custom function?

hey bruno... could you please explain how above problem can be solved by fit function??

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');

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

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)

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.

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???

Log in to comment.

- 104 questions asked
- 4,068 answers
- 1,622 accepted answers
- reputation: 8,755

Answer by Matt J
on 22 Jan 2014

- 104 questions asked
- 4,068 answers
- 1,622 accepted answers
- reputation: 8,755

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

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

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.

Log in to comment.

Log in to answer this question.

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply TodayUnable to complete the action because of changes made to the page. Reload the page to see its updated state.

You can also select a location from the following list:

- Canada (English)
- United States (English)

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

## 0 Comments

Log in to comment.