+ All Categories
Home > Documents > Math 216 - Assignment 2 - First Order Differential Equationsfmassey/math216/Assignments... ·...

Math 216 - Assignment 2 - First Order Differential Equationsfmassey/math216/Assignments... ·...

Date post: 26-May-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
13
1 Math 216 - Assignment 2 - First Order Differential Equations Due: Monday, February 12. Nothing accepted after Tuesday, February 13. This is worth 15 points. 10% points off for being late. You may work by yourself or in pairs. If you work in pairs it is only necessary to turn in one set of answers for each pair. Put both your names on it. Do Problems 1 - 7 in the discussion below. Use one or more Live Scripts to hold your input and output. Label the work for each problem: Problem 1, Problem 2, etc. Print a copy of the file(s) to hand in. Live Scripts So far, we have seen how to execute commands in the Command Window. There is an alternative that is more convenient in many ways such as when one is correcting errors or when one needs to execute a series of commands or one expects to repeatedly use and modify several commands. Live Scripts allow one to write and save these commands. Live Script files are created and edited using the MATLAB editor. Let's write a Live Script that finds the volume and surface area of a cylinder of radius r and height h. We shall use the formulas V = r 2 h and A = 2r 2 + 2rh. We begin by clicking on New on the tool bar and selecting Live Script on the drop down menu. The Editor Window will appear, with a tab named Untitled.mlx. In the editor window, type the following r = 2; h = 3; V = pi*r^2*h A = 2*pi*r^2 + 2*pi*r*h Note that there are no command prompts in the script window. Click on the Live Editor tab on the menu bar and click Save. A box opens up for us to name the file. Let's name it cylinder. Fill this in and click the Save button in the box. Note that the name of the tab has been changed to cylinder.mlx and a file named cylinder.mlx appears in the Current Folder Window. To run the commands in the script, enter Ctrl-Enter (hold down the Ctrl key and press Enter). In the editor window to the right of the commands one sees
Transcript
Page 1: Math 216 - Assignment 2 - First Order Differential Equationsfmassey/math216/Assignments... · 2018-02-08 · Math 216 - Assignment 2 - First Order Differential Equations Due: Monday,

1

Math 216 - Assignment 2 - First Order Differential Equations

Due: Monday, February 12. Nothing accepted after Tuesday, February 13. This is worth 15

points. 10% points off for being late. You may work by yourself or in pairs. If you

work in pairs it is only necessary to turn in one set of answers for each pair. Put both

your names on it.

Do Problems 1 - 7 in the discussion below. Use one or more Live Scripts to hold your input and

output. Label the work for each problem: Problem 1, Problem 2, etc. Print a copy of the

file(s) to hand in.

Live Scripts

So far, we have seen how to execute commands in the Command Window. There is an

alternative that is more convenient in many ways such as when one is correcting errors or when

one needs to execute a series of commands or one expects to repeatedly use and modify several

commands. Live Scripts allow one to write and save these commands. Live Script files are

created and edited using the MATLAB editor.

Let's write a Live Script that finds the volume and surface area of a cylinder of radius r and

height h. We shall use the formulas V = r2h and A = 2r2 + 2rh.

We begin by clicking on New on the tool bar and selecting Live Script on the drop down menu.

The Editor Window will appear, with a tab named Untitled.mlx.

In the editor window, type the following

r = 2; h = 3; V = pi*r^2*h A = 2*pi*r^2 + 2*pi*r*h

Note that there are no command prompts in the script window.

Click on the Live Editor tab on the menu bar and click Save. A box opens up for us to name the

file. Let's name it cylinder. Fill this in and click the Save button in the box. Note that the name

of the tab has been changed to cylinder.mlx and a file named cylinder.mlx appears in the Current

Folder Window.

To run the commands in the script, enter Ctrl-Enter (hold down the Ctrl key and press Enter). In

the editor window to the right of the commands one sees

Page 2: Math 216 - Assignment 2 - First Order Differential Equationsfmassey/math216/Assignments... · 2018-02-08 · Math 216 - Assignment 2 - First Order Differential Equations Due: Monday,

2

V = 37.6991 A = 62.8319

If one has made an error, one simply corrects the error in the Editor window and enters Ctrl-

Enter again to re-execute the commands.

One can also enter comment lines in a Live Script by positioning the cursor where the comment

line is to go, clicking on the Live Editor tab on menu bar and clicking the Text button. A new

line opens up on which one can enter comments. To switch from comment lines to command

lines, click the Code button.

As noted above, the Current Folder Window contains the contents of the currently active folder

where files will be saved if one doesn't specify otherwise. The name of the Current Folder is

displayed above this Window and can be changed by using the back arrow to get to the root

folder of the desired folder and then going down to the desired folder. Often, the Current Folder

starts out as one titled MATLAB which is located inside Documents.

In the Mac lab in 2048 CB the folder MATLAB inside Documents is a temporary folder that

disappears when you Logoff. You should save items in that folder that you want to keep either

on a flash drive or in your campus file space before you Logoff. To get to your campus file

space, use the Fetch program which is in the Applications folder. Enter login-umd.umich.edu in

the Hostname field, your campus id in the Username field and your password in the Password

field. Click Connect. Your campus file space should appear. You can drag files back and forth

between your campus file space and folder on the Mac.

Symbolic Functions

To solve differential equations symbolically we need to use symbolic functions. The following

commands define f(x) to be a symbolic function corresponding to f(x) = x2 – 3x + 2.

>> syms x

>> f(x) = x^2 – 3*x + 2

Note that the entry for f in the workspace has 11 symfun for its value indicating it is a symbolic

function. We can apply the function to a value.

Page 3: Math 216 - Assignment 2 - First Order Differential Equationsfmassey/math216/Assignments... · 2018-02-08 · Math 216 - Assignment 2 - First Order Differential Equations Due: Monday,

3

>> a = f(2.5) a = 3/4

Note that instead of returning the value 0.7500 for the value of f(2.5) it returns 3/4. The returned

value 3/4 is a symbolic constant just as with the solve function. If you check the value of a in

the workspace it says 11 sym.

You can use f in situations where you would use symbolic expressions.

>> diff(f^2, x) ans(x) = 2*(2*x – 3)*(x^2 – 3*x* + 2)

Problem 1: Define a symbolic function corresponding to the function h(x) = x

4 - x2. Use it to

find h(3) and the solutions to the equation x

4 - x2 = 1

3.

We need to be careful when we specify symbolic functions. Compare the following two

examples.

>> syms x >> g = cos(x) g = cos(x) >> g(pi) Index exceeds matrix dimensions >> subs(g, x, pi) ans = -1

Page 4: Math 216 - Assignment 2 - First Order Differential Equationsfmassey/math216/Assignments... · 2018-02-08 · Math 216 - Assignment 2 - First Order Differential Equations Due: Monday,

4

The commands syms x and g = cos(x) assign the symbolic expression cos(x) to g. However, g is

just a regular variable. When the command g(pi) is executed, the value cos(x) replaces g. It is

treated as a 11 array and MATLAB tries to take the th element of this array. It can’t do this, so

it gives the error message Index exceeds matrix dimensions. When the command subs(g, x, pi) is

executed, the value cos(x) replaces g. The command subs(g, x, pi) substitutes pi for x in cos(x) and

then evaluates it to give -1.

In the second example, we put (x) after g in g = cos(x).

>> syms x >> g(x) = cos(x) g(x) = cos(x) >> g(pi) ans = -1

This time the commands syms x and g(x) = cos(x) create a symbolic function g(x) which

implements the function g(x) = cos x. When the command g(pi) is executed, the value pi is

substituted for x in cos(x) and then evaluated to give -1.

When we solve differential equations we need to define symbolic functions without giving a

definition to these functions. For example, this defines a symbolic function y(x) but does not

give a definition to y(x).

>> syms y(x)

In the next section we shall use this to solve differential equations. However, another use is to

differentiate implicitly:

>> diff(y^2, x)

ans = 2 * y(x)*diff(y(x), x)

Page 5: Math 216 - Assignment 2 - First Order Differential Equationsfmassey/math216/Assignments... · 2018-02-08 · Math 216 - Assignment 2 - First Order Differential Equations Due: Monday,

5

Note that for the input we use y for the function, but in the output MATLAB uses y(x). Also,

MATLAB uses diff(y(x), x) to mean ∂y/∂x.

Problem 2: Suppose y stands for an unknown function of x. Find the derivative with respect to

x of x2 cos y . The answer will involve dy

dx.

Solving differential equations symbolically

The dsolve command solves differential equations symbolically. In order to specify the equation

we need a symbolic function. In the following example we solve the equation dy

dx = y + x. To

specify the equation in dsolve, we first create a symbolic function y(x). Then we describe the

differential equation in dsolve by diff(y,x) == y + x.

>> syms y(x); >> dsolve(diff(y,x) == y + x) ans = C1*exp(x) – x - 1

We didn't specify an initial condition so we obtained a general solution. If we specify an initial

condition then the solution will be a single function. Often we want to do computations with the

solution, so we assign it to sol(x) which will also be a symbolic function.

>> syms y(x) >> sol(x) = dsolve(diff(y,x) == y + x, y(0) == 1) sol(x) = 2*exp(x) – x - 1

For example, we can evaluate the solution for x = 4 and plot it.

>> s4 = sol(4) s4 = 2*exp(4) – 5 >> double(s4)

Page 6: Math 216 - Assignment 2 - First Order Differential Equationsfmassey/math216/Assignments... · 2018-02-08 · Math 216 - Assignment 2 - First Order Differential Equations Due: Monday,

6

ans =

104.1963

>> u = linspace(0, 3, 100); >> plot(u, sol(u))

Problem 3: Find the general solution of dy

dx = y(4 - y). Then solve the differential equation

dy

dx = y(4 - y) with the initial condition y = 1 when x = 0. Then make the graph of the

solution to this initial value problem for 0 x 4.

Sometimes dsolve gives disconcerting results. The solution of the differential equation

dy

dx = y2 - 3x involves Bessel functions which are a type of special function. Try this.

>> syms y(x); >> dsolve(diff(y,x) == y^2 - 3*x) ans = (x^(1/2)*(C2*(besselj(1/3, (3^(1/2)*x^(3/2)*2i)/3)/(2*x) - 3^(1/2)*x^(1/2)*besselj(-2/3, (3^(1/2)*x^(3/2)*2i)/3)*1i) + C3*(bessely(1/3, (3^(1/2)*x^(3/2)*2i)/3)/(2*x) - 3^(1/2)*x^(1/2)*bessely(-2/3, (3^(1/2)*x^(3/2)*2i)/3)*1i)) - (C2*besselj(1/3, (3^(1/2)*x^(3/2)*2i)/3) + C3*bessely(1/3, (3^(1/2)*x^(3/2)*2i)/3))/(2*x^(1/2)))/(x^(1/2)*(C2*besselj(1/3, (3^(1/2)*x^(3/2)*2i)/3) + C3*bessely(1/3, (3^(1/2)*x^(3/2)*2i)/3)))

And often dsolve is unable to solve a differential equation. This is like when solve is unable to

solve an algebraic equation. For example,

Page 7: Math 216 - Assignment 2 - First Order Differential Equationsfmassey/math216/Assignments... · 2018-02-08 · Math 216 - Assignment 2 - First Order Differential Equations Due: Monday,

7

>> syms y(x); >> dsolve(diff(y,x) == sin(x*y)) Warning: Explicit solution could not be found. > In dsolve (line 201) ans = [ empty sym ]

If a differential equation can not be solved symbolically, then one can solve it numerically.

Numerical valued functions

In the first section we saw how one defines symbolic functions which return symbolic values.

These functions were used to solve differential equations symbolically. In this section we see

how to define functions which return numbers and vectors. These are useful in numeric

computations. In particular, we shall use them in the next section to solve differential equations

numerically.

The following command defines f to be a numerical valued function corresponding to

f(x) = x2 - 3x + 2.

>> f = @(x) x^2 – 3*x + 2 f = function_handle with value: @(x)x^2-3*x+2

When we define a numerical valued function f in this way, MATLAB indicates this by saying the

value of f is a function handle. We can apply the function to a value.

>> a = f(2.5) a = 0.7500

Page 8: Math 216 - Assignment 2 - First Order Differential Equationsfmassey/math216/Assignments... · 2018-02-08 · Math 216 - Assignment 2 - First Order Differential Equations Due: Monday,

8

This function returns the value 0.7500 for the value of f(2.5) instead of 3/4 which the

corresponding symbolic function returned.

Here is a function of two variables.

>> g = @(x,y) y^2 - 3*x; >> g(4,2) ans = -11

Problem 4: Define a numerical valued function corresponding to the function h(x) = x

4 - x2. Use

it to find h(4) and to make the graph of x

4 - x2 for -4 x 4. You will probably have

to use ./ and .^ in the definition of h.

Sometimes it takes more than one line to define a numerical valued function (or it is more

convenient to use more than one line). In that case one needs to write a function script.

We shall illustrate how function scripts are written and used by means of the function Area

which computes the surface area A of a sphere whose volume is V.

We begin by clicking on New Script on the tool bar. The Editor Window will appear, with a tab

named Untitled1.

In the editor window, type the following

function A = Area(V) r = (3*V/(4*pi))^(1/3); A = 4*pi*r^2; end

Note that there are no command prompts in the script window.

Click on the Editor tab on the menu bar and click Save. A box opens up for us to name the file.

Fill in Area and click the Save button in the box. Note that the name of the tab has been changed

to Area.m and a file named Area.m appears in the Current Folder Window. In general, the name

of the file a function is saved in should be the same as the name of the function with .m attached.

Functions begins with a line called a header. For the function Area the header is the following.

function A = Area(V)

Page 9: Math 216 - Assignment 2 - First Order Differential Equationsfmassey/math216/Assignments... · 2018-02-08 · Math 216 - Assignment 2 - First Order Differential Equations Due: Monday,

9

The header begins with the word function whose purpose is to signal that this is a function. This

is followed by A which specifies that the value returned will be the final value of the variable A.

Then there is an = which is followed by the name of the function Area. Then comes the

parameter V which holds the input to the function from the command line. The parameter is

enclosed in parentheses.

Suppose we want to use this function of find the surface area of a sphere whose volume is 8. We

could do that using the function Area by entering the following in the command line.

>> s = Area(8) s = 19.3439

When the command Area(8) is encountered, MATLAB goes to the file Area.m and fills in the

parameter V in the function with the value of the corresponding argument 8 in the command line.

Then it executes the lines following the header in the file as if they had been entered in the

command line. When it reaches the end, MATLAB assigns the return value A from the function

to the variable s to the left of the = in the command line.

Here is a function Integrate which computes a numerical approximation to a definite integral

a

b

f(x) dx using the midpoint method with n subintervals. Recall, the midpoint method

approximates the integral by i = 1

n

f(a + (i - 1

2)h) h where h =

b - a

n. The script is as follows.

function integral = Integrate(f, a, b, n) h = (b-a)/n; integral = 0; for i = 1:n integral = integral + f(a + (i - 1/2)*h) end integral = integral*h; end

In this case there are four parameters f, a, b, n which hold the inputs to the function from

the command line. The script is saved in the file Integrate.m. Suppose we want to use

approximate 0

1

x3 dx using the midpoint method with 20 subintervals. We could do that using

the function Integrate as follows.

Page 10: Math 216 - Assignment 2 - First Order Differential Equationsfmassey/math216/Assignments... · 2018-02-08 · Math 216 - Assignment 2 - First Order Differential Equations Due: Monday,

10

>> g = @(x) x.^3;

>> Integrate(g, 0, 1, 20) ans = 0.2497

Note that the answer was off by about 0.0003. If we increased the number of subintervals the

answer would be closer to the correct value. Normally we would use a better method, such as

Simpson's rule or Gaussian quadrature, to approximate a definite integral.

The function uses a MATLAB construct called a for loop. This is the sequence of three lines

for i = 1:n integral = integral + f(a + (i - 1/2)*h) end

What this does is repeat the line integral = integral + f(a + (i - 1/2)*h) with each of the integer values of

i starting at 1 and ending at n. So it is adding f(a + (i - 1/2)*h) to integral for i = 1, 2, ..., n. In this

way it forms the sum i = 1

n

f(a + (i - 1

2)h).

Problem 5: Modify the above function Integrate above so it uses the trapezoidal rule instead of

the midpoint method. Recall the trapezoidal rule is

a

b

f(x) dx h [1

2 f(a) +

i = 1

n-1

f(a + ih) + 1

2 f(b)] where h =

b - a

n. Use it to

approximate 0

1

x3 dx with 20 subintervals. How does it compare with the midpoint

method.

Numerical approximations

The solution of most differential equations can not be expressed in terms of familiar functions.

However, one can obtain numerical approximations to the solution of an initial value problem

using generalizations of the methods discussed in class. The command ode45 does this using an

adaptation of the classical Runge-Kutta method that can adjust the step size for accuracy and

efficiency. We illustrate it with the differential equation dy

dx = y2 - 3x. When one solves a

differential equation numerically, one needs to decide on an interval a x b on which to

approximate the solution. Suppose in this example, we want to approximate the solution on the

interval 0 x 9. Finally we need to specify the initial condition. The default for ode45 is the

initial condition is specified at the left endpoint of the interval on which we are approximating

the solution. In our example it is at x = 0. Suppose we want the solution to this equation which

Page 11: Math 216 - Assignment 2 - First Order Differential Equationsfmassey/math216/Assignments... · 2018-02-08 · Math 216 - Assignment 2 - First Order Differential Equations Due: Monday,

11

satisfies the initial condition y = 1 when x = 0. Then we can generate the numerical

approximation and store the resulting approximation in the vectors x and y with the command

>> f = @(x,y) y^2 - 3*x;

>> [xA, yA] = ode45(f, [0, 9], 1);

The vector [0, 9] specifies the interval of x, and the third input, 1, gives the initial value of y

when x = 0. The function f = @(x,y) y^2-3*x specifies the right hand side of the differential

equation dy

dx = y2 - 3x.

If you look at the vectors xA and yA you can see that they both have 105 values. The number of

values depends on the particular solution you are approximating and the interval. The values of

xA run from 0 to 9 corresponding to the fact that we are approximating the solution on the

interval 0 x 9. The yA values are the values of y at the corresponding x values. For example,

- 5.1676 is the estimated value for y(9). If we want to write the command without knowing how

many steps MATLAB took, we can tell it just to give us the last entry by entering y(length(y)).

>> xA(105)

>> ans = 9

>> yA(105)

>> ans = -5.1676

We can make the graph of the approximate

solution by entering

>> plot(xA, yA)

The graph looks like the one at the right.

Page 12: Math 216 - Assignment 2 - First Order Differential Equationsfmassey/math216/Assignments... · 2018-02-08 · Math 216 - Assignment 2 - First Order Differential Equations Due: Monday,

12

Problem 6: a. Use ode45 to construct a numerical approximation to the solution of the

differential equation dy

dx = y(4 - y) with the initial condition y = 1 when x = 0. Construct the

numerical approximation of the interval 0 x 4.

b. Make the graph of the numerical approximation on this interval. How does it compare

with the one obtained in Problem 3.

c. Make a graph showing difference between the numerical approximation and the true

solution obtained in Problem 3. To do this plot(xA, sol(xA) – yA) where [xA, yA] is the

solution in part a and sol(x) is the solution from Problem 3.

EulerApprox

Even though we have ode45 available to solve differential equations numerically, it is instructive

to look at a the following simple function, EulerApprox, based on Euler's method for solving

differential equations numerically.

function [x,y] = EulerApprox(dydx, xrange, y0, h) x = xrange(1):h:xrange(2); n = length(x); y(1) = y0; for i = 1:n-1 y(i+1) = y(i) + dydx(x(i),y(i))*h; end end

The function EulerApprox is similar to ode45, but one must also specify a step size. A typical

series of command lines ending up with one calling the function EulerApprox would be the

following.

>> h = 0.1; >> xspan = [0, 9]; >> y0 = 1; >> dydx = @(x,y) y^2 - 3*x; >> [x, y] = EulerApprox(dydx, xspan, y0, h);

After one calls EulerApprox one can use the outputs x and y in the same manner as one uses the

output of ode45. For example, one can draw a plot of the resulting approximation.

Page 13: Math 216 - Assignment 2 - First Order Differential Equationsfmassey/math216/Assignments... · 2018-02-08 · Math 216 - Assignment 2 - First Order Differential Equations Due: Monday,

13

>> plot(x, y)

Problem 7: a. Create a file with the above script for EulerApprox

and use it to construct a numerical approximation to the

solution of the differential equation dy

dx = y(4 - y) with the initial

condition y = 1 when x = 0. Construct the numerical

approximation on the interval 0 x 4 with h = 0.04.

b. Make a graph showing difference between the numerical approximation and the true

solution obtained in Problem 3. To do this plot(xA, sol(xA) – yA) where [xA, yA] is the

solution in part a and sol(x) is the solution from Problem 3. What is the maximum error

in the approximation?

c. Repeat a and b with h = 0.004. How much did the maximum error reduce?


Recommended