8 views (last 30 days)

Hi All,

I am solving an optimization problem.There are no equality constraints in my model. I want to pass ODEs as constraint to fmincon using,non-linear constraint, nlcon argument .

The function model contains odes

function dz = model(t, z, c)

dz(1) = ..

dz(2) = ..

I'd like to ask how the ode intergrator has to be called in the place of nlcon in fmincon(@objective,p0,A,b,Aeq,beq,lb,ub,nlcon).

%p0 = Initial Value of Parameters

A = [];

b = [];

Aeq = [];

beq = [];

nlcon = ode45(@(t,z)model(t,z,x), tSpan, z0); % Is this correct?

p = fmincon(@objective,p0,A,b,Aeq,beq,lb,ub,nlcon);

Since the ode function is already called in nlcon , is it required to call the function model again inside the objective function?

function cost = objective(c,time_exp,expZ,tSpan,z0)

sol = ode45(@(t,z)model(t,z,c), tSpan, z0); % Is this step required?

ModelZ = deval(sol, time_exp);

cost = ModelZ-expZ;

Any help would be highly appreciated.

Alan Weiss
on 23 Apr 2019

I am not sure that I understand what you mean by "I want to pass ODEs as a constraint to fmincon." What about the ODE solution is a constraint? Do you want the minimum value of the solution to be above zero? Do you want the ODE solution to lie in a certain box? Once you explicitly define what you mean by the ODE solution is a "constraint," then I think that the answer to your question will be obvious. You will solve the ODE, get sol, and then write the constraint in terms of sol, maybe fun(deval(sol,tspan)) for some appropriate function fun and set of times tspan.

Alan Weiss

MATLAB mathematical toolbox documentation

Alan Weiss
on 23 Apr 2019

I still do not understand precisely what you are trying to do, so cannot give you a very detailed answer.

If you want your steady-state values to be in a certain range, then you probably want to restrict deval(sol,lasttime), where lasttime is the final time. But if you just want to restrict the steady-state, then probably you shouldn't solve an ODE anyway, just the steady-state equations, which would be an fsolve call, finding the location where dz = zeros(size(z0)).

Maybe you want to restrict the values of the solution at all times to be in a certain range, not just the final values. In that case, you would set constraints on deval(sol,tspan), where tspan is the set of times that you want to restrict.

Maybe you are trying to fit your ODE solution to some observed (measured) values as a function of time. In that case, follow the procedures in this example.

Good luck,

Alan Weiss

MATLAB mathematical toolbox documentation

Alan Weiss
on 23 Apr 2019

Sorry, I am unable to understand what you are trying to do, so this will be my last reply. I suggest that you write out equations or inequalities describing your constraint. Seriously, write them down. Then, given those equations or inequalities, you can write a corresponding nonlinear constraint function in fmincon syntax, restricting the result by using deval on the solution at relevant times. If you don't understand what I am suggesting, sorry, maybe someone else can help.

Alan Weiss

MATLAB mathematical toolbox documentation

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 14 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697189

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697189

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697201

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697201

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697210

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697210

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697215

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697215

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697527

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697527

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697565

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697565

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697635

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697635

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697654

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697654

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697710

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697710

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697714

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697714

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697962

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_697962

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_698021

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_698021

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_698462

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_698462

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_698466

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/457960-question-regarding-passing-input-arguments-in-fmincon#comment_698466

Sign in to comment.