How to return to the main function from function file by using return

I'm using simulated annealing to do minimization for my model.
The following is my main function.
lb=[0.01 0.01 0.01]; ub=[0.9 3.0 0.9]; z=[0.3 2 0.4];
for i=1:100
h=@test;
est=simulannealbnd(h,z,lb,ub,options);
end
for the h function file as shown,
i have one condition to consider to estimate z
let's say the condition=exp(z(2)+(1-z(1)))-(1/z(3));
if the condition is fulfill
then run
sumQ=0;
for i=2:length(Y)
Q=model likelihood
sumQ=sumQ+Q;
end
Qfun=sumQ;
else
return;
end
Otherwise, have to go back to the main function to load simulannealbnd to obtain the new set of z from lb and ub.
The question is: When i use return to go back to main function, the error showing Qfun is not assigned any value?
Anyone can help with return command to go back to the main function and run again?

Answers (2)

You could put the line
Qfun = [];
before your return statement.

5 Comments

But i got this error after trying,
Error in simulannealcommon (line 111) solverData = samakedata(solverData,problem,options);
Error in simulanneal (line 44) [x,fval,exitflag,output,solverData,problem,options] = ...
Error in simulannealbnd (line 122) [x, fval, exitflag, output] = simulanneal(FUN, x0, [], [], [], [], lb, ub, options);
Error in Runfile (line 28) est=simulannealbnd(h,z,lb,ub,options);%
Can i assign any value for Qfun? Say like Qfun=1. It won't affect the new picking value for vector z, right?
You have a bit of an odd mix of code and pseudocode in your question. Can you post your actual code?
Yes, you should be able to assign any value to Qfun. (But I was assigning an empty array, which should also have worked. You just need to the variable to exist, to send it out as output.) You may have other errors.
This is my main function
warning('off') options=optimset('Display','off');
format long; z=[0.3 2 0.6];
samplesize=100; iterate=15; sumest=0; sumSE=0;
lb=[0.01 0.01 0.01]; ub=[0.9 10.0 0.9];
for iter=1:iterate
h=@clstest; est=simulannealbnd(h,z,lb,ub,options);%
sumest=sumest+est; SE=(est-z).^2; sumSE=sumSE+SE;
fprintf('%1.0i\n',iter);
end
MSE=sumSE/iterate; SD=sqrt(MSE/samplesize) %sample standard deviation zEstimate=sumest/iterate % sample mean
and this is my function file
function Qfun=clstest(z)
Y=invtransgen; cond=exp(z(2)*(1-z(1)))-(1/z(3));
if cond<0 sumQ=0; for i=2:length(Y)
Q=(Y(i)-z(3)*z(1)-(1-z(3)*z(1))*z(2))^2; %model likelihood
sumQ=sumQ+Q;
end
Qfun=sumQ;
else
Qfun=[];
return
end
and it turns out the error message:
Error using samakedata (line 30) Your objective function must return a scalar value.
Error in simulannealcommon (line 111) solverData = samakedata(solverData,problem,options);
Error in simulanneal (line 44) [x,fval,exitflag,output,solverData,problem,options] = ...
Error in simulannealbnd (line 122) [x, fval, exitflag, output] = simulanneal(FUN, x0, [], [], [], [], lb, ub, options);
Error in Runfile (line 21) est=simulannealbnd(h,z,lb,ub,options);%
Okay i assigned some value for Qfun and it can be passed back to the main function. But the simulannealbnd command doesn't really working as it gives me the initial values for the z estimates.

Sign in to comment.

Put
Qfun = sumQ
right before the return

3 Comments

I always initialize return args as the first lines in the function, just in case an exception gets thrown and it never makes it as far as that return statement.
I don't get it. How to initialize args as the first line in the function?
Qfun=sumQ getting the same error...
Just put that line immediately after the function line, before any of the other lines in the function. Then Qfun is guaranteed to have a value, even if it's null, no matter what else happens in the code (i.e. you encounter an error before you get to assign it with the desired values). So that error about exiting the function without assigning QFun will never happen - though you may get other errors.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 20 Aug 2013

Community Treasure Hunt

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

Start Hunting!