Graphing a 2nd order ODE

30 Ansichten (letzte 30 Tage)
Kyle Brahm
Kyle Brahm am 24 Apr. 2022
Beantwortet: Torsten am 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 Kommentare
VBBV
VBBV am 24 Apr. 2022
Can you share your code ?
Kyle Brahm
Kyle Brahm am 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))

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Sam Chak
Sam Chak am 24 Apr. 2022
Bearbeitet: Sam Chak am 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 am 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))

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by