### 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")
})

``````
``````

requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window.Plotly) {{require(['plotly'],function(plotly) {window.Plotly=plotly;});}}

require(["plotly"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL="https://plot.ly";Plotly.newPlot("ca662e91-072f-4c75-ac76-6e10c39bc609", [{"type": "scatter", "y": [16.0, 9.0, 6.25, 5.0625, 4.515625, 4.25390625], "x": [4.0, 3.0, 2.5, 2.25, 2.125, 2.0625], "line": {"color": "#bc42f4"}, "name": "right"}, {"type": "scatter", "y": [0.0, 1.0, 2.25, 3.0625, 3.515625, 3.75390625], "x": [0.0, 1.0, 1.5, 1.75, 1.875, 1.9375], "line": {"color": "#41f1f4"}, "name": "left"}, {"type": "scatter", "y": [4.0], "x": [2.0], "name": "point", "mode": "markers"}], {"title": "Convergence Example"}, {"showLink": true, "linkText": "Export to plot.ly"})});

``````
``````

In [42]:

# Copied directly from example Modeule 2
'''
Math for Modelers Session #6 Python Module #2

"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")
})

``````
``````

requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window.Plotly) {{require(['plotly'],function(plotly) {window.Plotly=plotly;});}}

require(["plotly"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL="https://plot.ly";Plotly.newPlot("11ccee05-63b3-4f44-b891-6b07b3d5a96d", [{"type": "scatter", "y": [1.3817735607657808, 1.2976264352760167, 1.2134793097862526, 1.1293321842964885, 1.0451850588067244, 0.9610379333169603, 0.8768908078271962, 0.7927436823374321, 0.708596556847668, 0.6244494313579039, 0.5403023058681398, 0.4561551803783756, 0.37200805488861144, 0.2878609293988474, 0.20371380390908322, 0.11956667841931923, 0.03541955292955501, -0.0487275725602091, -0.1328746980499731, -0.2170218235397373, -0.3011689490295013], "x": [0.0, 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6000000000000001, 0.7000000000000001, 0.8, 0.9, 1.0, 1.1, 1.2000000000000002, 1.3, 1.4000000000000001, 1.5, 1.6, 1.7000000000000002, 1.8, 1.9000000000000001, 2.0], "name": "tangent"}, {"type": "scatter", "y": [-0.5389614493995115, -0.45217616214091194, -0.36087288013976715, -0.26596387560898016, -0.1683974479490768, -0.06914844865406172, 0.030791459082466565, 0.130423708738146, 0.22875280780846002, 0.3247962844387769, 0.4175945039583588, 0.5062202572327792, 0.589788025031099, 0.6674628258413089, 0.7384685587295886, 0.8020957578842933, 0.8577086813638248, 0.904751663219964, 0.9427546655283467, 0.97133797485203, 0.9902159962126375, 0.999200106660978, 0.9982005399352041, 0.9872272833756266, 0.9663899781345127, 0.9358968236779341, 0.8960524975255243, 0.8472551110134149, 0.7899922314973636, 0.7248360107409035, 0.6524374681640499, 0.5735199860724545, 0.48887208186052517, 0.3993395294062706, 0.30581690837828657, 0.20923866589141643, 0.1105697798200665, 0.010796117058264285, -0.08908541693646228, -0.18807683889288337, -0.285189059245024, -0.37945176478815734, -0.46992311372760537, -0.555699146250616, -0.6359228165940055, -0.709792556362123, -0.7765702835332956, -0.8355887771314101, -0.8862583438773539, -0.928072710209334, -0.9606140808009535, -0.9835573130340073, -0.9966731657160469, -0.9998305895825983, -0.9929980366980922, -0.9762437756724087, -0.9497352095434947, -0.9137372031415428, -0.8686094366471624, -0.8148028117859092, -0.7528549465672918, -0.6833848035833326, -0.6070865055389502, -0.5247223998073418], "x": [-2.14, -2.04, -1.94, -1.8399999999999999, -1.7399999999999998, -1.6399999999999997, -1.5399999999999996, -1.4399999999999995, -1.3399999999999994, -1.2399999999999993, -1.1399999999999992, -1.0399999999999991, -0.9399999999999991, -0.839999999999999, -0.7399999999999989, -0.6399999999999988, -0.5399999999999987, -0.4399999999999986, -0.3399999999999985, -0.23999999999999844, -0.13999999999999835, -0.03999999999999826, 0.06000000000000183, 0.16000000000000192, 0.260000000000002, 0.3600000000000021, 0.4600000000000022, 0.5600000000000023, 0.6600000000000024, 0.7600000000000025, 0.8600000000000025, 0.9600000000000026, 1.0600000000000027, 1.1600000000000028, 1.260000000000003, 1.360000000000003, 1.460000000000003, 1.5600000000000032, 1.6600000000000033, 1.7600000000000033, 1.8600000000000034, 1.960000000000003, 2.0600000000000036, 2.160000000000004, 2.260000000000004, 2.3600000000000034, 2.460000000000004, 2.5600000000000045, 2.660000000000004, 2.760000000000004, 2.8600000000000043, 2.960000000000005, 3.0600000000000045, 3.160000000000004, 3.2600000000000047, 3.360000000000005, 3.460000000000005, 3.5600000000000045, 3.660000000000005, 3.7600000000000056, 3.860000000000005, 3.960000000000005, 4.060000000000006, 4.1600000000000055], "name": "curve"}], {"title": "Tangent at a point"}, {"showLink": true, "linkText": "Export to plot.ly"})});

``````
``````

In [ ]:

``````