- go into an infinite loop
- throw an error
- freeze
- crash your computer
- provide an answer that you suspect is not correct
- short-circuit your mains power
- return nothing when you expected an output
- ignore your attempt to call the function
- quit its job and start writing poetry instead
"fminsearch" and issues
6 views (last 30 days)
Show older comments
Hello The Matlab community,
I am back again with more simple questions, which I cannot find answers for easily. In the following code, I am trying to compute Rc and Rw, but the function does not execute for some reason. Will you please have a look and help find a solution to the problem?
close all
clear all
clc
global sig nu a a1 a2 S phiw phic b delta tauc L1 tauw
T=50;
a=0.6;
a1=0.20;
a2=0.20;
delta=0.8;
tauc=0.02;
tauw=0.01;
gamma=0.11
sig=3;
phic=3;
phiw=6;
%Initial values
Rc0=5;
Rw0=10;
Sw0=100;
Sc0=400;
Ec=zeros(T,1);Ew=zeros(T,1);
Yc=zeros(T,1); Yw=zeros(T,1);
Y=zeros(T,1);
xc=zeros(T,1);xw=zeros(T,1);
Pc=zeros(T,1); Pw=zeros(T,1);
pc=zeros(T,1); pw=zeros(T,1);
cc=zeros(T,1); cw=zeros(T,1);
Rc=zeros(T+1,1);Rw=zeros(T+1,1);
Rc(1,1)=Rc0; Rw(1,1)=Rw0;
Sc=zeros(T+1,1); Sw=zeros(T+1,1);
Sc(1,1)=Sc0; Sw(1,1)=Sw0;
Lw=zeros(T,1);Lc=zeros(T,1);
Xw=zeros(T,1);Xc=zeros(T,1);
Pic=zeros(T,1);Piw=zeros(T,1);
for t=1:T
S=[Sc(t),Sw(t)];
x0=[Rc(t),Rw(t)];
options=optimset('TolFun',1e-11,'TolX',1e-9,'MaxFunEvals',10e+12,'MaxIter',10e+10);
out1=fminsearch(@prod_vL2,x0,options);
Rc(t+1)=out1(1);
Rw(t+1)=out1(2);
Lc(t)=L1;
Lw(t)=1-Lc(t);
Pc(t)=Lc(t)^(1/(1-sig));
Pw(t)=(1-Pc(t)^(1-sig))^(1/(1-sig));
Ec(t)=((a2*Pc(t)^(1/(1-a1))*Lc(t)^((1-a)/(1-a1))*Rc(t+1)*Sc(t))/phic)^((1-a1)/a2);
Ew(t)=((a2*Pw(t)^(1/(1-a1))*Lw(t)^((1-a)/(1-a1))*Rw(t+1)*Sw(t))/phiw)^((1-a1)/a2);
Xc(t)=Sc(t)/(Rc(t+1)^gamma);
Xw(t)=Sw(t)/(Rw(t+1)^gamma);
Sc(t+1)=Sc(t)+Xc(t)-Ec(t)/Rc(t+1);
Sw(t+1)=Sw(t)+Xw(t)-Ew(t);
FUNCTION
function F=prod_vL2(x)
global a a1 a2 sig S phiw phic nu delta tauc d L1 L2 tauw
d=(1-sig)*(1-a1)/(2*(1-(1-a)*(1-sig)));
L1=(phic*x(1)^(-delta)/(tauc*delta*a1*(1-a1)*a2*S(1)))^d;
L2=(phiw*x(2)^(-delta)/(tauw*delta*a1*(1-a1)*a2*S(2)))^d;
F=L1+L2-1;
2 Comments
Stephen23
on 3 Jan 2015
Edited: Stephen23
on 4 Jan 2015
What exactly does "the function does not execute" mean? Did MATLAB:
The more useful information you give us, the better we can help you. Re-read your question: you demand a lot ("have a look and help find a solution to the problem") and yet do not tell us exactly how you are using this code or exactly what kind of problem you are experiencing. What do you want us to do?
Answers (2)
John D'Errico
on 3 Jan 2015
Edited: John D'Errico
on 3 Jan 2015
Um, look how you called it!
options=optimset('TolFun',1e-11,'TolX',1e-9,'MaxFunEvals',10e+12,'MaxIter',10e+10);
So you have chosen to allow 10 trillion function evaluations, that is 10e12 of them! Ok, only 100 billion iterations. You do realize how long that would take IF you got anywhere near those limits? Then, you put it in a loop, executing that loop 50 times. You are complaining that it seems like MATLAB has hung up? Are you really surprised?
Really though, you need to learn that when using an optimizer, if you are unsure what is happening, to look at the iterations! Look at where the optimizer is going! Think for yourself. Don't just let the computer do magic, and hopefully return a solution that makes sense. So, learn to either...
- Put in a line into your objective function that displays the current step, and the objective as returned.
- Use the display option for the optimizer.
- Use the debugger.
Any of these will show you what is happening. Is the optimizer diverging to infinity? Is it stuck somehow? Is there a problem with your objective? Or with the tolerances?
Don't treat these tools like a magic black box. Look at what it is doing. Think about what it is doing.
Oh, by the way, I tried a quick test of your function in fminsearch as you call it. It looks like you have posed such extremely tight tolerances on the solution, for an objective function that wants to diverge, but is essentially a constant to machine precision. But since your tolerances are tight enough, it keeps going. A surface plot and/or a contour plot would seem to help greatly.
ezsurf(@(xx,yy) prod_vL2([xx,yy]),[1,1000],[1,1000])

Gosh, I wonder why fminsearch is having a problem? A good rule to remember is to to never just throw an optimizer at something without plotting the objective if you can, well, at least unless you absolutely know what is happening. Even then a good rule is to use the 'display','iter' options in optimset the first time you do any optimization.
Think about what you are doing. Plot EVERYTHING, especially if you are unsure what is happening.
1 Comment
Stephen23
on 4 Jan 2015
A very clear and detailed explanation. Hopefully the OP can learn something useful from this excellent breakdown of the problem.
Star Strider
on 3 Jan 2015
I changed your code to get rid of the globals (globals can cause problems), and added the necessary variables into the argument list:
function F=prod_vL2(x, a, a1, a2, sig, S, phiw, phic, delta, tauc, tauw)
and changed the fminsearch call accordingly:
out1=fminsearch(@(x) prod_vL2(x, a, a1, a2, sig, S, phiw, phic, delta, tauc, tauw),x0,options);
When I then ran your code and removed the trailing semicolon from ‘F’ in the function, your function produced a repeating series of:
F =
-786.1969e-003
F =
-786.1777e-003
F =
-786.2238e-003 + 59.1647e-006i
F =
-786.2170e-003 + 73.1128e-006i
that otherwise never change.
Obviously, something is not working as you want it to. I have no idea what you are doing, so I will leave you to figure out what the problem might be. It’s easier and more efficient to use the debugger rather than remove semicolons, but as a first step to see what the function is doing, it at least demonstrated the problem.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!