prepared by Mark E. Johnson, maejohns@math.princeton.edu

MAE 305

Mathematics in Engineering I


Mathematica: The Laplace Transform


Introduction

On this page, I hope to show you how to use Mathematica to perform the Laplace Transform and the Inverse Laplace Transform ; moreover, by the end, I hope you will be able to solve a second order differential equations using these methods.

To run Mathematica, proceed as directed on the previous tutorial. Do not forget to set the DISPLAY variable as directed there! The main new commands we will be using here are called LaplaceTransform and InverseLaplaceTransform. However, if you run Mathematica and try to get help on one of them, say by typing ?LaplaceTransform, you should get a message saying that the symbol was not found. This is because the LaplaceTransform is not a default Mathematica command. In order to teach Mathematica about the Laplace Transform, we have to tell it to read the system folder in which the relevant commands are defined. So, issue the following command each time you start Mathematica and plan to use the LaplaceTransform:

In[1] := << Calculus`LaplaceTransform`
To save space, I will avoid including the output which Mathematica returns ; I advise you to read what it returns to make sure there are no mistakes.

The Laplace Transform

Given a function f(t) of the positive real numbers, t >= 0, we can define a new function (of s), called the Laplace Transform of f, as

We can use Mathematica to evaluate this integral for many relevant choices of f. The command is defined with the following syntax: LaplaceTransform[f[t],t,s].

Some easy examples

Here are some simple examples to try:
LaplaceTransform[Sin[a t],t,s]

LaplaceTransform[t^2,t,s]

LaplaceTransform[t^3,t,s]

LaplaceTransform[a t^2 + b t^3,t,s]

Easy enough.

A tricky example

Here is an example which involves the Laplace Transform, but thanks to a mathematical fact, can be reduced to simply evaluating an integral.

The purpose of introducing you to this example is primarily to illustrate some new ways of thinking when using Mathematica and to teach you some new commands. Let's get started:

Suppose the function f(t) is periodic of period P. By this, I mean that the f(t)=f(t+P), for all real numbers t. Then it can be shown that the Laplace Transform is equal to

Now, let us use Mathematica to find the Laplace Transform of f(t), which we define to be f(t)=1-t if 0 <= t <= 1 and f(t)=f(t-1) if t>1. First we define the function and plot it for our own good.

In[3] := Clear[f]
In[4] := f[x_] := 1-x /; 0 <= x <=1
In[5] := f[x_] := f[x-1] /; x > 1
In[6] := Plot[f[x],{x,0,5},PlotRange->{0,3/2},Ticks->{Automatic,{0,.5,1,1.5}}]
NOTE that before beginning Mathematica you should have set the DISPLAY variable as was discussed in the other tutorials!! If you don't, then you will not see any graphics output.

Now, using the above equation with P=1 and f(t)=1-t, we can arrive at the Laplace Transform through the following sequence of commands:

In[7] := stepone = Integrate[(1-t)Exp[-s t],{t,0,1}]
In[8] := lf = 1/(1-Exp[-s]) stepone//Simplify
In line 7, we evaluated the integral using Mathematica and called the answer stepone. In line 8, we combined two Mathematica commands into one line by using the double slash (//) notation : first stepone was multiplied by the term which appears in front of the integral, then second, we Simplify'd the expression to get the final answer, namely lf.

Inverse Laplace Transform

Suppose we would like to find the inverse Laplace transform of 1/(8+4s+s^2). We can use Mathematica by issuing the following command:
In[10] := InverseLaplaceTransform[1/(8+4s+s^2),s,t]
To avoid having to retype InverseLaplaceTransform every time you want to perform the inverse transform, I suggest creating an alias, such as ILT by simply performing the assignment
In[11] := ILT = InverseLaplaceTransform
within Mathematica. Now, we can proceed as before but without having to do as much typing. Nice!
In[12] := ILT[1/(8+4s+s^2),s,t]
Note: You do not have to use ILT as your alias. Feel free to use whatever you want ; I will continue to use ILT throughout the remainder of this handout.

Some easy examples

Let's try some easy example:
ILT[1/s^2,s,t]

ILT[s/(s^2+4),s,t]

ILT[a s/(s^2+4) - b/(s^2-4),s,t]


Getting Tricky

Let's again get a little tricky. Suppose we would like to compute the inverse Laplace Transform of

1/(s^n (s*s+16)), for n=0,1,2,3,4,5,6.

A good exercise for you is to show that if F(n) denotes the inverse Laplace transform for 1/(s^n(s*s+16)), then one can compute the F(n+1) by the following formula:

Thus we can set up a recursion relation in Mathematica as follows:

In[13] := Clear[F]
In[14] := F[0] = ILT[1/(s^2+16),s,t]
In[15] := F[n_] := F[n] = Integrate[F[n-1] /. t->a, {a,0,t}]
We end this exercise by printing out the results in table form:
In[16] := Table[{n,F[n]},{n,0,6}]//TableForm
Notice again here that we used the double-slash notation to take the output of the first command and use it as input to the second command, TableForm.

Solving second order equations

Finally, let's use Mathematica to solve a differential equations via the Laplace Transform.

For the first problem, let's solve y''+2y'+4y=t-exp(-t), subject to y(0)=1 and y'(0)=-1.

In[17] := lhsone=LaplaceTransform[y''[t]+2y'[t]+4y[t],t,s]
After taking the Laplace Transform of the differential equation, we can plug in the initial values and make a substitution for the Transform of y (usually denoted capital Y, which, remember, is still unknown) ; we do this with the following command:
In[18] := lhstwo = lhsone /. {LaplaceTransform[y[t],t,s]->capy,y[0]->1,y'[0]->-1}
Now we compute the transform of the right hand side:
In[19] := rhs=LaplaceTransform[t-Exp[-t],t,s]
and solve for capital y.
In[20] := stepthree=Solve[lhstwo==rhs,capy]
Now we must extract the solution from all of the surrounding brakets with the following command:
In[21] := stepfour=stepthree[[1,1,2]]
so we can then compute the inverse transform and find the solution to our problem!
In[22] := sol=ILT[stepfour,s,t]

The above six steps can be used as a general recipe for solving differential equations using the Laplace Transform, provided Mathematica knows the transform of the left annd right hand side. In some cases, you may have to do some of the work on your own, "by hand", before proceeding.

A step function example

Let us consider the same example above, but with a different right hand side. In particular, we will use the step function which is equal to one when x is greater than 5 and zero when x is less than 5. This function is known as u_5 in your textbook.

We can define a function like this in Mathematica with the command

f[x_] := If[x<=5,0,1]
Plot[f[x],{x,0,7}]
However, if we try to use Mathematica to compute the Laplace Transform of this function, we see that it fails to do anything useful:
In[33]:= LaplaceTransform[f[t],t,s]

Out[33]= LaplaceTransform[If[t <= 5, 0, 1], t, s]

Thus, in order to solve the differential equation from above with this right hand side, we will have to either appeal to the tables from our textbook to compute the Laplace Transform for this function on our own, or we have Mathematica to the integral for us as follows:
In[34]:= rhs = Integrate[ Exp[-s t],{t,5,Infinity}]

           1
Out[34]= ------
          5 s
         E    s

Had we put in the expression Integrate[ Exp[-s t] f[t],{t,0,Infinity}] we would not have received an answer ; a little of our own work was required.

Assuming you have not cleared the variables that were defined above, we can now solve the problem with the following commands:

In[35]:= stepthree=Solve[lhstwo==rhs,capy];
In[36]:= stepfour=stepthree[[1,1,2]];
In[37]:= sol=ILT[stepfour,s,t]
                                5 - t
         Cos[Sqrt[3] t]    1   E      Cos[Sqrt[3] (-5 + t)]
Out[37]= -------------- + (- - ---------------------------- - 
                t          4                4
               E
 
         5 - t
        E      Sin[Sqrt[3] (-5 + t)]
>       ----------------------------) UnitStep[-5 + t]
                 4 Sqrt[3]

In[38]:= Plot[sol,{t,-1,15}]  

AAAAAHHHH-HAAAAHHH!!! I lied! In this problem, we actually do not have to do any of the above work by hand, since we just discovered the Mathematica function called UnitStep. So, we now reproduce everything we have just done, but with this command instead.

lhsone=LaplaceTransform[y''[t]+2y'[t]+4y[t],t,s]
lhstwo = lhsone /. {LaplaceTransform[y[t],t,s]->capy,y[0]->1,y'[0]->-1}
rhs=LaplaceTransform[UnitStep[t-5],t,s]
stepthree=Solve[lhstwo==rhs,capy]
stepfour=stepthree[[1,1,2]]
sol=ILT[stepfour,s,t]
Plot[sol,{t,-1,15}]  
and there you have it. With this information, you should be able to easily solve a lot of homework-type problems just by using Mathematica!
Please feel free to send me email if you have any questions.