solving ODE by analytical & numerical solution
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
Dyuman Joshi
on 11 Oct 2023
Is it necessary to use laplace transform to solve the ODE?
승언 김
on 11 Oct 2023
Accepted Answer
More Answers (1)
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')
Categories
Find more on Programming 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!










