Graphing a 2nd order ODE

22 views (last 30 days)
Kyle Brahm
Kyle Brahm on 24 Apr 2022
Answered: Torsten on 24 Apr 2022
I am looking to graph the 2nd order ODE my''+cy'+ky = r(t). with m, c, and k, being adjustible variables. I have successfully made graphs where the m value where 1, yet I cannot figure out how to alter my code to make this work.
  2 Comments
VBBV
VBBV on 24 Apr 2022
Can you share your code ?
Kyle Brahm
Kyle Brahm on 24 Apr 2022
Main Code:
function dydt = order2(t,y)
dydt = zeros(size(y));
a = 2.4; %coefficient for y. term
b = 1.44; %coefficient for y term
r = -10*t*exp(-1.2*t); %forcing function
dydt(1) = y(2);
dydt(2) = r -a*y(2) - b*y(1);
Command window Code:
tspan = [0 2];
y0 = [1,2];
[t,y]=ode45(@order2,tspan,y0);
plot(t,y(:,1))

Sign in to comment.

Answers (2)

Sam Chak
Sam Chak on 24 Apr 2022
Edited: Sam Chak on 24 Apr 2022
I did something similar moment ago in a separate question. Perhaps you can refer to that and apply your own parameters:
function Demo_ODE
close all;
clear;
clc;
Tspan = [0 10]; % simulation time
y0 = [1; 2]; % initial values
[t, y] = ode45(@odefcn, Tspan, y0);
plot(t, y, 'linewidth', 1.5)
grid on
xlabel('Time, t [sec]')
ylabel({'$y_{1}\; and\; y_{2}$'}, 'Interpreter', 'latex')
legend({'$y_{1}$', '$y_{2}$'}, 'Interpreter', 'latex', 'location', 'best')
title('Time responses of the system')
end
function dydt = odefcn(t, Y)
dydt = zeros(2,1);
m = 1;
c = 2.4;
k = 1.44;
r = -10*t*exp(-1.2*t);
F = [0; r/m]; % input force vector
A = [0 1; -k/m -c/m]; % state matrix
dydt = A*Y + F; % state-space model
end
Result:

Torsten
Torsten on 24 Apr 2022
function dydt = order2(t,y)
dydt = zeros(size(y));
a = 2.4; %coefficient for y. term
b = 1.44; %coefficient for y term
m = 2; %coefficient for y.. term
r = -10*t*exp(-1.2*t); %forcing function
dydt(1) = y(2);
dydt(2) = (r -a*y(2) - b*y(1))/m;
Command window Code:
tspan = [0 2];
y0 = [1,2];
[t,y]=ode45(@order2,tspan,y0);
plot(t,y(:,1))

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!