What is wrong with this code?
Mostrar comentarios más antiguos
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 comentario
KSSV
el 29 de Jul. de 2019
Show us the whole code and tell us your error.
Respuesta aceptada
Más respuestas (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 comentario
Stephen23
el 30 de Jul. de 2019
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
el 30 de Jul. de 2019
2 comentarios
Guillaume
el 30 de Jul. de 2019
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
el 30 de Jul. de 2019
Satyajit Mahapatra
el 30 de Jul. de 2019
0 votos
Categorías
Más información sobre Solver Outputs and Iterative Display en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!