-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMath
More file actions
116 lines (97 loc) · 3.23 KB
/
Math
File metadata and controls
116 lines (97 loc) · 3.23 KB
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
# python 2
#
# Homework 3, Problem 3
# List comprehensions!
#
# Name:Shivam jindal
#
# this gives us functions like sin and cos
from math import *
# two more functions (not in the math library above)
def dbl(x):
""" doubler! input: x, a number """
return float(2*x)
def sq(x):
""" squarer! input: x, a number """
return float(x**2)
# examples for getting used to list comprehensions
def lc_mult( N ):
""" this example takes in an int N
and returns a list of integers
from 0 to N-1, **each multiplied by 2**
"""
return [ db(x) for x in range(N) ]
def lc_idiv( N ):
""" this example takes in an int N
and returns a list of integers
from 0 to N-1, **each divided by 2**
WARNING: this is INTEGER division...!
"""
return [(x/2) for x in range(N) ]
def lc_fdiv( N ):
""" this example takes in an int N
and returns a list of integers
from 0 to N-1, **each divided by 2**
NOTE: this is floating-point division...!
"""
return [ float(x)/2 for x in range(N) ]
def interp(low,hi,frac):
""" returns a value frac of the way from low to hi """
return low + (hi-low)*frac
# Here is where your functions start for the homework:
# Step 1, part 1
def unitfracs( N ):
""" this example takes in an int N
and returs a list of N number between 0 and 1
which have same difference
NOTE: this is floating-point division...!
"""
return [float(x)/N for x in range(N)]
def scaledfracs(low,hi,N):
""" this example takes in three value low,hi,and N
and returs a list of N number between low and hi
which have same difference
NOTE: this is floating-point division...!
"""
return [float(low+x*(hi-low)) for x in unitfracs(N)]
def sqfracs(low,hi,N):
""" this example takes in three value low,hi and N
and returs a list of N number which is the
square of its scaled fraction
NOTE: this is floating-point division...!
"""
return [sq(x) for x in scaledfracs(low,hi,N)]
def f_of_fracs(f,low,hi,N):
""" this example takes in four value f as function,
low and hi is limit and N is number of division
and returs a list of N number which is the
result of f of its scaled fraction
NOTE: this is floating-point division...!
"""
return [f(x) for x in scaledfracs(low,hi,N)]
def integrate(f,low,hi,N):
""" integrate returns an estimate of the definite integral
of the function f (the first input)
with lower limit low (the second input)
and upper limit hi (the third input)
where N steps are taken (the fourth input)
integrate simply returns the sum of the areas of rectangles
under f, drawn at the left endpoints of N uniform steps
from low to hi
"""
diff = (hi-low)/float(N)
return sum(f_of_fracs(f,low,hi,N))*diff
def c(x):
""" c is a semicircular function of radius two """
return (4-x**2)**0.5
"""
Q.2
integrate(c,0,2,200) is 3.15117
integrate(c,0,2,2000) is 3.14258
"""
"""
Q.2
if N goes to infinity then integral become zero
because if N zero then diff = (hi-low)/float(N) is zero then
sum(f_of_fracs(f,low,hi,N))*diff become zero so integral become zero.
"""