I would like to return a parameter, r, from the ode solution, but this parameter is not to be integrated. However, it is important to the result and for later post-processing. I want r to be a vector matching the length of t so I can do
plot(t, r)
The tricky part for me that seems to stand out from the examples I've looked at here at the forum is that the time dependent variables and their derivatives are dependent on r which in turn r is dependent on a boolean state variable. This means it is rather difficult to calculate r based on the output results from the ode solution variables.
My code is
clear; close all; clf; clct_span = [0 1];y_0 = [0, 2];env.g = 9.81;figure(101); hold onoptions = odeset('AbsTol',1e-6, 'RelTol',1e-6, 'Stats','on');[t, y] = ode45(@my_ode_fcn, t_span, y_0, options, env);figure(1)plot(t, y(:, 1))xlabel('time, t [s]')ylabel('position, x [m]')figure(2)plot(t, y(:, 2))xlabel('time, t [s]')ylabel('velocity, u [m/s]')function dydt = my_ode_fcn(t, y, env) persistent state_flag if isempty(state_flag); state_flag=0; end x = y(1); u = y(2); g = env.g; if ~state_flag r = 20 + 50*t; else r = 20 + 10*t; end if (r > 30) && ~state_flag state_flag = 1; sprintf('State flag = %d', state_flag) end if (r > 27) xdot = 2*u; ydot = -g*x; else xdot = 3*u; ydot = -2*g*x; end figure(101) plot(t, r, '*') dydt = zeros(length(y),1); dydt(1) = xdot; dydt(2) = ydot;end
I've tried all the suggestions in this thread, but nothing has worked so far:
Any syggestions?
Best Answer