How to define variables for nested functions
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(3 个)
Brian B
2014-6-13
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 个评论
Brian B
2014-6-13
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
Michele
2014-6-13
2 个评论
Kelly Kearney
2014-6-13
编辑:Kelly Kearney
2014-6-13
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.)
Michele
2014-6-13
1 个评论
Brian B
2014-6-13
编辑:Brian B
2014-6-13
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!