# Preface

This is a tutorial made solely for the purpose of education and it was designed for students taking Applied Math 0330. It is primarily for students who have very little experience or have never used Mathematica before and would like to learn more of the basics for this computer algebra system. As a friendly reminder, don't forget to clear variables in use and/or the kernel.

Finally, the commands in this tutorial are all written in bold black font, while Mathematica output is in normal font. This means that you can copy and paste all commands into Mathematica, change the parameters and run them. You, as the user, are free to use the scripts for your needs to learn the Mathematica program, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately.

# Numerical Solutions

Although sometimes Mathematica is able to solve a differential equation, in practice we tend to rely on numerical methods to solve differential equations. Most differential equations that are found in actual practice are much too complicated to express a solution using elementary or special functions. Usually, we want to obtain a solution in implicit form, but unfortunately this only happens occasionally. It is important to note that because a differential equation does not have an explicit or implicit solution does not necessarily mean that the solution does not exist.

Since the kernel of a differential operator usually contains a space of functions, we do not expect to obtain a general solution that includes arbitrary constants. Instead, we knock out one solution by considering the initial value problem (IVP for short)

$y' = f(x,y), \qquad y(x_0 ) = y_0 ,$
which is assumed to have a unique solution.

Example: Consider the initial value problem $$y' = \sin \left( x^2 \right) , \quad y(0) =0 .$$ We try to find its solution using the standard Mathemaica command DSolve:

exactsol = DSolve[{y'[x] == Sin[x^2], y[0] == 0}, y[x], x]
Out[1]= {{y[x] -> Sqrt[\[Pi]/2] FresnelS[Sqrt[2/\[Pi]] x]}}
Mathematica represents the solution through a special function, called the Fresnel function: $$S(x) = \int_0^x \sin \left( t^2 \right) {\text d}t .$$
?FresnelS
FresnelS[z] gives the Fresnel integral S(z).  >>

Now we solve the same problem numercally using NDSolve.

numsol = NDSolve[{y'[x] == Sin[x^2], y[0] == 0}, y[x], {x, 0, 10}]
Out[3]= {{y[x] -> InterpolatingFunction[{{0., 10.}}],<>],[x]}}
Plot[y[x] /. numsol, {x, 0, 10}, PlotRange -> All]
This is a plot of the numerical solution. Now we calculate its approximate value at x=6.5 as follows:
numsol /. x -> 6.5
Out[5]= {{y[6.5] -> 0.639918}}

This indicates that y(6.5) approximately equal to 0.639918. We can improve accuracy by choosing the option PrecisionGoal

There are two main approaches to finding a numerical value for the solution to the initial value problem $$y' = f(x,y), \quad y( x_0 )= y_0 .$$ Its solution can be obtained using either DSolve (for solutions represented using known functions, if it is possible) or NDSolve (for numerical solutions). The first one is based on definition of the slope function f(x,y) as an expression. The second one (which is recommended) is to define the slope as a function. To see how it works, let us consider an example.

Example: : Consider the slope function f(x,y) = 1/(3*x - 2*y + 1). Then we find a numerical value treating f(x,y) as an expression.

fp = 1/(3*xinit - 2*y[xinit] + 1)
Out[4]= 1/(1 + 3 xinit - 2 y[xinit])
f = DSolve[{y'[xinit] == fp, y[0] == 0}, y, xinit]
Out[5]= {{y -> Function[{xinit}, 1/6 (1 + 9 xinit - 2 ProductLog[1/2 E^(1/2 + (9 xinit)/2)])]}}
xinit = 1.2
Out[6]= 1.2
yvalue = f[[1]][[1]][[2]][[2]]]       (* this is the value of the solution at the point x=1.2 *)
Out[7]= 0.68101

Now we use the function DSolve to find the explicit solution:

DSolve[{y'[x] == 1/(3*x - 2*y[x] + 1), y[0] == 0}, y[x], x]

Solve::ifun: Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information. >>
Out[1]= {{y[x] -> 1/6 (1 + 9 x - 2 ProductLog[1/2 E^(1/2 + (9 x)/2)])}}
Now we define solution as a function of x:
f[x_] = y[x] /. %[[1]]
Out[2]= 1/6 (1 + 9 x - 2 ProductLog[1/2 E^(1/2 + (9 x)/2)])
f[1.2]
Out[3]= 0.68101

We present two other options to find a numerical value of a solution to the initial value problem using NDSolve.

NDSolve[{y'[x] == x^2 - (y[x])^2, y[0] == 1}, y[x], {x, 0, 1.4}]
Out[1]= {{y[x] -> InterpolatingFunction[{{0.,1.4}}, <>] [x]}}
fsol[x_] = y[x] /. %[[1]]
Out[2]= InterpolatingFunction[{{0.,1.4}}, <>] [x]
fsol[0.4]
Out[3]= 0.732727
or
sol = NDSolve[{y'[x] == Sqrt[4*x + y[x] - 1], y[1] == 1}, y, {x, 1, 3}]
y[x] /. sol /. x -> 3
Out[3]= 7.456545294165829
The general reference for NDSolve applications is

As we see applications of NDSolve are very straightforward --- use the solution "interpolating function" as a regular function. When plotting the solution, Mathematica recommends to use the Evaluation. We give some examples.

Example: First, we solve a first-order ordinary differential equation, which gives you decaying oscillations:

sol1 = NDSolve[{y'[x] == y[x] Sin[2 x + y[x]], y[0] == 1}, y, {x, 0, 30}];
Now we use it inside another equation. The important thing to remember is that the domain of the previous equations should be equal to or contain the domain of the next equations.
sol2 = NDSolve[{z'[x]== -z[x]^2+First[Evaluate[y[x] /. sol1]], z[0] == 0}, z, {x, 0, 30}]
Then we plot both solutions. Indeed, first solution here plays the role of sort of a driving force, so on the graphs below you see synchronization between oscillations.
Plot[{Evaluate[y[x] /. sol1], Evaluate[z[x] /. sol2]}, {x, 0, 30}, PlotRange -> All]
Now you can do more advanced things, like using also derivative of the "Interpolating Function" inside of new equations:
sol3 = NDSolve[{y'[x] == y[x] Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}];
sol4 = NDSolve[{z'[x]== -z[x]+First[y[x]/5 + y'[x] /. sol3], z[0] == 0}, z, {x, 0, 30}];
Plot[{Evaluate[y[x] /. sol3], Evaluate[z[x] /. sol4]}, {x, 0, 30}, PlotRange -> All, Filling -> 0]