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!