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.
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Direct Search에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!