solve nonlinear equations with fsolve - exitflag is 2 and the roots are complex numbers
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi Matlab experts,
I am trying to solve a system of highly nonlinear equations below: f(x) = .01*(2*(1.04+x).^(-4.8).*(.1+x).^(-1.9)+(1.04+x).^(-3.8).*(.1+x).^(-2.9)).*A-3*B; where both A and B are vectors, say of size 100 by 1.
I tried to solve this nonlinear equation by fsolve and I run into 3 issues: 1) the results from using fsolve in vector form (solve all equations in one go) are different from in scalar form (each time solve one equation) 2) I got complex results, no matter what starting values I tried, particularly in vector form (almost all x estimated are complex). Results from the 3 procedures are different too! 3) when I try to solve the nonlinear problem equation by equation, I get exitflag = 2 for some equations, is the result with exitflag = 2 correct? In this case I also try different starting values and it does not work.
I add in 'JacobPattern' in options as suggested by one of your colleague but it does not seem to help much.
Thank you so much for your help!
Below is my matlab codes:
////////////////////////////////////////////////////////////////////////////////////////////
function main()
% assign values for B and A (skipped here)
........
% Solve nonlinear equations one by one%
options=optimset('Display','off');
sb = length(B);
for i = 1:sb
fmiu = @(x)miu(B(i),A(i),x);
[xs(i,1),fval(i,1),exitflag(i,1)] = fsolve(@(x)fmiu(x),.05,options);
while exitflag(i,1) <= 0
[xs(i,1),fval(i,1),exitflag(i,1)] = fsolve(@(x)fmiu(x),rand(1)*.1,options);
end
end
if sum(xs ~= real(xs)) disp('!!!Warning: xs is complex!!!'); mu end
[xs mu]
% solve nonlinear equations in one go - vector form%
options = optimset('Display', 'notify', 'JacobPattern', speye(sb));
fx = @(x)miu(B,A,x);
[xz, fval, exitflag] = fsolve(@(x)fx(x),rand(sb,1), options);
% Private subfunction
function fun = miu(B,A,x)
fun = .01*(2*(1.04+x).^(-4.8).*(.1+x).^(-1.9)+(1.04+x).^(-3.8).*(.1+x).^(-2.9)).*A-3*B;
end
end
3 Kommentare
Sargondjani
am 20 Jun. 2012
and you get different results every time because you take different starting values (random draws). fsolve only finds a local minimum, so it can get stuck in different local optima
if you use the jacobpattern and use the same starting values, the solutions should be the same for each sinlge equation (or at least very close to each other)
Sargondjani
am 20 Jun. 2012
pls note that with walter's solution the problem becomes non-differentiable at the break. this will make derivative based methods problematic
Antworten (0)
Siehe auch
Kategorien
Mehr zu Systems of Nonlinear Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!