ode45 solver errors with time-dependant forcing/parameters

Hi, I'm using a script below to apply a small time-dependant impulse force to a 2DOF spring-mass damper system.
I seem to be getting a lot of errors with my ode45 solver and was wondering if I could get some help? It's due in 2 days so I could really use some support.
Thanks!
function batch_timeresp
% the total time domain response
clear all
close all
clc;
% System parameters
k3 = 15; k1 = 10.0; k2 = 10.0; m1 = 1; m2 = 2; L = 1.0; c = 1; ct = 0.5; g = 9.81;kequi=5;
% Excitation parameters
tpulse=linspace(0,1,50);
Impulse = heaviside(tpulse-0)-heaviside(tpulse-0.1);
F_I=@(t)(interp1(tpulse,Impulse,t));
% State space system matrix and coupling
A=[0,0,1,0;0,0,0,1;-(k1+k2)/(m1+m2),0,-c/(m1+m2),0;0,-k3/(m1*(L^2)),0,-ct/(m1*(L^2))]; %state space matrix
S=[1,0,0,0;0,1,0,0;0,0,1,(1*L)/(m1+m2);0,0,(m1*L)/(m1*(L^2)),1]; %coupling
Sinv=inv(S);
Gmat = [0;0;-g;g/L]; %gravity terms
Fmat=@(tt) ([0;0;F_I(tt)/(m1+m2);(2*F_I(tt)*L)/(m1*(L^2))]); %impulse force
% Numerical integration
tspan=linspace(0,10,1e3);
X0=[0;0;0;0];
[TT,XX]=ode45(@(t,x) ssmodel(t,x,A,Gmat,Fmat(t),Sinv),tspan,X0);
%Plot Impulse
imp = F_I(tpulse);
figure;
plot(tpulse,imp,'b-');
% Results
hold on;
plot(TT,XX),
xlabel('time [s]')
legend('Impulse Force [N]','Block Dispalcement [m]','Ball Displacement [rad]','Block Velocity [m/s]','Ball Velocity [rad/s]')
function dx=ssmodel(t,x,A,Gmat,Fmat,Sinv)
% State-space model of 1 DOF system
dx=Sinv*(A*x+Gmat+Fmat(t)); %%%%%%%%%%%%%%%%%%%%%%
end
end

 Accepted Answer

There are a few flaws - major and minor ones.
(1) size of t has to be consistent to perform all calculations.
(2) Fmat has to be computed before-hand, e.g.: FF = Fmat(t) and then FF values need to be plugged in
(3) dx has to be of 4 - by - sth size. Therefore, you'd need to fix indexes for dx in ssmodel nested fcn. See this post how to fix this flaw: https://www.mathworks.com/matlabcentral/answers/422073-how-to-solve-multi-variable-system-of-odes
Minor flaws are: clc; close all; clear all don't get executed.
Good luck.

More Answers (0)

Categories

Find more on General Applications in Help Center and File Exchange

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!