I keep getting this error (Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue)
3 views (last 30 days)
Show older comments
My currenct script looks like this:
function F = myfun(x)
x0 = [0.1 0.2 0.3 0.4]
x = x0
f1 = inline('1*(1 - x(1))+100*(-1*x(1)*x(2))');
f2 = inline('1*(2 - x(2))+100*(-1*x(1)*x(2)-1*x(2)*x(3))');
f3 = inline('1*(0 - x(3))+100*(1*x(1)*x(2)-1*x(2)*x(3))');
f4 = inline('1*(0 - x(4))+100*(1*x(2)*x(3))');
F = [f1(x);f2(x);f3(x);f4(x)]
options=optimset('Display','iter');
x = fsolve(@myfun,x0);
end
Still nothing seems to get this working, can anyone help me out? If i do not define my first [x] he will not run because there are not enough input arguments.
Thanks in advance!
Frenk
0 Comments
Accepted Answer
Matt J
on 14 Sep 2014
Edited: Matt J
on 14 Sep 2014
The call to fsolve and the definition of myfun should take place in two separate workspaces. The function definition should look like this,
function F = myfun(x)
f1 = 1*(1 - x(1))+100*(-1*x(1)*x(2));
f2 = 1*(2 - x(2))+100*(-1*x(1)*x(2)-1*x(2)*x(3));
f3 = 1*(0 - x(3))+100*(1*x(1)*x(2)-1*x(2)*x(3));
f4 = 1*(0 - x(4))+100*(1*x(2)*x(3));
F = [f1;f2;f3;f4];
end
The call to fsolve should be in a different function and look like this,
x0 = [0.1 0.2 0.3 0.4];
options=optimset('Display','iter');
x = fsolve(@myfun,x0,options);
Notice also that there is no need to be using inline() in this situation. Moreover, unless you have a really old version of MATLAB, you should be discontinuing the use of inline() altogether and using Anonymous Functions instead.
1 Comment
Matt J
on 14 Sep 2014
Frenk's Comment:
Thank you for your explanation, the question has been solved and i can continue with my proceedings. I will keep in mind not using the inline command.
More Answers (2)
Yahia Mounir
on 14 Sep 2015
Hello I am trying to run the following file however it give me error undefined variable X, I attached the my file can you help with that ,
Please note if i call the function at command window with the following steps @myfun t x0=[0 0] x=(@myfun,xo) it is working and giving me the answer i need only to use the mfile to get the answer
1 Comment
Walter Roberson
on 14 Sep 2015
Just like Matt J explained, you need to be using two different files.
File myfun.m
function F = myfun(x)
F = [2*x(1) - x(2) - exp(-x(1));
-x(1) + 2*x(2) - exp(-x(2))];
end
file myfun_driver.m
x0=[0 0];
options = optimoptions('fsolve', 'Display', 'iter'); % Option to display output
x = fsolve(@myfun,x0,options);
Then you would invoke
myfun_driver
to run the code.
Henn
on 31 Jul 2017
My current script looks like this:
for kk=1:+1:21;
sigma=sigmastart+0.01*(kk-1)
beta1 = 0.5-alpha/sigma^2+sqrt((0.5-alpha/sigma^2)^2+2*rho/sigma^2);
beta2 = 0.5-alpha/sigma^2-sqrt((0.5-alpha/sigma^2)^2+2*rho/sigma^2);
e=0:+0.001:100;
%guesses for investment and switching thresholds
xi010=(beta1/(beta1-1))*(rho-alpha)*(H*F*rho*I)/rho;
xi120=(beta2/(beta2-1))*(rho-alpha)*(H*F/rho-S12);
xi210=(beta1/(beta1-1))*(rho-alpha)*(H*F/rho+S21);
a010=xi010^(1-beta1)/(beta1*(rho-alpha));
a120=-xi120^(1-beta2)/(beta2*(rho-alpha));
a210=xi210^(1-beta1)/(beta1*(rho-alpha));
%initial guess for operational decisions
vars0 = [a120 a210 xi120 xi210];
%specify options for fsolve
opts=optimset('fsolve');
opts=optimset(opts , 'Maxiter' , 2000 , 'Tolx' , 1e-6, 'tolfun' , 1e-6);
vars = fsolve(@fffRoinvof , vars0 , opts , beta1 , beta2 , alpha , H , F , rho , S12, S21);
and my function file looks like this
function f = fffROinvof (vars,beta1,beta2,alpha,H,F,rho,S12,S21)
a12=vars (1);
a21=vars (2);
xi12=vars (3);
xi21=vars (4);
f=zeros(4,1);
f(1)=a12*xi12^beta2+xi12/(rho-alpha)- H*F/rho-a21*xi12^beta1+S12;
f(2)=beta2*ai12*xi12^(beta1-1);
f(3)=-a21*xi21^beta1+...
1/(rho-alpha)-H*F/rho+a12*xi21^beta2-S21;
f(4)=-beta1*a21*xi21^(beta1-1)+...
1/(rho-alpha)+beta2*a12*xi21^(beta2-1);
end
However, I still get the error message when I run my script Error in Code (line 34) vars = fsolve(@fffRoinvof , vars0 , opts , beta1 , beta2 , alpha , H , F , rho , S12, S21);
Caused by: Failure in initial objective function evaluation. FSOLVE cannot continue.
1 Comment
Walter Roberson
on 31 Jul 2017
Change
vars = fsolve(@fffRoinvof , vars0 , opts , beta1 , beta2 , alpha , H , F , rho , S12, S21);
to
vars = fsolve(@(vars) fffRoinvof(vars, beta1 , beta2 , alpha , H , F , rho , S12, S21), vars0 , opts );
See Also
Categories
Find more on Function Creation 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!