How to define variables for nested functions
6 views (last 30 days)
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
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)?
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;