HW 02
HW 02
Spring 2024
HW #2
In Matlab, you can solve Ordinary Differential Equations (ODEs) numerically. The most commonly
used Matlab function used for such applications is ode45(). Read the documentation to
understand how it works.
( )
Example: Let’s have an ODE as such, + 4𝑦(𝑡) = 0, 𝑦(0) = 5. We want to find 𝑦(𝑡) in the range
0 ≤ 𝑡 ≤ 10
( )
For ode45(), you need to represent the ODE as a function in the form = 𝑓 𝑡, 𝑦(𝑡) for the
current 𝑡
File: func_f.m
function dy = func_f(t, y)
% t is a scalar, and is the current time
% y could be a vector, is the current value y at time t
% dy is the current value of the derivative of y at time t
dy = -4*y;
end
This function above describes the ODE. (Note that obviously you can call this function whatever you
want as long as its name doesn’t overlap with another function you plan to use)
Next, we can solve for 𝑦(𝑡) from the function ode45() by running the script in the Command
Window:
𝑑𝑦 (𝑡)
+ 4𝑦 (𝑡) + 5𝑦 (𝑡) = 0,
𝑑𝑡
𝑑𝑦 (𝑡)
+ 𝑦 (𝑡) + 2𝑦 (𝑡) = 0.
𝑑𝑡
In addition: 𝑦 (0) = 1, 𝑦 (0) = 2
( ) 𝑦 (𝑡)
Our new function func_2f() representing = 𝑓 𝑡, 𝑦(𝑡) where 𝑦(𝑡) = is
𝑦 (𝑡)
File: func_2f.m
function dy = func_2f(t, y)
% t is a scalar, and is the current time
% y is a vector, is the current value y at time t
% dy is the current value of the derivative of y at time t
dy1 = -4*y(1) - 5*y(2);
dy2 = -1*y(1) - 2*y(2);
dy = [dy1; dy2]; % Column vector
end
Next, our script will be:
Given:
𝑑 𝑦(𝑡) 𝑑𝑦(𝑡)
+3 + 9𝑦(𝑡) = 0,
𝑑𝑡 𝑑𝑡
And 𝑦(0) = 0, 𝑦̇ (0) = 2
First show your transformed ODEs (the new two first order ODEs)
Given:
𝑑𝑦(𝑡)
(2 + sin 𝑦(𝑡)) + 5𝑡 𝑦(𝑡) = 𝑥(𝑡),
𝑑𝑡
𝑦(0) = 0, and 𝑥(𝑡) = (1 − 𝑒 )𝑢(𝑡 − 1)
(Note that the step function in the input is shifted to the right).
(You can mimic step function by just having if statements in your ODE function).