Calculus II:  Mathematica Homework for Direction Fields and Euler's Method

Problem 1

Consider the initial value problem

(dy/dx) = 6  (x^2) - 3  (x^2) y,  
    y (0) = 3

(a) Construct a table that shows the Euler's method solution with step size 0.01 to estimate y(2.0). In your solution, provide a sketch of the points in the x y-plane.

(b) Construct the direction field for the differential equation for 0 < x < 3 and 1 < y < 4.

(c) Show the sketches from (a) and (b) on the same axes.

Barry's Solution

Part a

We need to implement Euler's method, which is the recursion relations:

(Subscript[x, 0]) = given
(Subscript[y, 0]) = given
h = given
(Subscript[x, n]) = (Subscript[x, n - 1]) + h
(Subscript[y, n]) = 
      (Subscript[y, n - 1]) + 
        h f ((Subscript[x, n - 1]), (Subscript[y, n - 1]))

Since we want to get out to x = 2 and we are starting at x = 0, we will need to go to n = ((2 - 0)/0.01) = 200.

These can be programmed into Mathematica in the following way:

Clear[x, y, f, h]
x[0] = 0
y[0] = 3
h = 0.01
f[x_, y_] = 6  x^2 - 3  x^2  y
x[n_] := x[n] = x[n - 1] + h
y[n_] := y[n] = y[n - 1] + h f[x[n - 1], y[n - 1]]
data = Table[{x[n], y[n]}, {n, 0, 200}];
TableForm[%]

Here is a plot of the data points:

dataplot = 
      ListPlot[data, PlotStyle -> {Red, PointSize[0.02]}, 
        ImageSize -> {500, Automatic}, 
        AxesLabel -> {"x", "y"}]

Part b

We need to load a package to get the direction field:

<< Graphics`PlotField`

field = 
      PlotVectorField[{1, 6  x^2 - 3  x^2  y}, {x, 0, 3}, {y, 1, 4}, 
        AspectRatio -> 1, Frame -> True, 
        ImageSize -> {500, Automatic}, 
        FrameLabel -> {"x", "y"}]

Part c

We can use the Show command to put the two together. Notice how the trajectory follows the arrows in the direction field.

Show[field, dataplot]

Problem 9.2.16

Consider the initial value problem

(dy/dx) = sin (x + y),  
    y (0) = 1

(a) Construct a table that shows the Euler's method solution with step size 0.005 to estimate y(3.0). In your solution, provide a sketch of the points in the x y-plane.

(b) Construct the direction field for the differential equation for 0 < x < 3 and 0 < y < 4.

(c) Show the sketches from (a) and (b) on the same axes.

Barry's Solution

Part a

We need to implement Euler's method, which is the recursion relations:

(Subscript[x, 0]) = given
(Subscript[y, 0]) = given
h = given
(Subscript[x, n]) = (Subscript[x, n - 1]) + h
(Subscript[y, n]) = 
      (Subscript[y, n - 1]) + 
        h f ((Subscript[x, n - 1]), (Subscript[y, n - 1]))

Since we want to get out to x = 2 and we are starting at x = 0, we will need to go to n = ((3 - 0)/0.005) = 600.

These can be programmed into Mathematica in the following way:

Clear[x, y, f, h]
x[0] = 0
y[0] = 1
h = 0.005
f[x_, y_] = Sin[x + y]
x[n_] := x[n] = x[n - 1] + h
y[n_] := y[n] = y[n - 1] + h f[x[n - 1], y[n - 1]]
data = Table[{x[n], y[n]}, {n, 0, 600}];
TableForm[%]

Here is a plot of the data points:

dataplot = 
      ListPlot[data, PlotStyle -> {Red, PointSize[0.02]}, 
        ImageSize -> {500, Automatic}, 
        AxesLabel -> {"x", "y"}]

Part b

We need to load a package to get the direction field (you only need to load this once, so if you did this for eaqrlier problems you can skip it here):

<< Graphics`PlotField`

field = 
      PlotVectorField[{1, Sin[x + y]}, {x, 0, 3}, {y, 0, 4}, 
        AspectRatio -> 1, Frame -> True, 
        ImageSize -> {500, Automatic}, 
        FrameLabel -> {"x", "y"}]

Part c

We can use the Show command to put the two together. Notice how the trajectory follows the arrows in the direction field.

Show[field, dataplot]

Problem 3

Consider the initial value problem

(dy/dx) = sin ((x^2) + (y^3)),  
    y (0) = 1.85

(a) Construct a table that shows the Euler's method solution with step size 0.005 to estimate y(3.0). In your solution, provide a sketch of the points in the x y-plane.

(b) Construct the direction field for the differential equation for 0 < x < 3 and 1.2 < y < 2.2.

(c) Show the sketches from (a) and (b) on the same axes.

Barry's Solution

Part a

We need to implement Euler's method, which is the recursion relations:

(Subscript[x, 0]) = given
(Subscript[y, 0]) = given
h = given
(Subscript[x, n]) = (Subscript[x, n - 1]) + h
(Subscript[y, n]) = 
      (Subscript[y, n - 1]) + 
        h f ((Subscript[x, n - 1]), (Subscript[y, n - 1]))

Since we want to get out to x = 2 and we are starting at x = 0, we will need to go to n = ((3 - 0)/0.005) = 600.

These can be programmed into Mathematica in the following way:

Clear[x, y, f, h]
x[0] = 0
y[0] = 1.85
h = 0.005
f[x_, y_] = Sin[x^2 + y^3]
x[n_] := x[n] = x[n - 1] + h
y[n_] := y[n] = y[n - 1] + h f[x[n - 1], y[n - 1]]
data = Table[{x[n], y[n]}, {n, 0, 600}];
TableForm[%]

Here is a plot of the data points:

dataplot = 
      ListPlot[data, PlotStyle -> {Red, PointSize[0.02]}, 
        ImageSize -> {500, Automatic}, 
        AxesLabel -> {"x", "y"}]

Part b

We need to load a package to get the direction field (you only need to load this once, so if you did this for eaqrlier problems you can skip it here):

<< Graphics`PlotField`

field = 
      PlotVectorField[{1, Sin[x^2 + y^3]}, {x, 0, 3}, {y, 1.2, 2.2}, 
        AspectRatio -> 1, Frame -> True, 
        ImageSize -> {500, Automatic}, 
        FrameLabel -> {"x", "y"}]

Part c

We can use the Show command to put the two together. Notice how the trajectory follows the arrows in the direction field.

Show[field, dataplot]

Problem 4

Consider the initial value problem

(dy/dx) = ((ln (x*y))/(1 + (x^2))),  
    y (1) = 1

(a) Construct a table that shows the Euler's method solution with step size 0.005 to estimate y(1.0). In your solution, provide a sketch of the points in the x y-plane.

(b) Construct the direction field for the differential equation for 0 < x < 2 and 0 < y < 2.

(c) Show the sketches from (a) and (b) on the same axes.

Barry's Solution

Part a

We need to implement Euler's method, which is the recursion relations:

(Subscript[x, 0]) = given
(Subscript[y, 0]) = given
h = given
(Subscript[x, n]) = (Subscript[x, n - 1]) + h
(Subscript[y, n]) = 
      (Subscript[y, n - 1]) + 
        h f ((Subscript[x, n - 1]), (Subscript[y, n - 1]))

Since we want to get out to x = 2 and we are starting at x = 0, we will need to go to n = ((1 - 0)/0.005) = 200.

These can be programmed into Mathematica in the following way:

Clear[x, y, f, h]
x[0] = 1
y[0] = 1
h = 0.005
f[x_, y_] = Log[(x*y)^2]/(1 + (x^2))
x[n_] := x[n] = x[n - 1] + h
y[n_] := y[n] = y[n - 1] + h f[x[n - 1], y[n - 1]]
data = Table[{x[n], y[n]}, {n, 0, 200}];
TableForm[%]

Here is a plot of the data points:

dataplot = 
      ListPlot[data, PlotStyle -> {Red, PointSize[0.02]}, 
        ImageSize -> {500, Automatic}, 
        AxesLabel -> {"x", "y"}]

Part b

We need to load a package to get the direction field (you only need to load this once, so if you did this for eaqrlier problems you can skip it here):

<< Graphics`PlotField`

field = 
      PlotVectorField[{1, Log[(x*y)^2]/(1 + (x^2))}, {x, 0, 2}, {y, 0, 
          2}, AspectRatio -> 1, Frame -> True, 
        ImageSize -> {500, Automatic}, 
        FrameLabel -> {"x", "y"}]

Part c

We can use the Show command to put the two together. Notice how the trajectory follows the arrows in the direction field.

Show[field, dataplot]