Due: Tuessday, May 28 by 11:59pm via CoCalc
SageMath
SageMath
SageMath
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.
If not asked to graph, then just type the syntax for the function.
a.
b.
c. , , ,
d. ,
e. Graph and evaluate
f. , ,
g. Graph and find an appropriate scale for the axes that shows all important features of the graph.
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: .
These are difficult to graph by hand but a computer can do it easily.
Here's how:
# 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.
# Customize the slope field:
plot_slope_field( F(x,y), (x,-2,2), (y,-2,2), color='blue', headaxislength=3, headlength=3)
You will need to modify the - and -values in the graphing window so that all interesting features are shown.
a.
b.
c.
d.
e.
f.
We now show how to solve differential equations using SAGE
We'll start with solving: .
If you recall from Chapter 6 and 9, this is the law of natural growth with constant .
We know the general solution is (again, with ).
Let's see how to solve it in SAGE
.
# 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)
# 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)
# 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)
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 and , but change the window for the particular solutions so that the red curves don't go past .
# 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)
Consider (DE): .
a. Plot the slope field for the DE with window: and .
b. Find the general solution to the differential equation.
c. Find the particular solutions passing through:
i. ii. iii. iv. v. vi.
d. Plot all five particular solutions found in part (c) in one window. Include the points of the initial conditions.