Graphing a 2nd order ODE

조회 수: 29 (최근 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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by