How to apply NonNegativity constraint in ODE solver when defining ode as a structure

I am trying to perform a local sensitivity analysis on the parameters of my model using odeSensitivity. Taking help from this page https://in.mathworks.com/help/matlab/ref/odesensitivity.html. but the problem is that when I used to code the ode simply I put nonnegativity under options, but now i am not able to do that. I tried taking F.NonNegative=ones(1,n_parameters) but that doesnt seem to work.

Réponses (1)

The relevant property of the ode object to specify that some of the variables must be non-negative is NonNegativeVariables. After you've set up that property, create the odeSensitivity object and set it to the ode object's Sensitivity property.

7 commentaires

Could you please give an example for this, the code is not recognizing NonNegativeVariables input
I see four options that can be used with odeSensitivity (ParameterIndices, InitialValue, InitialSlope and Jacobian).
No NonNegative option available.
So if my function is f(t,y,p). Solution is essentially dy/dt and p are constant parameters, i want to assess dydp for all p and all y for sensitivity analysis. Function is quite complex and I cant express it simply in a matrix of equations. It has internal iterative calculations for pH at each step. How do i calculate sensitivity?
NonNegativeVariables is a property of the ode object, not the odeSensitivity object. Using the example from the odeSensitivity documentation page with one addition:
p = [0.05 1.5];
F = ode(ODEFcn=@(t,y,p) [p(1)*y(1)-y(2); -p(2)*y(2)], ...
InitialValue=[2 3], ...
Parameters=p, ...
Sensitivity=odeSensitivity, ...
NonNegativeVariables = 2) % constrain the 2nd variable to be non-negative
F =
ode with properties: Problem definition ODEFcn: @(t,y,p)[p(1)*y(1)-y(2);-p(2)*y(2)] Parameters: [0.0500 1.5000] InitialTime: 0 InitialValue: [2 3] NonNegativeVariables: 2 Sensitivity: [1x1 odeSensitivity] Solver properties AbsoluteTolerance: 1.0000e-06 RelativeTolerance: 1.0000e-03 Solver: auto SelectedSolver: cvodesNonstiff Show all properties
Note that F has a Sensitivity property and a NonNegativeVariables property in its display. The NonNegativeVariables property probably won't do much (based on the picture in the example) as it looks like y(2) never becomes negative, but you can specify it.
S = solve(F,0,5)
S =
ODEResults with properties: Time: [0 2.9540e-09 2.9543e-05 2.2466e-04 4.1978e-04 0.0024 0.0139 0.0255 0.0484 0.0713 0.1363 0.2014 0.2992 0.3970 0.4948 0.6661 0.8373 1.0085 1.1798 ... ] (1x34 double) Solution: [2x34 double] Sensitivity: [2x2x34 double]
As long as you can define your ODE function that accepts t, y, and p inputs and returns the right-hand side of your system of differential equations this approach should work.
I tried this and I think it worked. But I am still facing an issue with using odesensitivity. I modelled the ode as an object and it gave me correct results, but as soon as I use Sensitivity=odesensitivity, the results returned are null matrices, I tried many times, different parameters, nothing, nada, everytime it gives null vector. I even tried to compress my whole code into one matrix and define everything as odes in matrix form.
Please send the code you're using to solve the system to Technical Support directly using this link. That way the development staff can investigate to determine if there is a bug in the sensitivity analysis code or if there's a modifiation required in your code.
I have sent the code through the link, I will be waiting for a reply, thank you very much

Connectez-vous pour commenter.

Produits

Version

R2024a

Question posée :

le 23 Août 2024

Commenté :

le 2 Sep 2024

Community Treasure Hunt

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

Start Hunting!

Translated by