Error: using fmincon with integral function

조회 수: 5 (최근 30일)
Manoj Manoj
Manoj Manoj 2023년 11월 13일
답변: Walter Roberson 2023년 11월 13일
I am using integral and fmincon simultaneously but I am getting an error. I want to integrate magnitude wrt p and then optimize the objective function wrt to d.
%% Input
syms d p
G1 = 3e6
G1 = 3000000
G0 = 1.25e8;
V0 = 250;
k0 = p/V0;
V1 = 316;
k1 = p/V1;
%% Calculation
D = [cos(k1*d) sin(k1*d)*p/(G1*k1)*1i; sin(k1*d)*(G1*k1)/p*1i cos(k1*d)];
L = [2*p/k0*cos(5*k0) sin(5*k0)*2*p/k0*1i; sin(5*k0)*2*G0*1i 2*G0*cos(5*k0)];
B = D*L;
B1 = B(1,1);
B2 = B(1,2);
B3 = B(2,1);
B4 = B(2,2);
Answer = (2*p*(B4-B3)-2*k0*G0*(B2-B1))/(k0*(B1+B2)*(B4-B3)-k0*(B4+B3)*(B2-B1));
magnitude = abs(Answer);
I = matlabFunction(amgnitude,"Vars",[p d]);
Unrecognized function or variable 'amgnitude'.
options = optimset('PlotFcns',@optimplotfval);
d0 = 0.5;
A = [];
b = [];
Aeq = [];
beq = [];
lb = 0;
ub = Inf;
nonlcon = [];
[s,fval] = fmincon(integral(I,0,10),d0,A,b,Aeq,beq,lb,ub,nonlcon,options); %minimize

채택된 답변

Walter Roberson
Walter Roberson 2023년 11월 13일
%% Input
syms d p
G1 = 3e6
G1 = 3000000
G0 = 1.25e8;
V0 = 250;
k0 = p/V0;
V1 = 316;
k1 = p/V1;
%% Calculation
D = [cos(k1*d) sin(k1*d)*p/(G1*k1)*1i; sin(k1*d)*(G1*k1)/p*1i cos(k1*d)];
L = [2*p/k0*cos(5*k0) sin(5*k0)*2*p/k0*1i; sin(5*k0)*2*G0*1i 2*G0*cos(5*k0)];
B = D*L;
B1 = B(1,1);
B2 = B(1,2);
B3 = B(2,1);
B4 = B(2,2);
Answer = (2*p*(B4-B3)-2*k0*G0*(B2-B1))/(k0*(B1+B2)*(B4-B3)-k0*(B4+B3)*(B2-B1));
magnitude = abs(Answer);
I = matlabFunction(magnitude,"Vars",[p d]);
options = optimset('PlotFcns',@optimplotfval);
d0 = 0.5;
A = [];
b = [];
Aeq = [];
beq = [];
lb = 0;
ub = Inf;
nonlcon = [];
[s,fval] = fmincon(@(d)integral(@(p)I(p,d),0,10),d0,A,b,Aeq,beq,lb,ub,nonlcon,options); %minimize
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by