Graphing a 2nd order ODE

29 vues (au cours des 30 derniers jours)
Kyle Brahm
Kyle Brahm le 24 Avr 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 commentaires
VBBV
VBBV le 24 Avr 2022
Can you share your code ?
Kyle Brahm
Kyle Brahm le 24 Avr 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))

Connectez-vous pour commenter.

Réponses (2)

Sam Chak
Sam Chak le 24 Avr 2022
Modifié(e) : Sam Chak le 24 Avr 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 le 24 Avr 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))

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by