algorithmmatlabsimulationodesolver

Specify Mass Matrix in ode45() MATLAB


I have the following nonlinear system and accompanying ODE: enter image description here enter image description here My solution in MATLAB:

% Solving with ODE45
dt = 10^-1; % [s]
T = 100; % [s]
N = 2; % number of masses
X_0 = zeros(2*N,1);
t_span = [0:dt:T];
m_i = 1; % [kg]
k_i = 2; % [N/m]
c_i = 1; % [kg/s]
gamma_i = 1; % [N/m^3]
f_i = 1; % [N]
f = @(t) f_i.*sin(alpha.*t).*cos(beta.*t); % [N]
m = m_i.*ones(N,1); % [kg]
k = k_i.*ones(N+1,1); % [N/m]
c = c_i.*ones(N+1,1); % [kg/s] 
gamma = gamma_i.*ones(N+1,1); % [N/m^3]
alpha = 0.2;
beta = 0.15;
K = karray(k,gamma,N);
M = marray(m);
C = carray(c,N);
F = @(t) farray(f,t,N);
options = odeset('Mass',M,'RelTol',1e-3,'AbsTol',1e-3);
fun = @(t,X)odefun(X,K,C,M,F(t),N); % Defines the ODE function and it's input matrices
[t_ode,X_answer] = ode45(fun,tspan,X_0); % Calls the ODE45 function (see end of code), sets the time and axial conditions. Outputs time and displacements.

The functions karray(),carray(),marray(),farray() return array representations of the system parameters while odefun() returns an array of the EOM's displacements and velocities. I had set the initial displacement and velocity of each mass to zero as my initial values. odefun() is shown below.

function output = odefun(X,K,C,M,F,resSize) % odefun, with input matrices
 X_variable = X(1:resSize); % position
 X_variable_dot = X(resSize+1:2*resSize); % 1st derivitive
 X_variable_dot_dot = M\(F-K(X)*X_variable-C(X)*X_variable_dot); % 2nd derivitive, from the equation of motion
 output=[X_variable_dot;X_variable_dot_dot]; % places results as a column vector
end

The code as shown works perfectly without errors. However, when I try to specify tolerance options with [t_ode,X_answer] = ode45(fun,tspan,X_0,options);, I get the error: enter image description here How do I properly specify odeset() when using ode45().

EDIT: The solution: enter image description here


Solution

  • Your Mass matrix in options needs to be a 4*4 matrix because you have 4 variables effectively.