Mathematica Curve fitting in Mathematica with FindMinimum function

AI Thread Summary
The discussion revolves around using the FindMinimum function with the Levenberg-Marquardt method to fit experimental data. A user seeks guidance on defining the residuals for fitting a function f(x, a, b) to a dataset. They clarify that the residuals can be represented as a vector of the differences between the observed values and the function's predictions. A solution is provided, demonstrating how to calculate the sum of squared residuals and apply FindMinimum to optimize the parameters a and b. The user successfully implements this method but later prefers to define the residual as an n-dimensional vector. They discover that it is not necessary to explicitly define the objective function; using "0" suffices for the algorithm to function effectively. This insight streamlines their approach to fitting the data.
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.
 

Similar threads

Replies
1
Views
2K
Replies
1
Views
1K
Replies
4
Views
1K
Replies
19
Views
2K
Replies
12
Views
4K
Replies
1
Views
2K
Replies
13
Views
2K
Replies
6
Views
5K
Back
Top