Week 6 Homework Notebook

This is to use for python homework practice

Description

Use plotly to produce examples or solve homework problems for week 6.


In [7]:
# pip install plotly first if you havent already done so. 
import plotly.plotly
from plotly.graph_objs import Scatter, Layout
from numpy import arange, cos
import numpy as np
print('imports completed')


imports completed

In [37]:
# Copied from example Module 1
def g(x):
    return x*x  # This is the function in Lial Section 11.1 Example 1.

'''
The numbers chosen below here are somewhat arbitrary to show what the limit is. Think about the chart on
page 266 and graph 267. Break it into discrete confided steps. Focus on learning the idea of a limit, getting closer
and closer without actually reaching a number.
'''
n = 5
powers = arange(0, n+1)
print("powers: {}".format(powers))
denominator = 2.0**powers
print("denominator: {}".format(denominator))
delta = 2.0

'''
This determines the number of values calculated on each side of x=0.
denominator contains exponentiated values of 2.0. # This is the interval used on either side of x=2.0.
The following are values of x and f(x) trending to the limit at x=2.0.
Delta is being divided by powers of 2 to reduce the distance from the limit.
'''
x_r = 2.0+delta/denominator  # Approaching from the right.
print("x_r: {}".format(x_r))
y_r = g(x_r)
print("y_r: {}".format(y_r))
x_l = 2.0-delta/denominator  # Approaching from the left.
print("x_l: {}".format(x_l))
y_l = g(x_l)
print("y_l: {}".format(y_l))
# The following determine the vertical boundaries of the resulting plot.


powers: [0 1 2 3 4 5]
denominator: [  1.   2.   4.   8.  16.  32.]
x_r: [ 4.      3.      2.5     2.25    2.125   2.0625]
y_r: [ 16.           9.           6.25         5.0625       4.515625
   4.25390625]
x_l: [ 0.      1.      1.5     1.75    1.875   1.9375]
y_l: [ 0.          1.          2.25        3.0625      3.515625    3.75390625]

In [44]:
#Required for displaying plotly in jupyter notebook
plotly.offline.init_notebook_mode(connected=True)

# Create traces
trace1 = Scatter(x=x_r, y=y_r, name='right', line=dict(color='#bc42f4'))
trace2 = Scatter(x=x_l, y=y_l, name='left', line=dict(color='#41f1f4'))
point = Scatter(x=[2.0], y=[g(2.0)], name='point', mode='markers')

plotly.offline.iplot({
    "data": [trace1, trace2, point],
    "layout": Layout(title="Convergence Example")
})



In [42]:
# Copied directly from example Modeule 2
'''
Math for Modelers Session #6 Python Module #2

Reading assignment:
"Think Python" 2nd Edition Chapter 7 (7.1-7.7)
“Think Python” 3rd Edition Chapter 7 (pages 75-81)

Module #2 objectives: 1) demonstrate numerical differentialtion,
and 2) illustrate results graphically.


A general function for calculating the slope between two points: x and
x+delta.  See Lial Section 11.3 dealing with instantaneous rates of change.
'''

# Define a function for demonstration.  This function may be changed.
def f(x):
    return cos(x)


point = 1.0  # This is a point at which a derivative will be calculated.

# Calculate values for the tangent.
w = arange(point - 1.0, point + 1.1, 0.1)
t = f(point) + limit * (w - point)

# Now we are going to plot the original function over a wider range.
# Define a domain for the function.
domain = 3.14

# Calculate values for the function on both sides of x=1.0.
u = arange(point - domain, point + domain + 0.1, 0.1)
z = f(u)


#Required for displaying plotly in jupyter notebook
plotly.offline.init_notebook_mode(connected=True)

# Create traces
tangent = Scatter(x=w, y=t, name='tangent')
curve = Scatter(x=u, y=z, name='curve')

plotly.offline.iplot({
    "data": [tangent, curve],
    "layout": Layout(title="Tangent at a point")
})



In [ ]: