Error using fsolve or lsqnonlin

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 个评论

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?
Torsten
Torsten 2018-8-23
编辑:Torsten 2018-8-23
x0 must have 2 elements, not 200.
I am trying to run a Monte Carlo Simulation. That's why I use 100 elements for each parameter but I guess that's not how it works...
I just tried to run x= lsqnonlin(CSB_Fraktionierung,x0); using only two elements in x0 and the same error occured...
Oh and I tried to run my function with an initial point (2 elements)
CSB_Fraktionierung(x0)
ans =
81.4170
216.6108
NaN
NaN
Are the nans the problem?
Yes, that's why the objective is undefined at the initial point.
Okay I hope it's not too confusing but I think I found a mistake in my code before the section I posted. So here is basically the rest of my code (one for loop around everything is missing):
M=xlsread(['KA-J-RVB-' int2str(i) '.xlsx'],['KA-J-RVB-' int2str(i) ''],'A14:AS2571');
M(M==0)=NaN;
%Vektoren mit benötigten Daten
c_CSB_KA=M(:,4);
c_BSB5_KA=M(:,5);
c_AFS_KA=M(:,6);
c_CSB_filt_NK=M(:,42);
%Interpolation c_CSB_filt_NK
for n=1:length(c_CSB_KA)
if isnan(c_CSB_filt_NK(n))>0
c_CSB_filt_NK_int(n)=interp1(int_M_d(:,1),int_M_d(:,2),c_CSB_KA(n));
else
c_CSB_filt_NK_int(n)=c_CSB_filt_NK(n);
end
end
c_CSB_filt_NK_int=c_CSB_filt_NK_int';
f_S=(0.9*c_CSB_filt_NK_int)./c_CSB_KA;
The thing is: the last two lines aren't properly excecuted. When I type
c_CSB_filt_NK_int=c_CSB_filt_NK_int';
f_S=(0.9*c_CSB_filt_NK_int)./c_CSB_KA;
in the command line it works but not in the script/loop and then I have an NAN in my function (CSB_Fraktionierung)...
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.
Okay then I am kind of stuck again :/ but thank you for your help everybody :)
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.

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Word games 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by