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
KSSV el 29 de Jul. de 2019
Show us the whole code and tell us your error.

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 30 de Jul. de 2019

1 voto

%the below code can be simplified for the case where your x values are all sequential
%positive integers such as the 1:1:10 that you used. I used the more general form
%where the values might not be integers, so that you can explore on other ranges
%and other granularities.
F = 10;
nvars = 2;
Aeq(:,:) = [1 1];
Beq(:,:) = F;
LB =[ 0.1,0.1];
UB = [F,F];
xvals = 1:1:10;
numx = length(xvals);
x0=[10,10];
options = optimoptions(@ga,'UseVectorized',true);
fs = zeros(numx, nvars);
fvals = zeros(1, numx);
for xidx = 1 : numx
x = xvals(xidx);
rng(1,'twister'); % for reproducibility
[fs(xidx, :) fvals(xidx)] = ga(@(f) fitnesstrial(f,x);,nvars,[],[],[Aeq],[Beq],LB,UB,[]);
disp([x, fvals(xidx)])
end
plot(x, fvals);
function U=fitnesstrial(f, x)
k1=2.*f(1).*(x).^2;
k2=5.*f(2)*(x);
U=f(1).^2./(2*k1)+f(2).^2./(2*k2);
end

Más respuestas (3)

Guillaume
Guillaume el 29 de Jul. de 2019
Editada: Guillaume el 29 de Jul. de 2019

2 votos

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
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?

Iniciar sesión para comentar.

Satyajit Mahapatra
Satyajit Mahapatra el 30 de Jul. de 2019

0 votos

Also I am attaching the fitness function and main function for optimizing in ga. This is for one value of U. How can I get the results for each value of U.
The fitness function
function U=fitnesstrial(f)
x=1:1:10;
k1=2.*f(1).*(x).^2;
k2=5.*f(2)*(x);
U1=f(1).^2./(2*k1)+f(2).^2./(2*k2);
U=U1(10);
The main function
clear all;
close all;
clc
% f=zeros(10,2);
% f=zeros(10,2);
objfcn=@fitnesstrial;
% objfcn=@fitnesstrial;
F=10;
nvars=2;
Aeq(:,:)=[1 1];
Beq(:,:)=F;
LB=[0.1,0.1];
UB=[F,F];
% consfcn=@myconstraints;
rng(1,'twister'); % for reproducibility
x0=[10,10];
options = optimoptions(@ga,'UseVectorized',true);
[f fval]=ga(objfcn,nvars,[],[],[Aeq],[Beq],LB,UB,[]);
f

2 comentarios

Guillaume
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
Satyajit Mahapatra el 30 de Jul. de 2019
Ok, Thank you. Your initial analysis helped me.

Iniciar sesión para comentar.

Satyajit Mahapatra
Satyajit Mahapatra el 30 de Jul. de 2019

0 votos

Thanks for your help Walter Roberson

Community Treasure Hunt

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

Start Hunting!

Translated by