Problem in using Function with in a function

2 次查看(过去 30 天)
I am writing a code to join two GUI blocks. In my code i have genereated matrices A, B, C and X based on the input. now i need to optimize A and B so i added an optimize function to it. But in the optimization i need to use X and C which are already generated in the above function. can any one help how i can declare X and C as global functions so that the optimization will take the same value what ever my first function is generating.

采纳的回答

Titus Edelhofer
Titus Edelhofer 2012-1-13
Hi,
you don't need to use global variables. You can use anonymous functions to do so. If this is the function to be optimized:
function y = myobjective(AB, C, X)
% AB is the variable to be optimized, e.g.,
A = AB(1:5);
B = AB(6:10);
y = ...; % compute y with AB, C, X ...
Then in your function where you start the optimization:
C = ...; % compute C
X = ...; % compute X
% now build the objective function handle: C and X are "stored"
% in fun, so when called by optimizer, C and X are passed
fun = @(x) myobjective(x, C, X);
% call optimizer
x = fmincon(fun, ...);
Hope this helps,
Titus
  2 个评论
Srihari
Srihari 2012-1-16
Hi Titus,
now i have an another problem in declaring the function.
Imagine
fun(x_o,y_o)=(x,y)
since i am writing code for GUI
my matrix have angles which are given by the user
A=[cos(alpha)...(phi)];
B=[sin(alpha)....];
C=[...tau];
X = [fft(x);fft(y)];
Y =A*(C.*(B*X));
i will generate a valu P based on the output. Based on P i need to optimize alpha and phi.
so i need to use an other function where all the values are declared again and used for optimization.
Now the problem is i need to use the values of C and X which are generated by the first function.
I tried but i didnt able to generate the same values..please help me
Titus Edelhofer
Titus Edelhofer 2012-1-16
Sorry, but I don't quite understand the question...

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Direct Search 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by