Minimize cost function (4-d integral)
Show older comments
I want to integrate this cost function symbolically in order to later find its minimum using fmincon(): 

where K:
and
,
depend on
,
,
,
,
,
, θ. However, the int() function fails to integrate. The example 4-D Integral of Sphere is not suitable, since there the integral is calculated numerically.
10 Comments
Dyuman Joshi
on 12 Jan 2024
Edited: Dyuman Joshi
on 12 Jan 2024
@Arina Pirogova, The images are not of clear resolution/quality than the ones I edited to add in the question description.
Add them as images rather than files.
Arina Pirogova
on 12 Jan 2024
Sam Chak
on 12 Jan 2024
@Arina Pirogova, Usually, we use fmincon numerically.
fun = @(x) (x - 1).^2;
x0 = 0;
x = fmincon(fun, x0, [], [])
But if your cost function is described symbolically, this happens:
syms x
f(x) = (x - 1)^2
x0 = 0;
x = fmincon(f, x0, [], [])
Arina Pirogova
on 12 Jan 2024
Edited: Arina Pirogova
on 12 Jan 2024
Torsten
on 12 Jan 2024
But I don’t understand how to represent a cost function in order to minimize it.
Either by defining a function handle or a function with p = [psi1,psi2,phi1,phi2] as input vector from which you deduce the value of the cost function in this specific point.
Look at the examples provided for "fmincon", e.g.
Arina Pirogova
on 15 Jan 2024
Edited: Arina Pirogova
on 15 Jan 2024
@Arina Pirogova, I tried to fix what I can. If possible, put some annotations or comments.
%% Symbolic work section
syms t theta ddtheta ddx ddz psi1 psi2 phi1 phi2 g k mu
% ^ ^ ^ missing variables
% Some matrix A
A = [1 cos(phi1) cos(phi2);
0 sin(phi1) sin(phi2);
0 sin(psi1)/abs(sin(psi1+phi1)) sin(psi2)/abs(sin(psi2+phi2))];
% Some matrix theta (looks like a Rotation Matrix)
T_theta = [cos(theta) sin(theta) 0;
-sin(theta) cos(theta) 0;
0 0 1];
% Some matrix B
B = [ddx;
ddz+k*g;
mu*ddtheta];
% Maybe some state equations?
U = inv(A)*T_theta*B;
u0 = abs(U(1));
u1 = abs(U(2));
u2 = abs(U(3));
UU = simplify(u0 + u1 + u2);
%% Numerical work section
func = matlabFunction(UU, 'Vars', [theta, ddtheta, ddx, ddz, psi1, psi2, phi1, phi2, g, k, mu]);
% don't know what 'delta' is, but the anon function takes it.
I3 = @(theta) integral3(@(ddtheta, ddx, ddz) func(theta, ddtheta, ddx, ddz, psi1, psi2, phi1, phi2), -delta/mu, delta/mu, -delta, delta, -delta, delta)
% The cost function
f = @(psi1, psi2, phi1, phi2) integral(I3, -pi, pi);
psi1_0 = 3*pi/4; % for x_0
psi2_0 = 0; % for x_0
phi1_0 = 5*pi/12; % for x_0
phi2_0 = 7*pi/4; % for x_0
x_0 = [psi1_0,psi2_0,phi1_0,phi2_0]; % initial guess
lb = [0,pi,0,pi]; % lower bound
ub = [pi,2*pi,pi,2*pi]; % upper bound
%% call fmincon solver
[x, fval] = fmincon(f, x_0, [], [], [], [], lb, ub, @non_linear)
%% Nonlinear constraints:
function [c, ceq] = non_linear(x)
c = [-abs(tan(x(1) + x(3)) + 0.2); % <-- unsure if the bracket is placed correctly
-abs(tan(x(2) + x(4)) + 0.2)]; % <-- unsure if the bracket is placed correctly
ceq = [];
end
Arina Pirogova
on 15 Jan 2024
Edited: Arina Pirogova
on 15 Jan 2024
Check whether this is really what you want. The construction is quite complicated, and the evaluation of your function F takes a while.
%% Symbolic work section
syms theta ddtheta ddx ddz psi1 psi2 phi1 phi2
%Constants:
mu = 55;
delta = 1;
k = 0.142857;
g = 9.8;
% Some matrix A
A = [1 cos(phi1) cos(phi2);
0 sin(phi1) sin(phi2);
0 sin(psi1)/abs(sin(psi1+phi1)) sin(psi2)/abs(sin(psi2+phi2))];
% Some matrix theta (looks like a Rotation Matrix)
T_theta = [cos(theta) sin(theta) 0;
-sin(theta) cos(theta) 0;
0 0 1];
% Some matrix B
B = [ddx;
ddz+k*g;
mu*ddtheta];
% Maybe some state equations?
U = inv(A)*T_theta*B;
u0 = abs(U(1));
u1 = abs(U(2));
u2 = abs(U(3));
UU = simplify(u0 + u1 + u2);
%% Numerical work section
func = matlabFunction(UU, 'Vars', [theta, ddtheta, ddx, ddz, psi1, psi2, phi1, phi2]);
I3 = @(theta,psi1,psi2,phi1,phi2) integral3(@(ddtheta, ddx, ddz) func(theta, ddtheta, ddx, ddz, psi1, psi2, phi1, phi2), -delta/mu, delta/mu, -delta, delta, -delta, delta);
% The cost function
f = @(psi1, psi2, phi1, phi2) integral(@(theta)I3(theta,psi1,psi2,phi1,phi2), -pi, pi, 'ArrayValued',1);
F = @(x)f(x(1),x(2),x(3),x(4));
psi1_0 = 3*pi/4; % for x_0
psi2_0 = 0; % for x_0
phi1_0 = 5*pi/12; % for x_0
phi2_0 = 7*pi/4; % for x_0
x_0 = [psi1_0,psi2_0,phi1_0,phi2_0]; % initial guess
lb = [0,pi,0,pi]; % lower bound
ub = [pi,2*pi,pi,2*pi]; % upper bound
%% call fmincon solver
[x, fval] = fmincon(F, x_0, [], [], [], [], lb, ub, @non_linear)
%% Nonlinear constraints:
function [c, ceq] = non_linear(x)
c = [-abs(tan(x(1) + x(3))) + 0.2; % <-- unsure if the bracket is placed correctly
-abs(tan(x(2) + x(4))) + 0.2]; % <-- unsure if the bracket is placed correctly
ceq = [];
end
Answers (0)
Categories
Find more on Conversion Between Symbolic and Numeric in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!