imaginary value from solution nonlinier least square
Ältere Kommentare anzeigen
im trying to fitting data from reflectance measurement with lavenberg marquardt algorithm , ive got this code LMFnlsq in file exchange but i dont understand how to use it in my case
here is the equation
R = (0.6/(a + b))*(sqrt(3*a*(a + b))+(1/c))*(exp(-sqrt(3*a*(a + b ))*c))/(c^2)
a and b is parameter that im trying to solve
R is known vector from measurement reflectance
c is known vector from distance in measurement
im using the code like this
r = [0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.6]';
data = [0.787357544 0.63199586 0.393109753 0.271890049 0.179074593 0.133498607 0.112568249 0.05274273]';
res = @(x) ((0.6./(x(1)+x(2))).*(sqrt(3.*x(1).*(x(1)+x(2)))+(1./r)).* (exp(-sqrt(3*x(1).*(x(1)+x(2)))*r))./(r.^2)) - data;
x0 = [0.1,10];
[x,ssq,cnt] = LMFnlsq(res,x0)
hold on
plot(r,data)
plot(r,res(x)+ data,'y'), grid
the problem is it sure did the iteration but the final value is in imaginary solution like this
x =
0.0528 - 0.2730i
1.1469 + 0.6248i
ssq =
0.0091
cnt =
29
anything wrong with the code?
Akzeptierte Antwort
Weitere Antworten (1)
Matt J
am 28 Feb. 2013
0 Stimmen
You have square roots in your function, which will produce complex values when their argument goes negative. You must use another algorithm, one that lets you constrain 3*a*(a + b))+(1/c) to a strictly positive lower bound.
4 Kommentare
Surpan
am 28 Feb. 2013
Adding real(...) isn't a great solution because then your function would be non-differentiable where 3.*x(1).*(x(1)+x(2)))+(1./r) = 0.
If you have the Optimization Toolbox, there are lots of alternative solvers that would let you incorporate constraints.
Surpan
am 28 Feb. 2013
Matt J
am 28 Feb. 2013
but i must solve it with lavenberg marquardt algorithm
Meaning it's an assignment? Then get clarification from the one who assigned it to you. LM is not applicable to constrained problems.
Kategorien
Mehr zu Systems of Nonlinear Equations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!