How to define variables for nested functions

3 views (last 30 days)
Michele
Michele on 13 Jun 2014
Commented: Brian B on 13 Jun 2014
Dear all, I am a newbie in Matlab and I have tried to fix my problem looking into other threads, unsuccessfully, so let's cut to the chase.
I have some output from another program, vectors KI and KII (real numbers), that are m x 1. I need to find, numerically, the minimum of a function G(alpha), with
G(alpha)=f(alpha,c11(alpha),c12(alpha))^2+g(alpha,c21(alpha),c22(alpha))^2
In particular I have
c11 = @(alpha) 3/4*cos(alpha/2)+1/4*cos(3*alpha/2);
c12 = @(alpha) -3/4*(sin(alpha/2)+sin(3*alpha/2));
c21 = @(alpha) 1/4*(sin(alpha/2)+sin(3*alpha/2));
c22 = @(alpha) 1/4*cos(alpha/2)+3/4*cos(3*alpha/2);
f = @(alpha,c11,c12) c11*KI+c12*KII;
g = @(alpha,c21,c22) c21*KI+c22*KII;
G = @(alpha,f,g,c11,c12,c21,c22) (f.^2+g.^2)
I know that I can simplify and eliminate some functions, but I would like to keep it this way as it is easier to read. I have 2 problems:
1) When I define the function G, should I define it as function of all the variables, even the ones of the nested functions?
2) How can I substitute, at a given i, the actual value of KI(i) and KII(i) to compute the minimum of G(alpha)?
Thanks, Michele

Answers (3)

Brian B
Brian B on 13 Jun 2014
I assume alpha is a scalar variable. Then you can write f, g, and G as
f = @(alpha) c11(alpha)*KI+c12(alpha)*KII;
g = @(alpha) c21(alpha)*KI+c22(alpha)*KII;
G = @(alpha) f(alpha).^2 + g(alpha).^2;
As for the second part of your question, it is not clear what you mean. Do you want to minimize G separately for each pair of values (KI(i), KII(i)), i=1, ..., m? If so, you could define instead
f = @(alpha,i) c11(alpha)*KI(i)+c12(alpha)*KII(i);
g = @(alpha,i) c21(alpha)*KI(i)+c22(alpha)*KII(i);
Gi = @(alpha,i) f(alpha,i).^2 + g(alpha,i).^2;
  1 Comment
Brian B
Brian B on 13 Jun 2014
I didn't explain how to use Gi. You might do something like
for i=1:m
G = @(alpha) Gi(alpha, i);
[x(i), fval(i)] = fminsearch(G, 0);
end

Sign in to comment.


Michele
Michele on 13 Jun 2014
Ok for the second question. As regards the first one, it doesn't work. To simplify the problem I wrote
KI=0.123;
KII=0.002;
c11 = @(alpha) 3/4*cos(alpha/2)+1/4*cos(3*alpha/2);
f = @(alpha) c11(alpha)*KI
G = @(alpha) f(alpha)
[x, fval] = fminsearch(G(alpha), [-6*pi,6*pi])
But it returns the error
Error using alpha
Too many output arguments.
Error in test (line 29)
[x, fval] = fminsearch(G(alpha), [-6*pi,6*pi])
Now there are 2 cases: 1) The way I defined nested functions is uncorrect 2) I should call fminsearch differently. I tried though
[x, fval] = fminsearch(G(alpha), [-6*pi,6*pi])
[x, fval] = fminsearch(G(f(c11(alpha))), [-6*pi,6*pi])
and still got the same error
  2 Comments
Kelly Kearney
Kelly Kearney on 13 Jun 2014
Edited: Kelly Kearney on 13 Jun 2014
Also, the second input to fminsearch should be a scalar:
[x, fval] = fminsearch(G, 0)
(Your error came because, with no variable named alpha in your workspace, Matlab instead interpreted it as a call to the function alpha.)

Sign in to comment.


Michele
Michele on 13 Jun 2014
@Brian: I tried that, but it didn't work. I fixed the problem right before I got Kelly's suggestion: even though in the example for the fminsearch it is shown
[x,fval] = fminsearch(banana,[-1.2, 1])
it actually does not accept a vector for the bounds. Instead
[x fval]=fminsearch(G,-6*pi,6*pi)
make the trick. Btw I have seen that this function does not handle very well if you have multiple minima (same values): it simply returns one of the values. Any suggestions for other functions? Anyway thanks a lot for your help. On friday evening even more ;)
  1 Comment
Brian B
Brian B on 13 Jun 2014
Edited: Brian B on 13 Jun 2014
fminsearch is for unbounded problems. You might look at fminbnd if you know bounds. Both functions look for a single local optimum. If your cost function is multivariate, you need to use fmincon instead of fminbnd.
If you want to find all minima, you could try starting the optimization from a lot of different starting points and comparing the results. The Global Optimization Toolbox ( http://www.mathworks.com/help/gads/how-globalsearch-and-multistart-work.html#bsc9eez ) provides functions which do this automatically, but it should be very easy to do in a for loop for a scalar variable.

Sign in to comment.

Categories

Find more on Optimization 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!