Error using fsolve or lsqnonlin
Show older comments
Hello, I am trying to solve a nonlinear function but I always get an error. I've tried to use fsolve and lsqnonlin but they're both not working. Can anybody help me?
Here is the part of my code where I use fsolve/lsqnonlin
for j=1:length(c_CSB_KA)
k=100;
f_A_in=(0.35-0.2)*rand(k,1)+0.2;
f_B_in=(0.4-0.1)*rand(k,1)+0.1;
x0=[f_A_in,f_B_in];%Anfangswerte
CSB = c_CSB_KA(j);
AFS = c_AFS_KA(j);
fS = f_S(j);
iCSB = 1.6;
fCSB = 0.15;
CSB_Fraktionierung = @(x)[
AFS*iCSB*(1-x(2))*x(1);
AFS*iCSB*(1-x(2))*(1-x(1));
fCSB*(CSB*(1-fS)-AFS*iCSB*(1-x(2))*x(1));
CSB*(1-fS)-AFS*iCSB*(1-x(2))-fCSB*(CSB*(1-fS)-AFS*iCSB*(1-x(2))*x(1))
];
x= lsqnonlin(CSB_Fraktionierung,x0);
a=x';
ak(:,:,j)=a; %Parameter fA,fS in ak speichern
end
And I got this error:
Error using levenbergMarquardt (line 16)
Objective function is returning undefined values at initial point. lsqnonlin cannot continue.
Error in lsqncommon (line 175)
levenbergMarquardt(funfcn,xC,flags.verbosity,options,defaultopt,initVals.F,initVals.J, ...
Error in lsqnonlin (line 253)
lsqncommon(funfcn,xCurrent,lb,ub,options,defaultopt,allDefaultOpts,caller,...
Error in Monte_Carlo (line 76)
x= lsqnonlin(CSB_Fraktionierung,x0);
Thank you for your help! :)
9 Comments
Adam Danz
on 23 Aug 2018
It's likely that your initial guesses (x0) do not return valid outputs from the function you're minimizing (CSB_Franktionierung). Have you checked that? In other words, have you run the CSB_Franktionierung function using the x0 initial inputs to confirm that it produces valid outputs?
Judith Lange
on 23 Aug 2018
Judith Lange
on 23 Aug 2018
Torsten
on 23 Aug 2018
Yes, that's why the objective is undefined at the initial point.
Judith Lange
on 23 Aug 2018
Adam Danz
on 23 Aug 2018
Looks like you found the source of the error, nice! Without being able to run the code, it will be difficult (or impossible) for us to fix it.
Judith Lange
on 23 Aug 2018
If it works in the command line but not when running the code, use the debug tool to stop the code just before executing the problematic lines and make sure the input values are as you expect them to be (they probably aren't). Then you can work your way backwards to see what's going on. This is the art of debugging (which you may already know) and once you get the hang of it, you'll become a hero.
Answers (0)
Categories
Find more on Word games in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!