Error using fmincon and integral2: taking integral variables theta and p as an array while performing computation

1 次查看(过去 30 天)
I am minimizing q for whole range of p and theta; p has limit from 2pi to 20pi and theta from 0 to pi; to find the value of d, r_g,G_g. q is function of theta, p,d, r_g, G_g.
But while computation p and theta are behaving as an array. Because of this I am getting error. I tried using "ArrayValued", true. This is also not working. How to correct this. Here my part of code
[q] = optimizeParameters();% Call optimizeParameters to define the objective function q
options = optimset('PlotFcns', @optimplotfval);% Set optimization options
d0 = [-0.5, 24, 3e6];
lb = [-1, 24, 2e6];
ub = [0, 32, 6e6];
[solution,fval] = fmincon(@(x)integral2(@(theta,p)q(theta,p,x(1),x(2),x(3)),pi/36,pi/6,2*pi,20*pi),d0,[],[],[],[],lb,ub,[],options); % minimize
function q = optimizeParameters()
Vs = 250;
k = @(p)p/Vs;
c = @(theta)Vs./sin(theta);
w5 = 0.001;
n = 3;
dl = -6;
d1 = @(d)dl + 2 * n * d - d;
q = @(theta, p, d, r_g, G_g) calculateObjective(theta, p, d, r_g, G_g, Vs, k, c, w5, n, dl, d1);
end
function answer = calculateObjective(theta, p, d, r_g, G_g, Vs, k, c, w5, n, dl, d1);
% printing the p and thetahere which is giving an array because of this error in further computation
theta
p
% rest of the code
end
  2 个评论
Steven Lord
Steven Lord 2024-5-2
What does "is also not working" mean in this context?
  • Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
  • Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
  • Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.
Manoj Manoj
Manoj Manoj 2024-5-2
This is the error I am getting. The error is due to matrix size not compatible for multiplication. But the matrix are x2_inv and B are 4x4 matrices. Since I am using k and c as function handles it shows the size of these matrices as 56x56.
This is not working contex: I tried using "ArrayValued",true; which we use when using integral and in integral2 it is not a valid option.
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on
each element of the matrix individually, use TIMES (.*) for elementwise multiplication.
Error in untitled5>calculateObjective (line 190)
Y2= x2_inv*(B*[0;0;-w5;w5]);
Error in untitled5>@(theta,p,d,r_g,G_g)calculateObjective(theta,p,d,r_g,G_g,Vs,k,c,w5,n,dl,d1) (line 35)
q = @(theta, p, d, r_g, G_g) calculateObjective(theta, p, d, r_g, G_g, Vs, k, c, w5, n, dl, d1);
Error in untitled5>@(theta,p)q(theta,p,x(1),x(2),x(3)) (line 11)
[solution,fval] = fmincon(@(x)integral2(@(theta,p)q(theta,p,x(1),x(2),x(3)),pi/36,pi/6,2*pi,20*pi,'Method','tiled'),d0,[],[],[],[],lb,ub,[],options); % minimize
Error in integral2Calc>integral2t/tensor (line 228)
Z = FUN(X,Y); NFE = NFE + 1;
Error in integral2Calc>integral2t (line 55)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 9)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 105)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
Error in untitled5>@(x)integral2(@(theta,p)q(theta,p,x(1),x(2),x(3)),pi/36,pi/6,2*pi,20*pi,'Method','tiled') (line 11)
[solution,fval] = fmincon(@(x)integral2(@(theta,p)q(theta,p,x(1),x(2),x(3)),pi/36,pi/6,2*pi,20*pi,'Method','tiled'),d0,[],[],[],[],lb,ub,[],options); % minimize
Error in fmincon (line 568)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in untitled5 (line 11)
[solution,fval] = fmincon(@(x)integral2(@(theta,p)q(theta,p,x(1),x(2),x(3)),pi/36,pi/6,2*pi,20*pi,'Method','tiled'),d0,[],[],[],[],lb,ub,[],options); % minimize
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.

请先登录,再进行评论。

采纳的回答

Torsten
Torsten 2024-5-2
编辑:Torsten 2024-5-2
Take care that you pass k, c and d1 as values, not as function handles to your objective function.
Thus (as in all your questions before) use
q = @(theta, p, d, r_g, G_g) calculateObjective(theta, p, d, r_g, G_g, Vs, k(p), c(theta), w5, n, dl, d1(d));
instead of
q = @(theta, p, d, r_g, G_g) calculateObjective(theta, p, d, r_g, G_g, Vs, k, c, w5, n, dl, d1);
!!!
[q] = optimizeParameters();% Call optimizeParameters to define the objective function q
options = optimset('PlotFcns', @optimplotfval);% Set optimization options
d0 = [-0.5, 24, 3e6];
lb = [-1, 24, 2e6];
ub = [0, 32, 6e6];
[solution,fval] = fmincon(@(x)integral2(@(theta,p)arrayfun(@(theta,p)q(theta,p,x(1),x(2),x(3)),theta,p),pi/36,pi/6,2*pi,20*pi),d0,[],[],[],[],lb,ub,[],options) % minimize
function q = optimizeParameters()
Vs = 250;
k = @(p)p/Vs;
c = @(theta)Vs./sin(theta);
w5 = 0.001;
n = 3;
dl = -6;
d1 = @(d)dl + 2 * n * d - d;
q = @(theta, p, d, r_g, G_g) calculateObjective(theta, p, d, r_g, G_g, Vs, k(p), c(theta), w5, n, dl, d1(d));
end
function answer = calculateObjective(theta, p, d, r_g, G_g, Vs, k, c, w5, n, dl, d1);
% printing the p and thetahere which is giving an array because of this error in further computation
answer = theta + p;
% rest of the code
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by