-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_of_plotting_iteration_roundoff.py
127 lines (85 loc) · 3.13 KB
/
copy_of_plotting_iteration_roundoff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# -*- coding: utf-8 -*-
"""Copy of Plotting_iteration_roundoff.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1nA2cS-aCTpUy7-SjTxG-KWW_QHNiAT81
#**Lab 3 - Plotting, iteration, and roundoff error**
Enter your code in the spaces provided. Do not change any of the variable names or function names that are already provided for you. In places where we specify the name of the return value, make sure that your code produces the a value with the correct name.
"""
# Do not edit this cell.
LabID="Lab3"
try:
from graderHelp import ISGRADEPLOT
except ImportError:
ISGRADEPLOT = True
"""**Enter your BYU NetID**"""
# Enter your first and last names in between the quotation marks.
first_name="Madison"
last_name="Wozniak"
# Enter your Math 215 section number in between the quotation marks.
section_number="001"
# Enter your BYU NetID in between the quotation marks. NOT YOUR BYU ID NUMBER!
BYUNetID="mwozniak"
"""**Import NumPy and PyPlot**"""
import numpy as np
from matplotlib import pyplot as plt
"""**Problem 1**"""
# Plot both functions from Problem 1 here. Put all of your code to create the plot inside the function below.
def create_plots():
x=np.arange((-2*np.pi),(2*np.pi),.1)
y=np.sin(x)
z=np.cos(4*x)
plt.plot(x,y,x,z)
plt.show()
return None
"""**Problem 2**"""
# Create the scatter plot from Problem 2 here. Put all of your code to create the plot inside the function below.
def create_scatter_plot():
x = np.random.normal(scale=1.5, size=500)
y = np.random.normal(scale=1,size=500)
plt.plot(x,y,'x',markersize=5,alpha=1)
plt.show()
return None
"""**Problem 3**"""
def fact(n):
if n==0:
return 1
else:
return n*fact(n-1)
"""**Problem 4**"""
def f(x):
return (x**2)+x-5
a= 0
b= 2
n= 100
for i in range(n):
d = (a + b)/2
if f(d) < 0:
a = d
else:
b = d
print(d)
root = 1.79128784747792
"""**Problem 5**"""
def g(x):
return (x**4)-(2*x**3)-(17*x**2)+(4*x)+30
def g_prime(x):
return (4*x**3)-(6*x**2)-(34*x)+4
"""**Problem 6**"""
def newtons_method(starting_guess,n):
x_j=starting_guess
for i in range(n):
x_j= x_j-(g(x_j)/g_prime(x_j))
return x_j
newtons_method(10,5)
"""**Problem 7**"""
def integration(m):
E=1-(1/np.exp(1))
for j in range(1,m+1):
E=1-j*E
return E
integration(10)
"""**STOP! BEFORE YOU SUBMIT THIS LAB:** Go to the "Runtime" menu at the top of this page, and select "Restart and run all". If any of the cells produce error messages, you will either need to fix the error(s) or delete the code that is causing the error(s). Then use "Restart and run all" again to see if there are any new errors. Repeat this until no new error messages show up.
**You are not ready to submit until you are able to select "Restart and run all" without any new error messages showing up. Your code will not be able to be graded if there are any error messages.**
To submit your lab for grading you must first download it to your compute as .py file. In the "File" menu select "Download .py". The resulting file can then be uploaded to http://www.math.byu.edu:30000 for grading.
"""