random equation fitting to data set and finding constant parameters
2 次查看(过去 30 天)
显示 更早的评论
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.
采纳的回答
Amit
2014-1-22
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 个评论
aditi
2014-1-22
Hey...thanks for the response...
I am new to matlab...could you please explain the steps so that i can try accordingly..plz
Thanks
aditi
2014-1-22
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.
Amit
2014-1-22
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));
aditi
2014-1-22
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
aditi
2014-1-22
hey bruno... could you please explain how above problem can be solved by fit function??
Amit
2014-1-22
Amit
2014-1-22
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.
aditi
2014-1-22
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??
Amit
2014-1-22
One the command window in matlab, type:
d = my_par(1)*exp(x.^2-2^my_par(2));
plot(x,y,x,d,'r');
aditi
2014-1-22
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?
Amit
2014-1-22
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');
aditi
2014-1-22
equation i corrected... it was just x in place of 2 in first part of bracket...after correcting i got above results... :(
aditi
2014-1-22
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??
aditi
2014-1-22
is there any other method to do this problem???? or what can be corrected in this method itself????
Amit
2014-1-22
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.
aditi
2014-1-23
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
aditi
2014-1-23
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
aditi
2014-1-23
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)
aditi
2014-1-23
and also could you please send me how you wrote that equation??? want to verify if i am using right brackets or not
aditi
2014-1-23
no...there is no exponential...plz see the .jpg file i sent to you...the equation is clear there..
aditi
2014-1-23
i wrote the equation like this:
a*((x.^(-1/b)) - (37^(-1/b)))
i dont know if its correct or not
Amit
2014-1-23
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.
aditi
2014-1-23
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??
aditi
2014-1-23
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 :)
Amit
2014-1-23
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.
aditi
2014-1-23
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
aditi
2014-1-23
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???
更多回答(1 个)
Matt J
2014-1-22
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 个评论
aditi
2014-1-22
Hey matt... thanks for the reply...
could you please explain how to use FMINSPLEAS please...
aditi
2014-1-22
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
Matt J
2014-1-22
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.
aditi
2014-1-22
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???
Matt J
2014-1-22
编辑:Matt J
2014-1-22
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.
Walter Roberson
2017-10-21
par_fit is defined in Amit's code, above https://www.mathworks.com/matlabcentral/answers/113318-random-equation-fitting-to-data-set-and-finding-constant-parameters#answer_121842
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interpolation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)