What is wrong with this code?
이전 댓글 표시
A=@(f,x) 2.*f(i,1).*(x(i,:)).^2
B=@(f,x) 5.*f(i,2).*(x(i,:))
objfcn=@(f,x) f(i,1).^2./(2*A(f,x))+f(i,2).^2./(2*B(f,x))
I need to use the objfcn for two variables of f . x is simply a row vector. The whole code is within a for loop with iteration i=1:10.
댓글 수: 1
KSSV
2019년 7월 29일
Show us the whole code and tell us your error.
채택된 답변
추가 답변 (3개)
Well, we can tell you what could be wrong with your code, but since you've given us absolutely no information about what it's meant to do, we certainly can't tell you how to fix it.
A = @(f,x) 2.*f(i,1).*(x(i,:)).^2
The anonymous function above has two input variables, f and x. The body of the function uses a third variable i. From the name, it looks like it may be intended to be a variable index for the rows of f and x. That's not going to be the case. Two possible scenarios:
- i exists as a variable prior to the above line. In this case, the value of i is used for A and is a constant for the lifetime of A. Modifying i after A has been created will not affect its value in A
- i doesn't exist when A is created. In this case, i is undefined within the context of the function and evaluating A will result in an obscure error (Index in position 1 is invalid. Array indices must be positive integers or logical values.) which is a bit misleading.
Perhaps, i is supposed to be an input of the anonymous function as well as f and x.
Of course, the problem might be something else entirely (like your computer is not turned on), since you haven't told us anything about it.
댓글 수: 1
Stephen23
2019년 7월 30일
Satyajit Mahapatra's "Answer" moved here:
Thank you for your valuable time Guillaume. The above obfcn is an objective function that is to be optimized using ga. I need the main function to call the fittness function( that contains the objective function ) recursively so that I get the optimization results for different sets of parameters. I tried to call the objfcn for ith iteration using function handle in the above code. Certainly, it seems it is not the way. Is there any way or should I be more elaborative about my problem here?
Satyajit Mahapatra
2019년 7월 30일
댓글 수: 2
Guillaume
2019년 7월 30일
Well, that thread is a mess! In the future, please use Comment on this Answer to comment rather than Answer this question.
I'm glad your problem is solved, but I'll point out that the code in your initial question is nothing like the actual code above, so there was no chance of understanding the problem to start with.
Satyajit Mahapatra
2019년 7월 30일
카테고리
도움말 센터 및 File Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!