Borrar filtros
Borrar filtros

error with optimisation

2 visualizaciones (últimos 30 días)
Villanova
Villanova el 20 de Abr. de 2012
Hi, I was looking to do optimization for this http://4.bp.blogspot.com/-WSb8BrAmc9w/T5Hy2QaXJqI/AAAAAAAAAac/ZwDB5y19jAs/s1600/toric_optimization.jpg shape. I was wondering if someone could kindly point out my errors in this code:
% objective function for optimization
function F = toric_opt_obj(XX)
global alpha11 alpha12 alpha21 alpha22 alpha0
global icount
alpha11 = XX(1);
alpha12 = XX(2);
alpha21 = XX(3);
alpha22 = XX(4);
alpha0 = XX(5);
icount = icount + 1;
toric_obstacle;
% (x_1^2+x_2^2)^2+alpha12*x_1^2+alpha22*x_2^2+alpha11*x_1+alpha21*x_2+alpha0=0
toric =(z1.^2+z2.^2).^2+alpha12.*z1.^2+alpha22.*z2.^2+alpha11.*z1+alpha21.*z2+alpha0;
L1 = length(z1);
L2 = length(z2);
if z2 == -1*ones(1,L2)
Err_1 = toric - z1;
end
if (z2 == .8*ones(1,L2) && (-1.2*ones(1,L1) <z1< -.5*ones(1,L1)))
Err_2 = toric - z2;
end
if z2 == .5*ones(1,L2)
Err_3 = toric - z1;
end
if (z2 == .8*ones(1,L2) && (.6*ones(1,L1) <z1< 1.2*ones(1,L1)))
Err_4 = toric - z2;
end
Err = Err_1^2 + Err_2^2 + Err_3^2 + Err_4^2;
% Err = (z1.^2+z2.^2).^2+alpha12*z1.^2+alpha22*z2.^2+alpha11*z1+alpha21*z2+alpha0;
F = norm(Err); %error square root of the sum of the error squared
[icount XX F]
this part of code is right which is
% Minimum error - toric section
clear all; clc;
global alpha11 alpha12 alpha21 alpha22 alpha0
alpha11 = 0;
alpha12 = -2;
alpha21 = .2;
alpha22 = -1.5;
alpha0 = .5;
% initial limity cycle shape
ii0 = 0;
for x10=-1.5:.01:1.5
x20 = roots([1 0 (2*x10^2+alpha22) alpha21 (alpha0+alpha11*x10+alpha12*x10^2+x10^4)]);
for ii=1:4
if isreal(x20(ii))
ii0 = ii0 + 1;
x1lci(ii0) = x10;
x2lci(ii0) = x20(ii);
end
end
end
XX0 = [alpha11 alpha12 alpha21 alpha22 alpha0];
lb = [-.1 -3 .01 -2 .3];
ub = [ .1 -1 .40 -1 .7];
optionso = optimset('LargeScale','off','MaxFunEvals',150);
%norm_tol = .01;
%icount = 0;
% [XX,fval,exitflag,output] = fmincon(@objfun,XX0,[],[],[],[],lb,ub,@confun,optionso)
[XX,fval,exitflag,output] = fminsearch(@toric_opt_obj,XX0)
alpha11 = XX(1);
alpha12 = XX(2);
alpha21 = XX(3);
alpha22 = XX(4);
alpha0 = XX(5);
%alpha22=-2.124;
% optimal limity cycle shape
ii0 = 0;
for x10=-1.5:.01:1.5
x20 = roots([1 0 (2*x10^2+alpha22) alpha21 (alpha0+alpha11*x10+alpha12*x10^2+x10^4)]);
for ii=1:4
if isreal(x20(ii))
ii0 = ii0 + 1;
x1lco(ii0) = x10;
x2lco(ii0) = x20(ii);
end
end
end
toric_obstacle;
figure (1)
plot(z1,z2,'k-',x1lci,x2lci,'b*',x1lco,x2lco,'ro','LineWidth',2);
legend('obstacle','initial','optimal');
and this is my obstacle m-file:
% obstacle definition
x0 = -1.2;
y0 = -1;
x1 = -0.6;
y1 = -0.5;
x2 = 0.6;
x3 = 1.2;
y3 = 0.8;
delta = 0.01;
%line 1
i = 0;
for x = x0:delta:x3
i = i + 1;
z1(i) = x;
z2(i) = y0;
end
%line 2
i = i - 1;
for y = y0:delta:y3
i = i + 1;
z1(i) = x3;
z2(i) = y;
end
%line 3
i = i - 1;
for x = x3:-delta:x2
i = i + 1;
z1(i) = x;
z2(i) = y3;
end
%line 4
i = i - 1;
for y = y3:-delta:y1
i = i + 1;
z1(i) = x2;
z2(i) = y;
end
%line 5
i = i - 1;
for x = x2:-delta:x1
i = i + 1;
z1(i) = x;
z2(i) = y1;
end
%line 6
i = i - 1;
for y = y1:delta:y3
i = i + 1;
z1(i) = x1;
z2(i) = y;
end
%line 7
i = i - 1;
for x = x1:-delta:x0
i = i + 1;
z1(i) = x;
z2(i) = y3;
end
%line 8
i = i - 1;
for y = y3:-delta:y0
i = i + 1;
z1(i) = x0;
z2(i) = y;
end
i = i - 1;
z1 = z1(1:i);
z2 = z2(1:i);
  4 comentarios
Walter Roberson
Walter Roberson el 21 de Abr. de 2012
So the obstacle shown in the image should be treated as not being there?
Anyhow, you are going to have to indicate what the error you observe _is_ . Error message, traceback ?
Villanova
Villanova el 24 de Abr. de 2012
I get this error message:
??? Operands to the || and && operators must be convertible to logical
scalar values.
Error in ==> toric_opt_obj at 24
if (z2== .8*ones(1,L2) && (-1.2*ones(1,L1) <z1< -.5*ones(1,L1)))
Error in ==> fminsearch at 205
fv(:,1) = funfcn(x,varargin{:});

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 24 de Abr. de 2012
Use & instead of && .
Read the documentation for all() and any()
a < b < c is interpreted as (a < b) < c . The first part produces a logical value, 0 or 1, and it is that logical value that is compared to c. Consider using (a < b & b < c)

Más respuestas (2)

Villanova
Villanova el 26 de Abr. de 2012
I also have this error :(
Undefined function or variable "Err_1".
Error in ==> toric_opt_obj at 36
Err = Err_1^2 + Err_2^2 + Err_3^2 + Err_4^2;
  3 comentarios
Villanova
Villanova el 26 de Abr. de 2012
yes i did change the errors by && in the if statements.
Walter Roberson
Walter Roberson el 26 de Abr. de 2012
Please show the current code for the "if" lines, including the current version of
if z2 == -1*ones(1,L2)
Please also show L2, and size(z2)

Iniciar sesión para comentar.


Villanova
Villanova el 26 de Abr. de 2012
hi the if was ok i believe. the only thing wrong seemed to be &&. So I didn't change much: this is the new one:
function F = toric_opt_obj(XX)
global alpha11 alpha12 alpha21 alpha22 alpha0
global icount
alpha11 = XX(1);
alpha12 = XX(2);
alpha21 = XX(3);
alpha22 = XX(4);
alpha0 = XX(5);
icount = icount + 1;
toric_obstacle;
% (x_1^2+x_2^2)^2+alpha12*x_1^2+alpha22*x_2^2+alpha11*x_1+alpha21*x_2+alpha0=0
toric =(z1.^2+z2.^2).^2+alpha12.*z1.^2+alpha22.*z2.^2+alpha11.*z1+alpha21.*z2+alpha0;
L1 = length(z1);
L2 = length(z2);
if z2 == -1*ones(1,L2)
Err_1 = toric - z1;
end
if (z2 == .8*ones(1,L2) & (-1.2*ones(1,L1) <z1< -.5*ones(1,L1)))
Err_2 = toric - z2;
end
if z2 == .5*ones(1,L2)
Err_3 = toric - z1;
end
if (z2 == .8*ones(1,L2) & (.6*ones(1,L1) <z1< 1.2*ones(1,L1)))
Err_4 = toric - z2;
end
Err = Err_1^2 + Err_2^2 + Err_3^2 + Err_4^2;
% Err = (z1.^2+z2.^2).^2+alpha12*z1.^2+alpha22*z2.^2+alpha11*z1+alpha21*z2+alpha0;
F = norm(Err); %error square root of the sum of the error squared
[icount XX F]

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by