我有以下非线性系统和随附的 ODE: 我在 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.
函数
karray(),carray(),marray(),farray()
返回系统参数的数组表示,而 odefun()
返回 EOM 位移和速度的数组。我将每个质量的初始位移和速度设置为零作为我的初始值。 odefun()
如下所示。
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
所示代码运行完美,没有错误。但是,当我尝试使用
[t_ode,X_answer] = ode45(fun,tspan,X_0,options);
指定容差选项时,出现错误:
使用 odeset()
时如何正确指定 ode45()
。
选项中的
Mass
矩阵需要是 4*4
矩阵,因为你实际上有 4 个变量。