%% Symbolic Laplace Transforms
% How to use Matlab Symbolics Toolbox to work with Laplace Transforms
%
% BJ Furman
%
% 16SEP2014
%% Laplace Transform of a function of time
%
clear all;
syms s t; % define the symbolic variables s and t
ft = 3*sinh(2*t) + 3*sin(2*t); % form f(t)
Fs = laplace(ft) % find the Laplace transform!
%% Inverse Laplace Transform
%
syms s t; % define the symbolic variables s and t
ft = 3*sinh(2*t) + 3*sin(2*t); % form f(t)
Fs = laplace(ft); % find the Laplace transform!
f_t = ilaplace(Fs)
simplify(f_t)
simplify((3*exp(2*t))/2 - (3*exp(-2*t))/2)
%% Solve a differential equation symbolically using Laplace Transform
%
% Consider the first order ODE describing a simple open-loop cruise control
% (m/b)*dV(t)/dt + V(t) = (Ke/b)*Theta0
% Let the symbolic mathematics capability of Matlab do all the work:
% Constants for the physical system
m = 1000; % kg
b = 50; % N-s/m
tau = m/b; % the time constant
g = 9.81; % m/s^2
Theta0 = 30*pi/180; % radians
Ke = 500/Theta0; % N/rad
K = (Ke/b)*Theta0;
v0 = 0; % initial velocity at t=0-
% Do the symbolic operations
syms s t Vs V(t) % define the symbolic variables s, t, and V(t)
Vdot(t) = diff( V(t),t ); % take the derivative of V(t)
eq1(t) = tau*Vdot(t) + V(t) - K;
L1(t) = laplace(eq1(t),t,s); % take the Laplace transform of eq1(t). Remove the semicolon to see what this generates
L1 = subs(L1(t),V(0),v0); % substitute the initial condition, V(0)=0. Remove the semicolon to see what this generates
L2 = subs(L1, laplace(V(t), t, s), Vs); % substitute the symbolic variable Vs for the transformed V(t). Remove the semicolon to see what this generates
L2 = solve(L2,Vs); % solve for V(s). Remove the semicolon to see what this generates
V = ilaplace(L2) % take the inverse Laplace transform
ezplot(V,[0 100])
%% Alternate way to solve a differential equation symbolically using Laplace Transform
%
% Consider the first order ODE describing a simple open-loop cruise control
% (m/b)*dV(t)/dt + V(t) = (Ke/b)*Theta0
% This example has the programmer do some of the Laplace transform work:
% Constants for the physical system
m = 1000; % kg
b = 50; % N-s/m
tau = m/b; % the time constant
g = 9.81 % m/s^2
Theta0 = 30*pi/180; % radians
Ke = 500/Theta0; % N/rad
K = (Ke/b)*Theta0;
v0 = 0; % initial velocity at t=0-
syms s t Vs % define the symbolic variables s, t, and Vs
ft = K; % (RHS of ODE) forcing function f(t) is just the step value, K, a constant for this problem
Fs = laplace(ft,t,s); % take the Laplace transform of f(t)
V1s = s*Vs - v0; % the Laplace transform of dV(t)/dt
eq2 = tau*V1s + Vs - Fs; % the Laplace transform of the full ODE with RHS brought to the LHS
sol = solve(eq2, Vs);
Vt = ilaplace(sol,s,t)
ezplot(Vt,[0 100]) % plots the function over the range 0<= t <= 100
%% Another way to symbolically solve an ODE using dsolve
%
eq3 = '20*Dv + v = 10'; % the ODE expressed as a string
init = 'v(0)=0'; % the initial condition
vt = dsolve(eq3,init,'t') % solves the ODE symbolically
ezplot(vt,[0 100]) % plots the function over the range 0<= t <= 100
%% Turn a symbolic result into a function that can generate a numeric solution
%
% Use the Matlab function, matlabFunction()
%
% matlabFunction() can:
%
% * generate an 'anonymous' function, or
% * a function defined as a file.
%
% An anonymous functon is a function that is NOT stored in a program file, but
% is associated with a variable whose data type is function_handle.
% Anonymous functions can accept inputs and return outputs, just as
% standard functions do. However, they can contain only a single executable
% statement.
%
Vt_num = matlabFunction(Vt) % converts the symbolic function Vt into an anonymous numeric function, Vt_num(t)
t=0:0.1:100; % give t some values of time
plot(t, Vt_num(t)) % plot the results
grid
%%