Passing variables out of ODE45

In summary,The ode code zdot = odenesttest has the following equations:zdot(2) = ((r_p*k_mb)/I_p)*(-r_p*z(1)+r_g*z(3)+z(5)-z(7));zdot(6) = (k_mb/m_p)*(r_p*z(1)-r_g*z(3)-z(5)+z(7));zdot(4) = ((r_g*k_mb)/I_g)*(-r_g*z(3)+r_p*z(1)+z(7
  • #1
Sirsh
267
10
Hi all,

I have the following ode code, and I want to know how I could output the variable "k_mb" within my main script so that I can plot it to see if it's actually active when the solver is integrating.
odescript
Matlab:
function zdot = odenesttest(t,z)
global r_p r_g I_p I_g m_p m_g V k_mb
A_or_B = [5,10];
V = [0,5,15,20,30,35,45,50,60];
%% state space equations are defined below:
zdot(2) = ((r_p*k_mb)/I_p)*(-r_p*z(1)+r_g*z(3)+z(5)-z(7));  % Eq 1ss
zdot(6) = (k_mb/m_p)*(r_p*z(1)-r_g*z(3)-z(5)+z(7));         % Eq 2ss
zdot(4) = ((r_g*k_mb)/I_g)*(-r_g*z(3)+r_p*z(1)+z(7)-z(5));  % Eq 3ss
zdot(8) = (k_mb/m_g)*(-r_p*z(1)+r_g*z(3)-z(7)+z(5));        % Eq 4ss

zdot(1) = z(2); % Eq 5ss
zdot(3) = z(4); % Eq 6ss
zdot(5) = z(6); % Eq 7ss
zdot(7) = z(8); % Eq 8ss

zdot(9) = z(1); % Pinion angular displacement (control angle).
torsional_stiffness_changer
   function torsional_stiffness_changer
       if 0 <= zdot(9)
           lower_index = find(V < zdot(9), 1, 'last');
           even_or_odd = mod(lower_index,2);
           k_mb = A_or_B(2 - even_or_odd);
       end
   end
% Outputs
zdot = zdot';
end

Mainscript
Matlab:
% Mainscript for vibration analysis (19/01/2017).
clear all;
clc;
%% Defining global variables
global r_p r_g I_p I_g m_p m_g k_mb
k_mb = 50;  % torsional stiffness.
r_p = 10;   % pinion pitch(?) radius
r_g = 15;   % gear pitch(?) radius
I_p = 100;  % pinion inertia.
I_g = 200;  % gear inertia.
m_p = 50;   % pinion mass
m_g = 100;  % gear mass
%==========================================================================
%% initial coditions for ode solver
%==========================================================================
z0 = [1,0,0,0,0,0,0,0,0]; % zero initial conditions.
t_step = [0:1:10];        % timestep 0 to 5 seconds.
opts = odeset('Reltol', 1e-3, 'AbsTol', 1e-3);  % conservative tolerances.
[t,z] = ode45(@odenesttest, t_step, z0, opts);
%==========================================================================
%% Screen Output and Plotting
%==========================================================================
[t,z(:,:)]

Thanks a lot!
 
Physics news on Phys.org
  • #2
if it is a global, you should be able to see it all the time, write it to a file or vector and update the plot...
 
  • #3
Dr Transport said:
if it is a global, you should be able to see it all the time, write it to a file or vector and update the plot...

Sorry, in the mainscript code that I listed I meant to take out the "k_mb = 50;". I have edited it and put it below.

Updated mainscript
Code:
% Mainscript for vibration analysis (19/01/2017).
clear all;
clc;
%% Defining global variables
global r_p r_g I_p I_g m_p m_g k_mb
r_p = 10;   % pinion pitch(?) radius
r_g = 15;   % gear pitch(?) radius
I_p = 100;  % pinion inertia.
I_g = 200;  % gear inertia.
m_p = 50;   % pinion mass
m_g = 100;  % gear mass
%==========================================================================
%% initial coditions for ode solver
%==========================================================================
z0 = [1,0,0,0,0,0,0,0,0]; % zero initial conditions.
t_step = [0:1:10];        % timestep 0 to 5 seconds.
opts = odeset('Reltol', 1e-3, 'AbsTol', 1e-3);  % conservative tolerances.
[t,z] = ode45(@odenesttest, t_step, z0, opts);
%==========================================================================
%% Screen Output and Plotting
%==========================================================================
[t,z(:,:)]
 
Last edited:

1. How do I pass variables out of ODE45?

To pass variables out of ODE45, you can use the "output function" feature of the ODE45 solver. This allows you to specify which variables you want to pass out of the solver, and they will be stored in the output argument of your function.

2. Can I pass multiple variables out of ODE45?

Yes, you can pass multiple variables out of ODE45 by using the "output function" feature. Simply specify all the variables you want to pass out when defining your output function.

3. What is the format of the output argument when using the "output function" feature?

The output argument when using the "output function" feature is a structure array. Each element of the array corresponds to a single time step in the ODE solver and contains the values of the variables you have specified to be passed out.

4. Can I modify the output argument of the "output function" feature?

Yes, you can modify the output argument of the "output function" feature to suit your needs. For example, you can add additional variables to be passed out or perform calculations on the existing variables before passing them out.

5. How do I access the variables passed out of ODE45 in my main function?

The variables passed out of ODE45 can be accessed in your main function by using the output argument of the "output function" feature. You can use the structure array to access the values of the variables at different time steps and use them for further calculations or analysis.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top