ode45 Argument Error On Program Restart

3 views (last 30 days)
Sarah Ten Eyck
Sarah Ten Eyck on 3 Oct 2019
Answered: Steven Lord on 3 Oct 2019
I'm having an incredibly bizarre error with my code. My assignment is to start from this base:
function [t,x] = rk_demo % saved under the M-file name: rk_demo.m
global m c k F_0 w
% Define the parameters
m = 5; % mass, kg
c = 30; % damping constant, N-s/m
k = 125; % stiffness, N/m
F_0 = 25; % amplitude of input or force, N
w = 10; % forcing or input frequency, rad/s
% Define the time interval for simulation
t_init = 0;
t_final = 3; % 0 to 3 sec.
t_inc = 0.01;
n_pts = t_final/t_inc + 1; % 301 data points
t_span = linspace(t_init, t_final, n_pts);
% Define the initial condition
x_init = 0.0; % initial position, m
xdot_init = 0.2; % initial velocity, m/s
x_0 = [ x_init; xdot_init ];
% Solve the system equations in differential form
[t,x] = ode45(@model, t_span, x_0);
% Plot displacement vs. time
plot(t,x(:,1))
title('System Response to Harmonic Excitation')
xlabel('time, t')
ylabel('Displacement, m')
end
function f = model(t,x)
global m c k F_0 w
% State space model – 1st order ODE’s
xdot1 = x(2);
xdot2 = ( F_0*cos(w*t) - k*x(1) - c*x(2) ) / m;
f = [ xdot1; xdot2 ]; % must be in column vector form
end
And modify it to work with variable k and c values. My current code looks like this:
function [t,x] = assignment
global m
% Define the parameters
m = 10; % mass, kg
% Define the time interval for simulation
t_init = 0;
t_final = 0.5; % 0 to 1 sec.
t_inc = 0.001;
n_pts = t_final/t_inc + 1;
t_span = linspace(t_init, t_final, n_pts);
% Define the initial condition
x_init = 0.0; % initial position, m
xdot_init = 25; % initial velocity, m/s
x_0 = [ x_init; xdot_init; 0 ];
% Solve the system equations in differential form
[t,x] = ode45(@model, t_span, x_0);
maxy = max(x(:,1));
indexOfFirstMax = find(x == maxy, 1, 'first');
maxy = x(indexOfFirstMax);
maxX = t(indexOfFirstMax);
[fm, fmt] = max(x(:,3));
% Plot displacement vs. time
subplot(1,2,1)
plot(t,x(:,1), maxX, maxy,'pr')
xlim ([0 0.5])
title('Spring Deflection')
xlabel('Time, s')
ylabel('Deflection, m')
txt1 = ['Maximum Deflection = ', num2str(maxy),'m'];
text(maxX, maxy, sprintf(txt1))
% Plot transmitted force vs. time
subplot(1,2,2)
plot(t,x(:,3),(fmt * .001),fm,'pr')
xlim([0 0.5]);
title('Force Transmitted to Base')
xlabel('Time (s)')
ylabel('Transmitted Force (N)')
txt2 = ['Maximum Transmitted Force = ', num2str(fm),'N'];
text((fmt * .001), fm, sprintf(txt2))
end
function f = model(t,x)
global m c k F_0 w
% State space model – 1st order ODE’s
c = 300 + (0.2 .* x(1)); % damping constant, N-s/m
k = 50000 + (10000 .* x(1)); % stiffness, N/m
xdot1 = x(2);
xdot2 = ( F_0*cos(w*t) - k*x(1) - c*x(2) ) / m;
xdot3 = (k .* x(1)) + (c .* x(2));
f = [ xdot1; xdot2; xdot3 ];
end
The code works just fine when I modify it piecemeal from start to finish; run it every time I make a change. The problem is that if I completely close MATLAB, reopen the program, and try to run it again, it gives me this error:
Error using odearguments (line 95)
MODEL returns a vector of length 2, but the length of initial conditions vector is 3. The vector returned by MODEL
and the initial conditions vector must have the same number of elements.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in assignment (line 20)
[t,x] = ode45(@model, t_span, x_0);
and if I try to remove the third initial condition, it tells me
Warning: Failure at t=1.794602e-02. Unable to meet integration tolerances without reducing the step size below
the smallest value allowed (5.551115e-17) at time t.
> In ode45 (line 308)
In assignment (line 20)
Index exceeds matrix dimensions.
Error in assignment (line 27)
[fm, fmt] = max(x(:,3));
Again, this only happens after completely restarting MATLAB, and the code cannot be successfully run again at that point. However, and here's the part that absolutely floors me, it does work again if I run the original base as a standalone .m first, and then run my assignment code; if I do that it'll work until I restart it again. It's like the program breaks on restart and the base program fixes it. I can recreate these events consistently (and I have, 4 times). I have no idea why this would be, nor any idea how to fix it.
To summarize, the sequence looks like this:
1) Run base.m: works every time
2) Run assignment.m: works every time
3) Close MATLAB
4) Start MATLAB
5) Run assignment.m: does not work, returns ode argument error shown above
6) Run base.m: works every time
7) Run assignment.m: works every time
8) Go To 3

Answers (1)

Steven Lord
Steven Lord on 3 Oct 2019
Stop using global variables. Your base function sets them to some values that your assignment requires, but that doesn't preclude some other function setting those same global variables to some arbitrary values that make your assignment code fail. Parameterize your functions instead.

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!