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
KSSV 2019년 7월 29일
Show us the whole code and tell us your error.

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2019년 7월 30일

1 개 추천

%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

추가 답변 (3개)

Guillaume
Guillaume 2019년 7월 29일
편집: Guillaume 2019년 7월 29일

2 개 추천

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
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
Satyajit Mahapatra 2019년 7월 30일

0 개 추천

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

Guillaume
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
Satyajit Mahapatra 2019년 7월 30일
Ok, Thank you. Your initial analysis helped me.

댓글을 달려면 로그인하십시오.

Satyajit Mahapatra
Satyajit Mahapatra 2019년 7월 30일

0 개 추천

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