MATLAB Answers

fmincon to get 3 unknown parameters

2 ビュー (過去 30 日間)
Ehu Li
Ehu Li 2019 年 10 月 28 日
Commented: Rena Berman 2019 年 12 月 12 日
Hey guys, i have no idea what is wrong. Who can helps me pls?
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 xdata
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(:,3) =V;
%---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-3;
x0(:,2) = [-6.3;-6.3;-6.3].*1e-3;
x0(:,3) = [0;0;0];
x0
%---------create the Lower(lb) and Upper(ub) bounds of fields x----------%
A = [];
b= [];
Aeq=[];
beq=[];
lb = [-21*1e-4,-21*1e-4,-5];
ub = [21*1e-4,21*1e-4,5];
options=optimset('MaxFunEvals',Inf);
%---------solve the NNLS function to find out the fields of x-------------%
[x] = fmincon(@(x)Fmin_fun(x),x0,A,b,Aeq,beq,lb,ub,@(x)unitdisk(x),options)
function fmin_fun = Fmin_fun(x)
% 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 xdata
% 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
xdata(:,3) = (xdata(:,3)');
fmin_fun(:,:) = sum((xdata(:,3)-sum(b)).^2);
end
end
end
function [c,ceq] = unitdisk(x)
c = x(:,1).^2+x(:,2).^2-RI^2;
ceq = [];
end
error:Error using fmincon (line 625)
Supplied objective function must return a scalar value.

  2 件のコメント

Stephen Cobeldick
Stephen Cobeldick 2019 年 11 月 6 日
Original question (from Google Cache):
Hey guys, i have no idea what is wrong. Who can helps me pls?
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 xdata
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(:,3) =V;
%---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-3;
x0(:,2) = [-6.3;-6.3;-6.3].*1e-3;
x0(:,3) = [0;0;0];
x0
%---------create the Lower(lb) and Upper(ub) bounds of fields x----------%
A = [];
b= [];
Aeq=[];
beq=[];
lb = [-21*1e-4,-21*1e-4,-5];
ub = [21*1e-4,21*1e-4,5];
options=optimset('MaxFunEvals',Inf);
%---------solve the NNLS function to find out the fields of x-------------%
[x] = fmincon(@(x)Fmin_fun(x),x0,A,b,Aeq,beq,lb,ub,@(x)unitdisk(x),options)
function fmin_fun = Fmin_fun(x)
% 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 xdata
% 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
xdata(:,3) = (xdata(:,3)');
fmin_fun(:,:) = sum((xdata(:,3)-sum(b)).^2);
end
end
end
function [c,ceq] = unitdisk(x)
c = x(:,1).^2+x(:,2).^2-RI^2;
ceq = [];
end
error:Error using fmincon (line 625)
Supplied objective function must return a scalar value.
Rena Berman
Rena Berman 2019 年 12 月 12 日
(Answers Dev) Restored edit

サインイン to comment.

件の回答 (1)

Matt J
Matt J 2019 年 10 月 28 日
As the error message says, your objective function must return a scalar value and does not. It returns a length 12 vector. The value returned is also complex-valued, which is another problem you will want to address.
K>> Fmin_fun(x0)
ans =
Columns 1 through 5
38.4142 +19.7592i 38.4142 +19.7592i 38.4142 +19.7592i 38.4142 +19.7592i 38.4142 +19.7592i
Columns 6 through 10
38.4142 +19.7592i 38.4142 +19.7592i 38.4142 +19.7592i 38.4142 +19.7592i 38.4142 +19.7592i
Columns 11 through 12
38.4142 +19.7592i 38.4142 +19.7592i

  4 件のコメント

表示 1 件の古いコメント
Matt J
Matt J 2019 年 10 月 28 日
No, you and only you know what your objective function is supposed to compute. The only thing I know is that it is not supposed to be a vector.
My recommendation, though, is that you first get your objective function to work before trying to run it through fmincon.
Ehu Li
Ehu Li 2019 年 10 月 28 日
But i don’t understand with scalar. It means it can be only a value? But if I have more than one value which Funktion I can use? Cause I try to use lsqcurvefit. But there are some problems with constraints. LoL. Do u have an idea?
Matt J
Matt J 2019 年 10 月 28 日
For fmincon, the scalar returned by your function should be the quantity you are trying to minimize. Note that this is different from lsqcurvefit, where you are providing a function that returns a vector of samples of a curve.

サインイン to comment.

サインイン してこの質問に回答します。

タグ


Translated by