ODE time vector plot does not correspond to the time vector values
조회 수: 9 (최근 30일)
이전 댓글 표시
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
Mike Croucher
2025년 2월 13일
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.
답변 (2개)
Star Strider
2025년 2월 10일
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.
댓글 수: 0
Sam Chak
2025년 2월 13일
이동: Walter Roberson
2025년 2월 13일
Hi @Afonso
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')
%% 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);
plot(t, x), grid on, xlabel('Time')
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

