How to correct the code?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
ancy s george
el 16 de Mayo de 2022
Respondida: Jan
el 16 de Mayo de 2022
I have to maximize the objective function with respect to x1 and x2.
func = -(x2*(-10)-x1*70+3*(80))
%Constrains are
3<=x1 ; x2<=8
This optimization has to be done in penality function method.
But my code has not working.
clc
clear
format long
syms x1 x2;
% objective function
func = -(x2*(-10)-x1*70+3*(80));
%Constrains
g1(1) = 3-x1;
g1(2) = x2-8;
eps = 0.001;
%initialize the convergance criteria
conv = 1;
% Initial Guess
i = 1;
x_1(i) = 3;
x_2(i) = 8;
rk(1) =0.001;
% addition of penality factor
f = func - rk(1)*(g1(1)+g1(2))
k = 1;
while conv > eps
Grad_f = gradient(f);
S = -subs(Grad_f,[x1,x2],[x_1(i),x_2(i)])
while norm(S)> eps
%calculate the step length
syms lambda
func_lambda = subs(f, [x1,x2], [x_1(i)+S(1)*lambda,x_2(i)+lambda*S(2)])
dfunc_lambda = (diff(func_lambda,lambda))
lambda = vpa(solve(dfunc_lambda==0,lambda),6);
lambda = lambda(imag(lambda)==0)
for k = 1:size(lambda)
fun_lambda_value(k) = subs(f,[x1,x2],[x_1(i)+lambda(k,1)*S(1),x_2(i)+lambda(k,1)*S(2)])
end
[value, index] = min(fun_lambda_value)
%compute the step length
lambda = lambda(index)
%replace the old value with new value for unconstrained
x_1(i+1) = x_1(i)+lambda*S(1)
x_2(i+1) = x_2(i)+lambda*S(2)
Grad_old = subs(Grad_f,[x1,x2],[x_1(i),x_2(i)])
i = i+1
Grad_new = subs(Grad_f,[x1,x2],[x_1(i),x_2(i)])
%update the search direction
S = -(Grad_new)+((norm(Grad_new))^2/(norm(Grad_old))^2)*S
end
Phi1 = subs(func,[x1,x2],[x_1(i-1),x_2(i-1)])
Phi2 = subs(func,[x1,x2],[x_1(i),x_2(i)])
conv = abs(abs(Phi2)-abs(Phi1))/abs(Phi1)
rk(k+1) = 0.01*rk(k)
k= k+1
end
Iter = 1:i;
K = 1:k
X_coordinate = x_1'
Y_coordinate = x_2'
Iterations = Iter'
Rk = rk'
for i=1:length(X_coordinate)
Objective_value(i) = double(subs(f,[x1,x2], [x_1(i),x_2(i)]))
end
Objective_value = Objective_value'
T = table(Iterations,X_coordinate,Y_coordinate, Objective_value)
I can't understand this error.How to rectify it?How to change the lamba as a empty system?
note: In function, -10,70,80 are changeable values.
0 comentarios
Respuesta aceptada
Jan
el 16 de Mayo de 2022
for k = 1:size(lambda)
fun_lambda_value(k) = subs(f,[x1,x2],[x_1(i)+lambda(k,1)*S(1),x_2(i)+lambda(k,1)*S(2)])
end
If lamda is a [1 x N] row vector, "1:size(lambda)" means "1:[1,N]". The colon operator uses the first element only, if an argument is a vector.
You want instead:
for k = 1:numel(lambda)
But in your case fun_lambda_value is not defined at all. Then lambda is empty and
[value, index] = min(fun_lambda_value)
cannot work.
I assume, the code has another bug: If lambda is smaller than in the former iteration, only the first elements are overwritten. I assume you want to set fun_lambda_value to a default value before the loop.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Get Started with Optimization Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!