Calculus II - Spring 2019

Instructor: Dr. Basilio

Lab 1

Differential Equations, Part 1

Slope Fields and Solving Differential Equations

Due: Tuessday, May 28 by 11:59pm via CoCalc

Objectives

  • Learn the syntax for other functions needed for calculus
  • Learn how to plot simple slope fields using SageMath
  • Learn how to solve simple differential equations using SageMath
  • Learn to plot the particular solutions together with a slope field using SageMath

Section 0. Create your lab document.

In your Lab_1 folder, create a new SAGE worksheet (.sagewa) file titled:

"Math5B-Lab_1-LASTNAME_FIRSTNAME-S19.sagews"

At the beginning of your document, type the following comments:

# Math 5B - Calc II - Spring 2019
# Lab_1
# Due: Tuesday, May 28 @11:59pm via CoCalc

# ()Your Last Name, Your First Name)

# Assignment

# Problem 1

Be sure to use comments to label each problem and it's parts.

Section 1. More functions

Problem 1: Look up the syntax for the following functions:

If not asked to graph, then just type the syntax for the function.

a. |x|

b. log10(x)

c. tan(x), sec(x), csc(x), cot(x)

d. sin1(x)=arcsin(x), tan1(x)=arctan(x)

e. Graph y=arctan(x) and evaluate limxarctan(x)

f. sinh(x), cosh(x), tanh(x)

g. Graph y=tanh(x) and find an appropriate scale for the axes that shows all important features of the graph.

Section 2. Slope Fields in Sage

Recall that solving differential equations (DEs) is very hard and it can be difficult to solve one with an exact formula.

Slope fields (or direction fields), are visual plots of the DE: dydx=F(x,y).

These are difficult to graph by hand but a computer can do it easily.

Here's how:

In [54]:
# Slope fields
x,y = var('x y') # define variables x and y
F(x,y) = x^2*y # define the function F(x,y)
plot_slope_field( F(x,y), (x,-2,2), (y,-2,2)) # Note: you can change the size of the window.
Out[54]:
In [55]:
# Customize the slope field:
plot_slope_field( F(x,y), (x,-2,2), (y,-2,2), color='blue', headaxislength=3, headlength=3)
Out[55]:

Problem 2. Plot the following slope fields:

You will need to modify the x- and y-values in the graphing window so that all interesting features are shown.

a. dydx=sin(x)sin(y)

b. dydx=x+y1

c. y=x(2y)

d. y=2y

e. y=x2y12y2

f. dydx=cos(x+y)

Section 3. Solving Differential Equations

We now show how to solve differential equations using SAGE

We'll start with solving: y=y.

If you recall from Chapter 6 and 9, this is the law of natural growth with constant k=1.

We know the general solution is y(x)=Cex (again, with k=1).

Let's see how to solve it in SAGE.

In [114]:
# Solving a DE
x = var('x') # define the independent variable
y = function('y')(x) # define y as a function of x
DE = diff(y,x) - y # define the differerntial equation: y'-y=0. 
    # Note: We want to put everything on one side except the "=0"
gen_sol = desolve(DE, y) # solve the differential equation DE and store it to "gen_sol"
    # the ",y" asks SAGE to solve for y.
show(gen_sol)
Out[114]:
Cex
In [115]:
# Solving a DE with initial conditions
x = var('x') # define the independent variable
y = function('y')(x) # define y as a function of x
DE = diff(y,x) - y # define the differerntial equation: y'-y=0. We will want to put everything on one side but not the "=0"

part_sol = desolve(DE, y, ics=[0,1]) # solve the differential equation DE with initial conditions (the "ics")
    # ics=[0,1] means x=0 and y=1
show(part_sol)

part_sol_2 = desolve(DE, y, ics=[0,1.5]) # solve the differential equation DE with initial conditions (the "ics")
    # ics=[0,1] means x=0 and y=1
show(part_sol_2)
Out[115]:
ex
Out[115]:
32ex
In [116]:
# Plotting slope field and particular solution together
x,y = var('x y')
# slope field for: y'=y so F(x,y)=y
SF = plot_slope_field( y, (x,-2,2), (y,-2,2), color='blue', headaxislength=3, headlength=3) # SF = slope field

PS1 = plot(part_sol, (x,-2,2), color='red', thickness=2.5) # PS = particular solution with C=1

PS2 = plot(part_sol_2, (x,-2,2), color='red', thickness=2.5) # PS2 = particular solution 2 with C=1.5

#note: by defining SF, PS1 and PS2 they are stored in SAGE. To plot them we will use 'show()' command
show(SF+PS1+PS2)
Out[116]:

Notice the graph is correct but doesn't look good. We can manipulate the windown so that they all match.

We'll keep the slope field window from 2x2 and 2y2, but change the window for the particular solutions so that the red curves don't go past y=2.

In [118]:
# Plotting slope field and general solution together (with better window and with point emphasized)
x,y = var('x y')
# slope field for: y'=y so F(x,y)=y
SF = plot_slope_field( y, (x,-2,2), (y,-2,2), color='blue', headaxislength=3, headlength=3) # SF = slope field

PS = plot(part_sol, (x,-2,2), ymax=2, color='red', thickness=2.5) # PS = particular solution with C=1

PS2 = plot(part_sol_2, (x,-2,2), ymax=2, color='red', thickness=2.5) # PS = particular solution with C=1.5

# Let's add the points too:
Pt = point((0,1), size=50, color='red') # add the point with IC
Pt2 = point((0,1.5), size=50, color='red') # add point with IC2

#note: by defining SF and PS etc they are stored in SAGE. To plot them we will use 'show()' command
show(SF+PS+PS2+Pt+Pt2)
Out[118]:

Problem 3. Solving Differential Equations and Graphing a Particular solution

Consider (DE): dydx=ysin(x).

a. Plot the slope field for the DE with window: 3x3 and 3y3.

b. Find the general solution to the differential equation.

c. Find the particular solutions passing through:

i. (2,2) ii. (1,2) iii. (0,2) iv. (1,2) v. (2,2) vi. (2,2)

d. Plot all five particular solutions found in part (c) in one window. Include the points of the initial conditions.

Bonus:

Extra-Credit

Plot the slope field of: dydx=xy+1 with window 3x3 and 3y3.

Include in your plot the 12 particular solutions passing through:

(2,2), (1,2), (0,2), (1,2), (2,2)

and

(2.8,0), (2.8,1), (2.8,2)

and

(2,2), (1,2), (0,2), (1,2).

Include in your plot the 12 points as well.