Why ODE45 returns NaN values
Show older comments
I am having trouble identifying why ODE45 returns
or
values for the following model I'm trying to compute.
I have experimented with a variety of cases which I will list below, but I still have no idea why it will return
or
values. I have also presented the relevant functions and scripts I have been running below.
RHS is the function for which I'm applying ODE45. RHS is being computed over a
grid (the unit square). I don't believe the RHS is part of the problem since there are no undefined values (such as dividing by 0 or
). If you believe so, please let me know why.
function uv = RHS(t,y)
% defines the right hand side of the Eulerian velocity field obtained
% from the analytical solution of the Stommel equation.
% Relevant equations and assumptions can be found from the paper:
% NUMERICAL SIMULATIONS OF PASSIVE TRACERS DISPERSION IN THE SEA, page 36
day = 11.6;
a = 1e7;
bta = 2*10^-11;
r = 1/(day*86400);
scalefac = 0.05;
es = r/(a*bta);
uv = [-(1-exp(-y(1)./es)-y(1))*pi^2.*cos(pi.*y(2))*scalefac;...
(exp(-y(1)./es)./es -1)*pi*sin(pi.*y(2))*scalefac];
(The following are the cases that I've tried, to hopefully see the cause of this problem.)
Case 1: For some initial conditons, ODE45 computes some iterations but ends up with
values.
tspan = [0 3];
y0 = [0.125; 0.265];
[t,y] = ode45(@RHS, tspan, y0);
figure(1)
plot(y(:,1),y(:,2),'-o');
title('Trajectory of particle');
xlabel('x'); ylabel('y');
axis([0 1 0 1]);
The result is a solution y of size
where its 14th entry suddenly becomes
.
continues till the end (53rd entry). Interestingly, the 17th entry presents
.
This result seems to be 'fixed' when I apply option tolerances as shown below. (For an arbitrary tolerance choice).
tspan = [0 3];
y0 = [0.125; 0.265];
opts = odeset('RelTol',1e-8,'AbsTol',1e-8);
[t,y] = ode45(@RHS, tspan, y0, opts);
figure(1)
plot(y(:,1),y(:,2),'-o');
title('Trajectory of particle');
xlabel('x'); ylabel('y');
axis([0 1 0 1]);
Case 2: For some initial conditions, the solution when applying option tolerances produces
values.
tspan = [0 3];
y0 = [0.005; 0.005];
opts = odeset('RelTol',1e-8,'AbsTol',1e-8);
[t,y] = ode45(@RHS, tspan, y0, opts);
figure(1)
plot(y(:,1),y(:,2),'-o');
title('Trajectory of particle');
xlabel('x'); ylabel('y');
legend('t = [0 3]');
axis([0 1 0 1]);
This time it goes straight to
values by the 2nd iteration. The result y is size
filled with
values except the initial condition. And of course, when running this without option tolerances, the trajectories are computed just fine. This has become a problem because I need to compute many trajectories and I can't just go specifying certain conditions for many different points. There is a more general problem that I'm not seeing.
Case 3: The above two cases seem to be 'fixed' when I ignore ODE45's default timestepping, and instead manually state a time step, as in the following, I let
increase from 0 by steps of
. We try Case 2 with this:
tspan = [0:0.01:3];
y0 = [0.005; 0.005];
opts = odeset('RelTol',1e-8,'AbsTol',1e-8);
[t,y] = ode45(@RHS, tspan, y0, opts);
figure(1)
plot(y(:,1),y(:,2),'-o');
title('Trajectory of particle');
xlabel('x'); ylabel('y');
legend('t = [0 3]');
axis([0 1 0 1]);
It computes the trajectory just fine, and the same results occur with Case 1. With or without option tolerances. It seems to be that the adaptive timestep used by ODE45 may be the problem, but I eventually found another point that began to cause problems.
Case 4: Some initial positions produce NaN values when specifying a timestep.
tspan = [0:0.01:3];
y0 = [0.7635; 0.0935];
opts = odeset('RelTol',1e-8,'AbsTol',1e-8);
[t,y] = ode45(@RHS, tspan, y0, opts);
figure(1)
plot(y(:,1),y(:,2),'-o');
title('Trajectory of particle');
xlabel('x'); ylabel('y');
legend('t = [0 3]');
axis([0 1 0 1]);
The result y of size
seems to be computed just fine until the 143rd entry, where it begins to present
values until the end. Where the last, 151st entry is
. This occurs with or without option tolerances and with or without explicit timesteps.
It seems that the above 4 cases rule out each of the possibilities to do with timestepping, options and that there is definitely something that I'm overlooking. Any help is appreciated.
Accepted Answer
More Answers (0)
Categories
Find more on Programming 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!