fmincon with handle function input
Afficher commentaires plus anciens
Hello, I have a question about the usage of the fmincon routine. I've read the documentation, and the call requires an explicit function with a certain number of variables and coefficients that determine equality and inequality constraints. I wanted to ask if, instead of an explicit function, it's possible to input a handle function, such as a numerical integrator (for example, one that varies initial conditions to minimize the integration output). In the documentation, the only example that shows the use of a handle function still includes an explicit function within the handle.
20 commentaires
Give us an example. It's not clear what you mean.
The functions in which you prescribe the expression you want to minimize and the nonlinear constraints can either be passed to fmincon as function handles (suited for short "one-liners") or functions (suited for difficult computations).
Hi @giacomo
If you want to minimize the norm of the states by varying the initial states, isn't it true that, logically, the initial states beginning from the equilibrium point will give the lowest possible norm value?
tspan = [0 10];
init = [0 0]; % initial states (equilibrium)
[t, x] = ode45(@odefcn, tspan, init);
nx = norm(x)
% system
function dxdt = odefcn(t, x)
A = [0 1; -1 -sqrt(3)];
dxdt = A*x;
end
Hynod
le 11 Sep 2023
Torsten
le 11 Sep 2023
p = fmincon(@fun,...)
function obj = fun(p)
[t,x] = ode113(@ODE_1_LanderOrbit,...)
...
obj = ...;
end
function dx = ODE_1_LanderOrbit(t,x)
...
end
Hynod
le 11 Sep 2023
Modifié(e) : Walter Roberson
le 12 Sep 2023
Sam Chak
le 12 Sep 2023
Out of curiosity, what does finding a set of initial states, S(0), that minimize the norm(S), physically mean for the lander? Understanding this is crucial in designing the objective function, which requires a blend of art and science—both inherent in human attempts to comprehend and describe the world around us.
From Wikipedia: A lander is a spacecraft that descends towards, then comes to rest on the surface of an astronomical body other than Earth.

Image: Chandrayaan-2 lander.
Hynod
le 12 Sep 2023
Hynod
le 12 Sep 2023
Sam Chak
le 12 Sep 2023
*The objective is to approach the planet thanks to the perturbation of the sun, to minimize the propellant which is necessary to let the orbit be appropriate to land.*
Based on the description, this sounds more like an orbit transfer problem than a lander descent problem. The spacecraft needs to be injected into an orbit where you think it is the best to trigger the lander descent event. If I'm not mistaken, the motion of the spacecraft needs to account for the motion of Mercury. However, is designing the objective function as a norm on the states S sufficiently justified for minimizing the burning of propellant?
Walter Roberson
le 12 Sep 2023
Mercury has an exosphere . Depending how extensive it is, it might need to be taken into account, https://www.space.com/18644-mercury-atmosphere.html as I suspect it might be enough to provide some heating during landing.
Hynod
le 13 Sep 2023
Walter Roberson
le 13 Sep 2023
is the density approximately zero though? It is enough to create bow shocks. https://www.nature.com/articles/s41467-020-18220-2
I have no idea what density would result in significant thermal heating of a landing probe, but I don't think it should be entirely neglected. At the very least I would suggest that measurements of heat and magnetism other factors during the descent could be interesting science.
Walter Roberson
le 13 Sep 2023
The professor indeed would have let me aware of something so relevant fo the de-orbiting,
Sorry, but No. Professors are not semi-supernatural, able to think of everything. If they were then you would not be writing a thesis: you would be writing a lab report.
If there were trajectory resistence due to shock formation in the exosphere of Mercury, how much would there have to be to affect the trajectory (to beyond the round-off error of the calculations -- more, for example, than the difference between using ode45() and ode78()) ? If there were heating due to the shock, how much would there have to be to be of concern? Now is there evidence that the actual density of the exosphere of Mercury is definitely below those levels, beyond any reasonable margin of error in the current modeling of the exosphere? At the very least your thesis should include a paragraph as to why you can be certain that it was definitely unnecessary to model those effects.
Hynod
le 14 Sep 2023
Yousef
le 22 Oct 2023
Modifié(e) : Walter Roberson
le 22 Oct 2023
Yes, you can definitely use a function handle as an input to `fmincon`, and the function that the handle refers to can be quite complex. In fact, many optimization problems involve non-trivial functions, including numerical integrators.
Let's break down your question a bit:
1. **Function Handle for Objective Function**: Your objective function can be a numerical integrator or any other complex routine. Here's a simple way to set it up:
matlab code:
function cost = myObjectiveFunction(x)
% Here, x is the vector of variables you're optimizing.
% You can use x as initial conditions or parameters for your numerical integrator.
% ... Your complex routine, e.g., numerical integration
result = myIntegrator(x); % This is just a hypothetical function
% The objective is to minimize 'result'
cost = result;
end
% Call fmincon
x0 = [initial_guesses]; % replace with your initial guesses
[x_optimal, fval] = fmincon(@myObjectiveFunction, x0, ...);
```
2. **Equality and Inequality Constraints**: If you have constraints that involve complex operations or other integrations, you can use function handles for those as well.
matlab code:
function [c, ceq] = myConstraints(x)
% c(x) <= 0 constraints
c = ...; % your inequality constraints
% ceq(x) = 0 constraints
ceq = ...; % your equality constraints
end
% Call fmincon
options = optimoptions('fmincon','SpecifyConstraintGradient',false);
[x_optimal, fval] = fmincon(@myObjectiveFunction, x0, [], [], [], [], [], [], @myConstraints, options);
```
3. **Using Integrators within the Optimization Routine**: If you're using MATLAB's built-in numerical integrators (like `ode45`, etc.), you can easily embed them inside your objective function or constraints:
matlab code:
function cost = myObjectiveFunction(x)
[~, Y] = ode45(@(t,y) myODE(t, y, x), tspan, initial_conditions);
% Process Y to get the cost, if necessary
cost = ...; % some function of Y
end
```
Here, `myODE` would be another function that defines your differential equations. The point is, `fmincon` doesn't care how the function value is computed. It just needs a function handle to obtain values for given inputs.
In essence, as long as you can define your problem in terms of functions (handles) that `fmincon` can call to get objective values, constraints, and optionally gradients, you can use any complex logic within those functions, including numerical integrators.
Walter Roberson
le 22 Oct 2023
In essence, as long as you can define your problem in terms of functions (handles) that `fmincon` can call to get objective values, constraints, and optionally gradients, you can use any complex logic within those functions, including numerical integrators.
Both fmincon() and ode45() have the restriction that the functions being invoked by them must have continuous first derivatives (and second derivatives too in some cases). Therefore you cannot use any complex logic in the functions.
The particular system being modeled has four phases. If there is a smooth (2 second derivatives) transition between each of the phases, then fmincon() and ode45() should be able to handle it, but if there is an abrupt transition anywhere in the mix then you would need to break up the logic into multiple parts.
Réponse acceptée
Plus de réponses (3)
No, the objective function that you give to fmincon can contain anything, but fmincon's algorithms will assume that the input-to-ouput mapping is differentiable and in some cases twice differentiable.
1 commentaire
Hynod
le 11 Sep 2023
The problem is that it is not an explicit function, so it seems that fmincon never stops finding the minimum
It is possible that fmincon is struggling to find the minimum, but that shouldn't be related to the "explicitness" of the function. There certainly is no requirement that fun(p) be implemented in a single-line formula, if that's what worries you.
Note however, that the solution to an ODE can be a discontinuous (and therefore non-differentiable) function of the initial state. That can be a problem, since fmincon assumes fun(p) to be continuous and differentiable. Note also that numerical ODE solvers like ode113() have certain fragilities. This is why MathWorks has posted some relevant guidelines about problems involving ODEs, which you should read.
so it seems that fmincon never stops finding the minimum, how can I limitate the iterations?
opts=optimoptions('fmincon','MaxIterations',100);
p=fmincon(fun,____,opts)
Generally, though, if you have to cut fmincon off after some number of iterations, it means the optimization is failing.
13 commentaires
Ajay
le 12 Sep 2023
nice
Hynod
le 12 Sep 2023
Yes, the number of iterations is not the same as the number of function evaluations. That, you would control with MaxFunctionEvaluations.
But note that because you are not supplying a gradient calculation (SpecifyObjectiveGradient=false), fmincon needs to call your objective fun at least N+1 times to make a finite difference approximation of the gradient. Here, N is the number of unknown variables..
Walter Roberson
le 13 Sep 2023
fmincon() asked to process 7 variables will immediately call the function 7+1=8 times in order to create a starting point for minimization. Those 7+1 = 8 will be counted in the final count, but the maximum iterations will not be checked until after those 7+1 = 8 calls are done.
Sam Chak
le 13 Sep 2023
Additionally, I don't know why thw initial guess and the final value obj1 are the same (obj is not in the workspace and I don't know why)
Your chosen objective function is the Euclidean norm of the vector KAPPA, κ and it is defined by

KAPPA = cspice_oscltx(S_1(end,:)', 2, mu_m);
If κ directly depends on final state vector
and you are trying to find the initial state vector
that minimizes
, isn't it logically true that fmincon will return
?
Are you trying to determine when to start 'deorbiting' the spacecraft by finding the initial state vector
that minimizes the burning of propellant? If so, I think that the objective function needs to be modified to address this as well.
Hynod
le 13 Sep 2023
Sam Chak
le 13 Sep 2023
The objective function needs to be carefully designed. But before that, it is necessary to understand what physical parameter in the system that you want to minimize. For example, if you want to obtain the time-optimal deorbiting maneuver from a non-equilibrium initial state, then the quadratic error function over time can be considered. The Euclidean norm is somewhat related to the quadratic error function. However, you considered only the norm of the final state.
Hynod
le 13 Sep 2023
Walter Roberson
le 13 Sep 2023
The objective function for minimization must return a scalar for all minimization algorithms other than gamultiobj and paretosearch
Hynod
le 14 Sep 2023
As you can see in my example below, we can work on a vector (state x) and return a scalar representing the Euclidean norm. This scalar can be used as the objective function value. However, in your code, you take the norm of the final state value, which also returns a scalar. Nevertheless, there is a significant difference in the physical meaning between the vector norm and the end-state norm.
% solving the 1st-order ODE
[t, x] = ode45(@(t, x) - x, [0 10], 1);
% check the size of vector, x(t)
Sx = size(x)
% vector-based norm
Nx = norm(x)
% end-state norm
Ne = norm(x(end))
% exponential decay solution
plot(t, x, 'linewidth', 1.5), grid on
Walter Roberson
le 16 Sep 2023
Unfortunately at the moment I do not really understand the difference between gamultiobj and paretosearch . It looks like they use different algorithms, with gamultiobj() using genetic algorithm approaches, and paretosearch using pattern searching; https://www.mathworks.com/help/gads/paretosearch-algorithm.html
Hynod
le 16 Sep 2023
0 votes
2 commentaires
Walter Roberson
le 22 Oct 2023
gamultiobj() for one internally generates a number of starting points (number == "population size") based upon the upper and lower bound. You can use the options for gamultiobj to supply a particular starting population.
Catégories
En savoir plus sur Earth and Planetary Science dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
