Graphing a 2nd order ODE

22 ビュー (過去 30 日間)
Kyle Brahm
Kyle Brahm 2022 年 4 月 24 日
回答済み: Torsten 2022 年 4 月 24 日
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 件のコメント
VBBV
VBBV 2022 年 4 月 24 日
Can you share your code ?
Kyle Brahm
Kyle Brahm 2022 年 4 月 24 日
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))

サインインしてコメントする。

回答 (2 件)

Sam Chak
Sam Chak 2022 年 4 月 24 日
編集済み: Sam Chak 2022 年 4 月 24 日
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 2022 年 4 月 24 日
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))

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by