Non-Negative Solution with Integrator in Simulink
Show older comments
When solving ode in Matlab, the ode solver has an option that allows setting the solution non-negative property to be true. For instance -
x = odeset;
x.NonNegative = ones(numStates,1);
Is there an equivalent setting in Simulink? Something that can be adjusted in either model parameters or the [1/s] block?
Accepted Answer
More Answers (1)
Hi @Rajmohan
I'm not familiar with all types of settings, codes, and tools in MATLAB/Simulink. Therefore, I usually resolve numerical issues using my own mathematical tricks, whenever possible.
In this case, you can attempt to address the ODE by incorporating a signum function in the Simulink model. You may find the demonstration below helpful, which is obtained from the "Nonnegative ODE Solution" article. However, I have also devised my own solution without relying on the built-in 'NonNegative' option.
ode = @(t, y) - abs(y);
% Analytic solution
tspan = [0 40];
t = linspace(tspan(1), tspan(2), 1001);
y = exp(-t);
%% Case 1: Standard solution with ode45
options1 = odeset('Refine',1);
[t1, y1] = ode45(ode, tspan, 1, options1);
%% Case 2: Solution with nonnegative constraint
options2 = odeset(options1, 'NonNegative', 1);
[t2, y2] = ode45(ode, tspan, 1, options2);
%% Case 3: Solution with signum function
odefix = @(t, y) - abs(y)*sign(y);
[t3, y3] = ode45(odefix, tspan, 1, options1);

%% Case 4: Solution with ReLU function
odeReLU = @(t, y) - abs((sqrt(y.^2) + y)/2); % ReLU(x) = (√(x²) + x)/2
[t4, y4] = ode45(odeReLU, tspan, 1, options1);
%% Plot results
plot(t, y, '-', t1, y1, 'o', t2, y2, '*', t3, y3, 'x', t4, y4, 'p'), grid on, ylim([-5 2])
xlabel('t'), ylabel('y(t)')
legend('Exact solution', 'No constraints', 'Nonnegativity', 'Signum Fix', 'ReLU Fix', 'Location', 'best')
1 Comment
Sam Chak
on 8 May 2024

- The output is true (equal to 1) when the input signal is greater than or equal to zero, and its previous value was less than zero.
- The output is false (equal to 0) when the input signal is less than zero, or if the input signal is nonnegative, its previous value was also nonnegative.
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



