# How to optimise a definite integral?

29 views (last 30 days)
Harvey Rael on 27 May 2018
Edited: DGM on 9 May 2021
In the attached image, I want to optimize the function Dt, as I know the end result of the integral. I have expressions for k1 and k0 in terms of k2 and N, and it is k2 and N that I would like to optimize. They have constraints, needing to be between certain values. I have it all setup in my code, but I am just unaware of how to tell the genetic alogrithm to optimize an integral function? Is there something I'm missing here?
EDIT: Usually, the integration is completed numerically on MATLAB, I don't actually know what the analytical result is.

sloppydisk on 27 May 2018
Edited: sloppydisk on 27 May 2018
You don't need to have the analytical result, simply pass the integral as the objective function with a function handle and k2 and N as the design variables. The genetic algorithm will use the outputs of the objective function to determine its minimum. If you need help with passing the function handle please post your code.
Harvey Rael on 28 May 2018
Hi!
So I have it all set up. I know that my delta t is three years, and so I have created a function y = integral - Dt, because I know my integral must = Dt, and I want to find the optimal values of the inputs when I do. below is the code, with k(1) and k(2) the variables to be optimized
function y = objective(k)
% Define constants
AU = astroConstants(2);
mu = astroConstants(4);
% Define start and finish parameters for the exponential sinusoid.
r1 = AU; % Initial radius
psi = pi/2; % Final polar angle of Mars/finish transfer
phi = pi/2;
r2 = 1.5*AU;
global k1
k1 = sqrt( ( (log(r1/r2) + sin(k(1)*(psi + 2*pi*k(2)))*tan(0)/k(1)) / (1-cos(k(1)*(psi+2*pi*k(2)))) )^2 + tan(0)^2/k(1)^2 );
k0 = r1/exp(k1*sin(phi));
R = k0*exp(k1*sin(k(1)*theta + phi));
theta_dot = sqrt((mu/(R^3))*1/((tan(0))^2 + k1*(k(1))^2*sin(k(1)*theta + phi) + 1));
z = 1/theta_dot;
y = int(z, theta, 0,(psi+2*pi*k(2))) - 94608000;
global x
x=y;
end
I also have a constraint function for the variables :
function [c,c_eq] = myconstraints(k)
global k1 x
c = [norm(k1*(k(1)^2))-1 -norm(k1*(k(1)^2))];
c_eq = [x];
end
I'm hoping that I've essentially told it that y MUST =0 thus optimising for that case.
However, when I run the genetic algorithm, the error message I am currently getting is an undefined variable (theta) in the objective function.. my issue is that the integral is being integrated from theta = 0 to theta = psi + 2*pi*k(2), with k(2) being one of the variables that need to be optimized. Here is the code for the genetic algorithm. Any chance of help?
clc; clear;
ObjFcn = @objective;
nvars = 2;
LB = [0 2];
UB = [1 7];
ConsFcn = @myconstraints;
[k,fval] = ga(ObjFcn,nvars,[],[],[],[],LB,UB,ConsFcn);

Torsten on 29 May 2018
Edited: Torsten on 29 May 2018
R = @(theta) k0*exp(k1*sin(k(1)*theta + phi));
theta_dot = @(theta) sqrt(mu./R(theta).^3./(k1*k(1)^2*sin(k(1)*theta + phi) + 1));
z = @(theta) 1./theta_dot(theta);
y = integral(z,0,(psi+2*pi*k(2))) - 94608000;
Best wishes
Torsten.
Harvey Rael on 29 May 2018
Hi Torsten!
Many thanks for your answer. It certainly got rid of those pesky and annoying error messages!
The current predicament I'm having is the ga is returning "Warning : Infinite or Not a Number value encountered." and "Optimization terminated : no feasible point found" - which under normal circumstances would make me question but the problem but I know there is a feasible point, mechanically I've found that there is at least a local minimum around the k values [1/5 2].
Moreover it is returning the fval as -6.7889e07-1.823e02i which I know is incorrect, the fval needs to be zero, which I thought I specified in my constraint function? (see above). I've obviously missed something critical in my thinking regarding this constraining. Any thoughts?
Best Regards, Harvey Rael
Harvey Rael on 29 May 2018
Hi Torsten,
I got the notification but you haven't changed anything?
Best Regards, Harvey

Kunwar Pal Singh on 8 May 2021
How to code this equation....could anyone help me ..
im getting errors in contraints
%LOGICAL Convert symbolic expression to logical array
% Y = LOGICAL(X) converts each element of the symbolic
% array X into the value true or false.
% Symbolic equations are converted to true
% only if the left and right sides are identically
% the same. Otherwise the equation is converted to
% false. Inequalities which cannot be proved will
% throw an error.
the equation is
DGM on 9 May 2021
Edited: DGM on 9 May 2021
Did you even read what sloppydisk posted?
This is not an answer to the original question. If you have a question, make a new post for it instead of putting it in some random place.
EDIT: Oh I see this isn't the first time. Walter has already given you an answer on this. If you have a question, ask a complete, descriptive question without concealing the sum of your efforts to date, and without dumping off-topic things where they don't belong. Doing this only makes it less likely that anyone will see your question, let alone understand it or answer it. Hiding questions and omitting information is contrary to the basic purpose of communication.
Kunwar Pal Singh on 9 May 2021
ok thanx

### Categories

Find more on Genetic Algorithm in Help Center and File Exchange

R2018a

### Community Treasure Hunt

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

Start Hunting!