lsqcurvefit add a constraint

18 visualizaciones (últimos 30 días)
Ehu Li
Ehu Li el 29 de Oct. de 2019
Comentada: Rena Berman el 12 de Dic. de 2019
i want to add my constraint, but i doesnt work. how can i put this x0(1).^2+x0(2).^2<RI^2 into my function??
And there is also a error. I cant use the fincom, cause my result is a vector. Someone can fix this two problems? Thank you so much!!
clear all; close all; clc
%%%%%%%%%%%%%%%%%%%%%%Define simulation parameters%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% My = magnetic permeability[H/m]
% Rho = Gain factor
% phi = phase delay [°]
% Theta Sensing directional angle
% r,Theta = Position and angular distance between conductors and sensors
% xs,ys = Coordinate of the sensors
% xi,yi = Coordinate of conductor to be measured
% I = Current of the Cables [A]
% RI = Cable Radius in m
global My Rho phi Theta RI B V Vm xs ys
My = 4*pi*10^(-7);
Rho = 6.824*10000;
phi = 13.61;
Theta = 0.01;
RI = 21*(1e-3); % in[m]
%------------Calculation of voltage by magnetic flux B in [T]-------------%
%-------------------------------------------------------------------------%
% Define the magetic fields in [T] of sensors
B = [0.17, 0.47, 35.30, 35.32, 6.66, 6.59, 36.28, 35.43, 13.36, 15.04, 42.17, 44.05];
B = B*1e-6;
B
% calculate the output voltage of sensors in [V]
V = (Rho*exp(1i*phi*pi/180))*B;
V
% % %---------------Define the measured voltage of sensors--------------------%
Vm = [0.0152, 0.03, 2.942, 2.943, 0.5751, 0.6, 3.0238, 2.9529, 1.1235, 1.2535, 3.714, 3.6713];
% Define the positions of each sensor in [m]
xs = [21;-21;0;0;-14.5;-15;16;15.3;-8.800;-7.900;10.3;9.4];
ys = [0;0;21;-21;14.5;13.8;-12.9;13.9;18.3;-18.8;-17.9;18];
xs = xs.*1e-3;
ys = ys.*1e-3;
%--------------all x-and y-coordinates value of the sensors---------------%
xdata = xs;
xdata(:,2) = ys;
xdata
%---create the initial guess of x-and y-coordinates value of the cables---%
%--------create the initial guess of current value of the cables----------%
%-------------the field x consists of this 3 parameters-------------------%
x0 = [-6.3;-6.3;-6.3]*1e-4;
x0(:,2) = [-6.3;-6.3;-6.3]*1e-4;
x0(:,3) = [-4;-4;-4];
x0
%---------create the Lower(lb) and Upper(ub) bounds of fields x----------%
lb = [-21*1e-4,-21*1e-4,-5];
ub = [21*1e-4,21*1e-4,5];
options=(x0(1).^2+x0(2).^2<RI^2);
%---------solve the NNLS function to find out the fields of x-------------%
[x,resnorm,residual] = lsqcurvefit(@NNLS,x0,xdata,V,lb,ub,options)
function NNLS1 = NNLS(x,xdata)
% x - parameter: positions of the conductors & the current in the conductors
% xdata - array of positions of the sensors
%
% NNLS - the calculated voltage at each position of each sensor
global My Rho phi Theta
% Create the number of x m and the xdata n
for n=1:1:12
for m=1:1:3
% calculate the distance between the sensor and conductor
dis_array =sqrt((xdata(n,1)).^2+(xdata(n,2)).^2);
% calculate cos angular distance between conductor and sensor
cos_delta_a = (xdata(n,1).*cos(Theta)-xdata(n,2).*sin(Theta)).*((xdata(n,1)-x(m,1))+(xdata(n,2).*cos(Theta)+xdata(n,1).*sin(Theta))).*(xdata(n,2)-x(m,2));
cos_delta_b = (xdata(n,1)-x(m,1)).^2+(xdata(n,2)-x(m,2)).^2;
a = My*Rho*exp(1i*phi*pi/180)*x(m,3);
% calculate the Output-voltage
b(m,n) = (a.*(cos_delta_a)./(2*pi*(dis_array).*cos_delta_b));
end
if n == 12
NNLS1(:,:) = sum(b);
end
end
end
error: Local minimum possible.
lsqcurvefit stopped because the size of the current step is less than
the value of the step size tolerance.
<stopping criteria details>
  3 comentarios
Stephen23
Stephen23 el 6 de Nov. de 2019
Editada: Stephen23 el 6 de Nov. de 2019
Original question (from Google Cache):
i want to add my constraint, but i doesnt work. how can i put this x0(1).^2+x0(2).^2<RI^2 into my function??
And there is also a error. I cant use the fincom, cause my result is a vector. Someone can fix this two problems? Thank you so much!!
clear all; close all; clc
%%%%%%%%%%%%%%%%%%%%%%Define simulation parameters%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% My = magnetic permeability[H/m]
% Rho = Gain factor
% phi = phase delay [°]
% Theta Sensing directional angle
% r,Theta = Position and angular distance between conductors and sensors
% xs,ys = Coordinate of the sensors
% xi,yi = Coordinate of conductor to be measured
% I = Current of the Cables [A]
% RI = Cable Radius in m
global My Rho phi Theta RI B V Vm xs ys
My = 4*pi*10^(-7);
Rho = 6.824*10000;
phi = 13.61;
Theta = 0.01;
RI = 21*(1e-3); % in[m]
%------------Calculation of voltage by magnetic flux B in [T]-------------%
%-------------------------------------------------------------------------%
% Define the magetic fields in [T] of sensors
B = [0.17, 0.47, 35.30, 35.32, 6.66, 6.59, 36.28, 35.43, 13.36, 15.04, 42.17, 44.05];
B = B*1e-6;
B
% calculate the output voltage of sensors in [V]
V = (Rho*exp(1i*phi*pi/180))*B;
V
% % %---------------Define the measured voltage of sensors--------------------%
Vm = [0.0152, 0.03, 2.942, 2.943, 0.5751, 0.6, 3.0238, 2.9529, 1.1235, 1.2535, 3.714, 3.6713];
% Define the positions of each sensor in [m]
xs = [21;-21;0;0;-14.5;-15;16;15.3;-8.800;-7.900;10.3;9.4];
ys = [0;0;21;-21;14.5;13.8;-12.9;13.9;18.3;-18.8;-17.9;18];
xs = xs.*1e-3;
ys = ys.*1e-3;
%--------------all x-and y-coordinates value of the sensors---------------%
xdata = xs;
xdata(:,2) = ys;
xdata
%---create the initial guess of x-and y-coordinates value of the cables---%
%--------create the initial guess of current value of the cables----------%
%-------------the field x consists of this 3 parameters-------------------%
x0 = [-6.3;-6.3;-6.3]*1e-4;
x0(:,2) = [-6.3;-6.3;-6.3]*1e-4;
x0(:,3) = [-4;-4;-4];
x0
%---------create the Lower(lb) and Upper(ub) bounds of fields x----------%
lb = [-21*1e-4,-21*1e-4,-5];
ub = [21*1e-4,21*1e-4,5];
options=(x0(1).^2+x0(2).^2<RI^2);
%---------solve the NNLS function to find out the fields of x-------------%
[x,resnorm,residual] = lsqcurvefit(@NNLS,x0,xdata,V,lb,ub,options)
function NNLS1 = NNLS(x,xdata)
% x - parameter: positions of the conductors & the current in the conductors
% xdata - array of positions of the sensors
%
% NNLS - the calculated voltage at each position of each sensor
global My Rho phi Theta
% Create the number of x m and the xdata n
for n=1:1:12
for m=1:1:3
% calculate the distance between the sensor and conductor
dis_array =sqrt((xdata(n,1)).^2+(xdata(n,2)).^2);
% calculate cos angular distance between conductor and sensor
cos_delta_a = (xdata(n,1).*cos(Theta)-xdata(n,2).*sin(Theta)).*((xdata(n,1)-x(m,1))+(xdata(n,2).*cos(Theta)+xdata(n,1).*sin(Theta))).*(xdata(n,2)-x(m,2));
cos_delta_b = (xdata(n,1)-x(m,1)).^2+(xdata(n,2)-x(m,2)).^2;
a = My*Rho*exp(1i*phi*pi/180)*x(m,3);
% calculate the Output-voltage
b(m,n) = (a.*(cos_delta_a)./(2*pi*(dis_array).*cos_delta_b));
end
if n == 12
NNLS1(:,:) = sum(b);
end
end
end
error: Local minimum possible.
lsqcurvefit stopped because the size of the current step is less than
the value of the step size tolerance.
<stopping criteria details>
Rena Berman
Rena Berman el 12 de Dic. de 2019
(Answers Dev) Restored edit

Iniciar sesión para comentar.

Respuestas (1)

Matt J
Matt J el 29 de Oct. de 2019
No, you cannot supply any constraints except simple bounds with lsqcurvefit. You do need to use fmincon. Write an objective function that returns the scalar quantity that you are trying to minimize. If you are trying to do a least squares fit to your model function F(x,xdata), then this should be
objective=@(x) norm(F(x,xdata)-ydata)^2;

Categorías

Más información sobre Solver Outputs and Iterative Display 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!

Translated by