ODE time vector plot does not correspond to the time vector values

2 Ansichten (letzte 30 Tage)
Afonso
Afonso am 10 Feb. 2025
Verschoben: Walter Roberson am 13 Feb. 2025
Hi! When I solve an ODE using ode23 I get a time vector and my state. In the time vector everything seems fine however when I plot the states agaisnt the time, the time appears completely skewed(it should be from 0-5 and its from 0-0.09.....)
How can I solve this?
  1 Kommentar
Mike Croucher
Mike Croucher am 13 Feb. 2025
It is very difficult to help here without all of the code in a runnable form. We'll need your ode_arm function and it would also help a lot if you copied and pasted the code as text rather than a screenshot. That way, we can easily run it ourselves.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Star Strider
Star Strider am 10 Feb. 2025
We don’t have your ‘ode_arm’ function, however it may be that ode23 is encountering a singularity () at about 0.9 time units, and then stops, probably also throwing an error message.

Sam Chak
Sam Chak am 13 Feb. 2025
Verschoben: Walter Roberson am 13 Feb. 2025
The simulation will stop when the ODE solver encounters NaN or Inf situations, or when the solver can no longer reduce the integration step size. Here is an example of an ODE involving the logarithm function. Ideally, the state should reach zero and stop, similar to the water tank level, where negative values are undefined.
oldparam = sympref("HeavisideAtOrigin", 1);
%% System for Cases 1 & 3
function dx = ode1(t, x)
b = 1.5;
dx = (x.^(2 - b)).*log(x);
end
%% System for Case 2
function dx = ode2(t, x)
b = 1.5;
dx = ((x.*heaviside(x)).^(2 - b)).*log(x.*heaviside(x));
end
%% Stop simulation when the state x reaches 0
function [position,isterminal,direction] = touchZeroEventFcn(t, x)
position = x; % The state variable that we want to be zero
isterminal = 1; % Halt integration
direction = -1; % The zero can be approached when x is decreasing
end
%% Settings
tspan = linspace(0, 3, 3001);
x0 = 0.9;
options = odeset('Events', @touchZeroEventFcn);
%% Case 1: Original dynamics without EventFcn
[t, x] = ode45(@ode1, tspan, x0);
plot(t, x), grid on, xlabel('Time')
Warning: Imaginary parts of complex X and/or Y arguments ignored.
%% Case 2: Modified for non-negative value of x
[t, x] = ode45(@ode2, tspan, x0);
plot(t, x), grid on, xlabel('Time')
%% Case 3: Original dynamics with EventFcn
[t, x] = ode45(@ode1, tspan, x0, options);
Error using odezero (line 49)
odezero: an event disappeared (internal error)

Error in ode45 (line 393)
odezero(@ntrp45,eventFcn,eventArgs,valt,t,y,tnew,ynew,t0,h,f,idxNonNegative);
plot(t, x), grid on, xlabel('Time')

Community Treasure Hunt

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

Start Hunting!

Translated by