euler.nb 1 Euler Method Mathematica Lab After a brief introduction and warm-up exercise on the basics of Mathematica we create a small routine for approximating the solution to a first order ordinary differential equation using the Euler method. The first thing you will probably notice about Mathematica is that it is finickey: it is case sensitive and things must be typed with proper syntax. Aside from being a symbolic math package, Mathematica is a full programming language. The workspace is divided into cells. All commands in a cell are evaluated by typing SHIFT+RETURN. It is generally best to use lower case letters for variables since many upper case letters are reserved for operators. Operators start with capital letters and expressions being operated on go inside square brackets: Sin[x] or Factor[x^2-4]. Use a space for multiplication so f r e d is f times r times e times d whereas fred is recognized as a single variable. The help browser is extremely useful. Click on the small triangle at the bottome to see the further examples. I usually look there first for a problem similiar to the one I'm trying to solve. I generally copy and paste the example into my worksheet and modify it for my problem. Here are some examples of what you can do with Mathematica: Examples of some basic tasks using Mathematica. In the following cells I multiply 4 and 17, factor x^2-6x+9, compute the derivative of (x^2+Cosh[x])/Exp[Sin[x]], and find the integral of Exp[a x] Sin[b x] (I got a complex answer so I use the Simplify command to get a simpler answer). Try them out yourself! 4 17 68 Factor!x ^ 2 ! 6"x # 9" !!3 " x"2 D!#x ^ 2 # Cosh!x"$ % Exp!Sin!x"", x" !#!Sin#x$ Cos#x$ !x2 " Cosh#x$" " #!Sin#x$ !2 x " Sinh#x$" Integrate!Exp!a x" Sin!b x", x" a #a x Sin#b x$ b #a x Cos#b x$ ! $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$ " $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$ !a ! % b" !a " % b" !a ! % b" !a " % b" euler.nb 2 Simplify!Integrate!Exp!a x" Sin!b x", x"" !a x !"b Cos"b x# # a Sin"b x#$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$ a2 # b2 The command Clear[x,y,t,f] clears all previous assignments to the variables x, y, t, and f. Example: x ! fred fred x fred Clear!x" x x The strange notation /. x!3 is used to evaluate an expression at 3. Example: Sin!x" Exp!"3 x" #. x # "2 Pi # 3 1 %&&& " $$$$ 3 !2 % 2 Using Mathematica to solve algebraic and differential equations analytically. Mathematica can be used to find solutions to equations. Algebraic equations are solved using Solve and differential equations are solved using DSolve. Notice double equal signs are used for conditional equalities. Examples: Solve!x ^ 2 $ 3%x $ 3 & 0, x" 1 1 %&&& %&&& ''x & $$$$ ("3 " ' 3 )*, 'x & $$$$ ("3 # ' 3 )** 2 2 Here's the equation y' = - y. We get the general solution with arbitrary constant C[1]. It is important to clear all variables before using DSolve. Clear!y, t" DSolve!y '!t" & "y!t", y!t", t" ++y"t# & !"t C"1#,, Here is an initial value problem y' = - y, y(0)=2: euler.nb 3 Clear!y, t" DSolve!#y '!t" ! "y!t", y!0" ## 2$, y!t", t" !!y"t# ! 2 "#t $$ We can use Plot to make a graph of a solution curve; I copied and pasted the output from the previous example into the slot for the expression: Plot!2 $"t , #t, 0, 5$" 2 1.5 1 0.5 1 2 3 4 5 Implementing Euler's method for solving an ODE numerically. We can build routines using loops to carry out iterative procedures such as implementing the Cauchy-Euler algorithm. Look in the help browser for Programming/Flow Control for instructions on using the commands Do, While, etc. In the following example I made a do loop to approximate the value of y[1] four the initial value problem y' = 1- t + 4 y, y[0]=1. I took h = 0.01 so there are (1-0)/0.01 = 100 iterates in this example. In other words, the y-value we seek is y[99+1]. Notice that in this example the initial value is given at t=0 so t = 0+0.01 n = 0.01 n in our algorithm. In[119]:= Clear!y" y!0" # 1; Do!y!n % 1" # y!n" % .01 %1 " .01 n % 4 y!n"&, #n, 0, 99$" y!100" Out[122]= 60.0371 The approximate value of y[1], using h=.01, is 60.0371. We can make a table of the approximate solution values (t,y(t)) and use them to plot the approximate solution curve using ListPlot to graph a curve through these points. (I named the graph "a" so I can use it again later.) The first line makes the table and gives it the clever name 'table'. The second line plots the points and joins them with a curve. If we need some of the values (for the homework, for instance) we can read them off of the table. euler.nb 4 In[123]:= table ! Table!"N!.01 k#, y!k#$, "k, 0, 100$# a ! ListPlot!table, PlotJoined " True# Out[123]= !!0., 1", !0.01, 1.05", !0.02, 1.1019", !0.03, 1.15578", !0.04, 1.21171", !0.05, 1.26978", !0.06, !0.11, !0.16, !0.21, !0.26, !0.31, !0.36, !0.41, !0.46, !0.51, !0.56, !0.61, !0.66, !0.71, !0.76, !0.81, !0.86, !0.91, !0.96, 1.33007", !0.07, 1.39267", !0.08, 1.45768", !0.09, 1.52518", !0.1, 1.59529", 1.6681", !0.12, 1.74373", !0.13, 1.82227", !0.14, 1.90387", !0.15, 1.98862", 2.07667", !0.17, 2.16813", !0.18, 2.26316", !0.19, 2.36188", !0.2, 2.46446", 2.57104", !0.22, 2.68178", !0.23, 2.79685", !0.24, 2.91642", !0.25, 3.04068", 3.16981", !0.27, 3.304", !0.28, 3.44346", !0.29, 3.5884", !0.3, 3.73903", 3.8956", !0.32, 4.05832", !0.33, 4.22745", !0.34, 4.40325", !0.35, 4.58598", 4.77592", !0.37, 4.97336", !0.38, 5.17859", !0.39, 5.39193", !0.4, 5.61371", 5.84426", !0.42, 6.08393", !0.43, 6.33309", !0.44, 6.59211", !0.45, 6.8614", 7.14135", !0.47, 7.43241", !0.48, 7.735", !0.49, 8.0496", !0.5, 8.37669", 8.71675", !0.52, 9.07032", !0.53, 9.43794", !0.54, 9.82015", !0.55, 10.2176", 10.6308", !0.57, 11.0604", !0.58, 11.5071", !0.59, 11.9716", !0.6, 12.4546", 12.9567", !0.62, 13.4789", !0.63, 14.0219", !0.64, 14.5864", !0.65, 15.1735", 15.7839", !0.67, 16.4187", !0.68, 17.0787", !0.69, 17.7651", !0.7, 18.4788", 19.2209", !0.72, 19.9927", !0.73, 20.7952", !0.74, 21.6297", !0.75, 22.4975", 23.3999", !0.77, 24.3383", !0.78, 25.3141", !0.79, 26.3289", !0.8, 27.3841", 28.4815", !0.82, 29.6227", !0.83, 30.8094", !0.84, 32.0434", !0.85, 33.3268", 34.6614", !0.87, 36.0492", !0.88, 37.4925", !0.89, 38.9934", !0.9, 40.5542", 42.1774", !0.92, 43.8654", !0.93, 45.6208", !0.94, 47.4463", !0.95, 49.3448", 51.3191", !0.97, 53.3722", !0.98, 55.5074", !0.99, 57.7279", !1., 60.0371"" 60 50 40 30 20 10 0.2 0.4 0.6 0.8 1 Out[124]= !"Graphics"! How did we do? We can plot the actual integral curve and the approximate integral curve on the same set of axes. I used DSolve to solve the equation then pasted the result into the Plot command. I called the approximate plot 'a' and the actual plot 'b'. The command Show combines the graphics. Clear!y, x# DSolve!"y '!x# !! 4 y!x# # 1 $ x, y!0# !! 1$, y!x#, x# 1 ##y$x% # $$$$$$$ &%3 & 19 '4 x & 4 x'(( 16 euler.nb 65 b ! Plot! """"""" "#3 $ 19 %4 x $ 4 x#, $x, 0, 1%& 16 y!0" ! 0; Do!y!n " 1" ! y!n" " .05 #3 #1 " .05 n$ ^ 2 % #3 y!n" ^ 2 # 4$$, &n, 0, 15'" 60 y!16" 50 table ! Table!&N!1 " .05 k", y!k"', &k, 0, 16'" ListPlot!table, PlotJoined $ True" 1 table" In[131]:= Clear!y, 40 Out[134]= !3.0981 30 Out[135]= !!1., 0", !1.05, !0.0375", !1.1, !0.0788874", !1.15, !0.124475", !1.2, !0.174652", 20 !1.25, !0.229916", !1.3, !0.290929", !1.35, !0.3586", !1.4, !0.434238", 10 !1.45, !0.519845", !1.5, !0.618731", !1.55, !0.737089", !1.6, !0.88914", !1.65, !1.12497", !1.7, !3.13341", !1.75, !3.11638", !1.8, !3.0981"" 0.2 Show'a, b( -0.5 0.4 1.2 0.6 0.8 1.6 0.6 0.8 1.4 1 1.8 60-1 -1.5 50 40-2 -2.5 30 20-3 10 Out[136]= "#Graphics#" 0.2 0.4 1 Your turn! Modify the above code to solve the following problems: Another example. As another example let's solve y'=3t^2/(3y^2-4), y(1)=0 for y(1.8) using h=0.05. Here n will Consider the initial value problem 1.1.Apply Euler's method to solve y'[t] = y[t] (2-y[t]), y[0]=0.1 for y[4]. Use h=0.2. Make a go from 0 to (1.8-1)/0.05 = 16. Here our initial value is at t=1 so t=1+0.05 n. Notice, we are y’[t]curve. = 1-y[t], y[0]What = 2. should the range of n be to get the table and plot the approximate solution (Hint: computing y[n]'s not y[t]'s. Inofthis example y[16]thecorresponds to the y-value at t=1.8. a) Use separation variables to find solution. solution at t=4 starting at t=0?) b) Apply Euler’s method (in Mathematica) to plot the approximate solution for t = 0 to t = 4. Use a step size of 0.1 Plot both the solution and approximate solution on 2. y'[t] = -0.5 the y-t+1.5, y(0)=1 for y(3). Use h=0.01. Make a table (put a semicolon after the same set of axes. table linec) to hide it's actual big!) and plot theWhat approximate solution curve. Solve the using equation Whatit is- the value y(4)? is approximate value of y(4) (from using DSolve.Euler’s Plot both the solution and What approximate solution on the same set of axes. method in part b))? is the relative error? 2. Consider the initial value problem y’[t] = cos(t)-y[t], y[0] = 5. a) Apply Euler’s method (in Mathematica) to plot the approximate solution for y[8]. Start with a step size of 1, then use successively smaller step sizes. Report what happens in these trials. Discuss how close your approximate solution is to the Solutions: actual solution and how the error changes as the step size decreases. Problem 1.