deq(t) =
Solution of ODE with Symbolic Math Toolbox.
Show older comments
Hello,
I want to solve the ordinary differential equation

with
,
,
. The function and
should be a unity step. In other words: it is the step response of a first order lag element. I am using the Symbolic Math Toolbox.
First I define the variables including the relevant assumptions.
syms("t", "positive")
syms("T_1", "k_p", "positive")
syms("u(t)", "y(t)")
The I establish the differential equation
deq = T_1 * diff(y, t) + y == k_p * u
and replace
with the unist step function.
deq = subs(deq, u, heaviside(t))
The solution
y_sol_t = dsolve(deq, y(0) == 0)
y_sol_t = simplify(y_sol_t)
is not very clear for students. It is hard to see that
for
.
If I use the Laplace Transform instead I get: the following result.
Laplace Transform of the above differential equation.
syms("s")
aeq = laplace(deq, t, s)
syms("y_LT")
Replacing laplace(y(t), t, s) and the initial condition
aeq = subs(aeq, [laplace(y(t), t, s) y(0)], [y_LT, 0])
Solve the algbraic equation vor y_LT
y_LT = solve(aeq, y_LT)
Inverse Laplace Transform
y_sol_LT = ilaplace(y_LT, s, t)
which is valid only for
, but can be extended to the general solt ion by multiplying it with ethe unit step.
y_sol_LT = y_sol_LT * heaviside(t)
How can I obtain this solution from the result calculated using dsolve?
Michael
Answers (2)
One approach simply tells simplify to keep going until it reaches the set limit (500 here), or can't simplify further.
Try something like this --
syms("t", "positive")
syms("T_1", "k_p", "positive")
syms("u(t)", "y(t)")
deq = T_1 * diff(y, t) + y == k_p * u
deq = subs(deq, u, heaviside(t))
y_sol_t = dsolve(deq, y(0) == 0)
y_sol_t = simplify(y_sol_t, 500)
syms("s")
aeq = laplace(deq, t, s)
syms("y_LT")
aeq = subs(aeq, [laplace(y(t), t, s) y(0)], [y_LT, 0])
aeq = aeq * laplace(heaviside(t))
y_LT = solve(aeq, y_LT)
y_sol_LT = ilaplace(y_LT, s, t)
y_sol_LT = y_sol_LT * heaviside(t)
Proof = isAlways(y_sol_t == y_sol_LT)
Plotting --
(Since isAlways cannot prove that they are the same, plotting is appropriate.)-
T_1 = rand
k_p = rand
figure
fplot(subs(y_sol_t), [-10 10])
grid
figure
fplot(subs(y_sol_LT), [-10 10])
grid
figure
fplot(subs(y_sol_t), [-10 10], LineWidth=3)
hold on
fplot(subs(y_sol_LT), [-10 10], LineWidth=1)
hold off
grid
.
syms("t", "positive")
syms("T_1", "k_p", "positive")
syms("u(t)", "y(t)")
deq = T_1 * diff(y, t) + y == k_p * u;
deq = subs(deq, u, heaviside(t));
y_sol_t = dsolve(deq, y(0) == 0)
y_sol_t = simplify(y_sol_t)
y_sol_t = rewrite(y_sol_t,'heaviside') % makes clear the solution is 0 for t < 0
To get the exact same result as from Laplace ... an easy, if not elegant, approach
simplify(y_sol_t/heaviside(t))*heaviside(t)
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!













