Curve fitting in Mathematica with FindMinimum function

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 6K views
grzesz
Messages
2
Reaction score
0
Hello,
I attempt to fit experimental data using FindMinimum function with Levenberg-Marquardt (LM) method. I know that the problem can be solved with FindFit function (or other methods of finding minimum), but I really have to use FindMinimum function and LM method. Therefore, I would appreciate if someone could explain how to use FindMinimum function to fit a set of experimental points (with LM method).

Let’s say that I have to fit a function f(x,a,b), where a and b are fit parameters, to a dataset that looks like this:{{x1,y1},...,{xn,yn}}.
1. How to define "Residual"? Is this a vector V of a form {y1-f(x1,a,b),...,yn-f(xn,a,b)}?
2. Is it necessary to define also the function f in FindMinimum[f,...]? How to do this?
I would be grateful for any help with this issue.
Regards,
G.
 
Physics news on Phys.org
I see you have asked this same question in a number of other places and for some reason do not seem to be getting any replies.

Perhaps you can see how to adapt this to your problem.

In[1]:= dataset={{1,2},{3,7},{4,3}};
f[x_,a_,b_]:=a x+b;
sumsquare=Total[Map[(#[[2]]-f[#[[1]],a,b])^2&,dataset]]
Out[3]= (3-4a-b)^2+(7-3a-b)^2+(2-a-b)^2

In[4]:= FindMinimum[sumsquare,{{a,7},{b,2}},Method->LevenbergMarquardt]
Out[4]= {12.0714,{a->0.642857,b->2.28571}}

In[5]:= {a,b}/.Last[%]
Out[5]= {0.642857,2.28571}
 
First of all thank you very much for your help. I tried this solution and it works very well. However, for my problem it was more conveniently to define „Residual” in the way that I described in my previous post (as an n-dimensional vector V). After that I just used formula:

FindMinimum[(0),{a, a0}, {b, b0}, Method->{“LevenbergMarquardt”, “Residual” -> V}]

The problem was that I did not know how to define the objective function. After some time I figured out that it is unnecessary. You can use “0” instead the objective function and the algorithm works pretty well.