solving ODE by analytical & numerical solution
9 views (last 30 days)
Show older comments
I'm a newbie to matlab and im trying to solve an ODE analytically and numerically
The problem given is x''+0.5x'+x=0
clc; clear all;
syms x(t) t s
m=1; b=0.5; k=1;
t_final=2;
Dx = diff(x,t);
D2x = diff(x,t,2);
F1 = laplace(m*D2x+b*Dx+k*x,t,s)
x(t) = ilaplace((s+0.5)/(s^2+0.5*s+1)) %analytical solution
sys = tf([1 0.5],[1 0.5 1]) %numerical solution
First I tried to laplace transform the equation and since I dont know how to automatically inverse the eq. by code, I had to organize the eq. manually and just used ilaplace to inverse the eq. and get the soltion.
For the numerical solution, I actually had no idea how to solve the problem that way so I just copied the code that was on an example problem but failed to get the answer.
This was the example problem that I refered. It was a 'car speed cruise control model'. In this code, they just used sys=tf(1, [m b]) to get a numerical solution. 
My script just plots the eq on the laplace plane and doesnt match with the analytical solution
So my firtst question is are there any way to get a numerical solution just done by code? How can I get the Laplace transformed eq. to get organized and inversed back to get the answer by the code itself.
My second question is why does that single line of code [ sys = tf() ] works on the example script but doesnt work on mine. Perhaps can I be having the wrong toolbox? I have the "control system toolbox" added on.
2 Comments
Accepted Answer
Sam Chak
on 11 Oct 2023
Hi @승언 김
There are a few approaches to solving linear differential equations without the Control System Toolbox. Here is one of them. The ode object numerical method was introduced in release R2023b.
%% Analytical Method
syms x(t)
Dx = diff(x,1);
eqn = diff(x,2) + 0.5*Dx + x == 0;
cond = [x(0)==1, Dx(0)==0];
xSol(t) = dsolve(eqn, cond)
fplot(xSol, [0 20]), grid on, xlabel('t'), ylabel('x(t)')
title('Analytical Method using dsolve')
%% Numerical Method
F = ode;
F.ODEFcn = @(t, x) [x(2); % x1' = x2
- 0.5*x(2) - x(1)]; % x2' = - 0.5*x1' - x1
F.InitialValue = [1 0];
sol = solve(F, 0, 20)
plot(sol.Time, sol.Solution(1,:), "-o"), grid on, xlabel('t'), ylabel('x(t)')
title('Numerical Method using ODE object')
3 Comments
Sam Chak
on 11 Oct 2023
Q2: My second question is why does that single line of code "sys = tf()" works on the example script but doesnt work on mine. Perhaps can I be having the wrong toolbox?
The reason your script didn't work is because the transfer function of the system was specified incorrectly. The simulation problem aims to generate the time response to an initial condition without any external input signal. The step response corresponds to the system's response to the external Heaviside step signal,
. The Laplace transform of the Heaviside function is
. Therefore, to use the 'step()' command to simulate an input-free system, you need to modify the numerator of the transfer function so that the 's' variable will cancel out in the 'step()' command later.
. Therefore, to use the 'step()' command to simulate an input-free system, you need to modify the numerator of the transfer function so that the 's' variable will cancel out in the 'step()' command later.syms x(t) t s X(s)
% parameters
m = 1;
b = 0.5;
k = 1;
%% Analytical solution using Inverse Laplace method
Dx = diff(x,t);
D2x = diff(x,t,2);
Eqn = m*D2x + b*Dx + k*x == 0
LEqn = laplace(Eqn);
LEqn = subs(LEqn, {laplace(x(t), t, s), x(0), Dx(0)}, {X(s), 1, 0});
LEqn = isolate(LEqn, X(s))
x(t) = ilaplace(rhs(LEqn))
fplot(x, [0 20]), grid on, xlabel('t'), ylabel('x(t)'), ylim([-0.5 1])
title('Analytical Method using ilaplace()')
%% Numerical solution using Step Response
Glap = tf([2 1], [2 1 2])
s = tf('s');
Gaug = s*Glap % Augmented transfer function
tFinal = 20;
step(Gaug, tFinal), grid on, xlabel('t'), ylabel('x(t)')
title('Response to Initial Condition using step()')
More Answers (1)
Sam Chak
on 11 Oct 2023
You are welcome, @승언 김. If you find the solutions helpful, please consider clicking 'Accept' ✔ on the answer and voting 👍 for it. Thanks a bunch!
By the way, the reason your script for second ODE problem didn't work is because the first-order ODE function
was incorrectly specified as a second-order system,
,
. I have corrected it in 'F.ODEFcn'. Take a look below.
was incorrectly specified as a second-order system, syms x(t) t s
Dx = diff(x,1);
eqn = 0.1*Dx + x == 1;
cond = [x(0)==0];
xSol(t) = dsolve(eqn, cond)
tspan = [0 1];
fplot(xSol, tspan), grid on, xlabel('t'), ylabel('x(t)')
title('Analytical Method using dsolve')
F = ode;
F.ODEFcn = @(t, x) - 10*x + 10; % x' = - 10*x + 10
F.InitialValue = [0];
tFinal = 1;
sol = solve(F, 0, tFinal)
plot(sol.Time, sol.Solution, "-o"), grid on, xlabel('t'), ylabel('x(t)')
title('Numerical Method using ODE object')
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!









