let's see what you can do

164 days ago by MACH

1) Write a function that will return an arithmetic sequence beginning at a, jumping by d, and containing n terms:

a, a+d, a+2d, a+3d, ..., a+(n-1)d

def arithmetic(a,d,n):return [a+d*(k-1) for k in [1..n]] 
       

Notice how zero based indexing can simplify things:

def arithmetic(a,d,n):return [a+d*k for k in range(n)] 
       
var('a d') arithmetic(a,d,10) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[a, a + d, a + 2 \, d, a + 3 \, d, a + 4 \, d, a + 5 \, d, a + 6 \, d, a + 7 \, d, a + 8 \, d, a + 9 \, d\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[a, a + d, a + 2 \, d, a + 3 \, d, a + 4 \, d, a + 5 \, d, a + 6 \, d, a + 7 \, d, a + 8 \, d, a + 9 \, d\right]

2) Write a function that will return a geometric sequence beginning at g, growing by r, and containing n terms:

g, gr, gr^2, gr^3, ..., gr^{n-1}

def geometric(g,r,n):return [g*r^(k-1) for k in [1..n]] 
       

Again, zero based indexing can simplify things:

def geometric(g,r,n):return [g*r^k for k in range(n)] 
       
var('g r') geometric(g,r,10) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[g, g r, g r^{2}, g r^{3}, g r^{4}, g r^{5}, g r^{6}, g r^{7}, g r^{8}, g r^{9}\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[g, g r, g r^{2}, g r^{3}, g r^{4}, g r^{5}, g r^{6}, g r^{7}, g r^{8}, g r^{9}\right]

3) Write a function that will evaluate \sum_{x=a}^{b}f(x).

def sigma(f,a,b): return sum([f(x) for x in [a..b]]) 
       
f(x) = x^2 [f(x) for x in [1..10]] 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[1, 4, 9, 16, 25, 36, 49, 64, 81, 100\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[1, 4, 9, 16, 25, 36, 49, 64, 81, 100\right]
sigma(f,1,10) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}385
\newcommand{\Bold}[1]{\mathbf{#1}}385

4) Write a function to find the area of a trapezoid with bases b_1 and b_2 and height h.

def trapezoid(b_1, b_2, h): return 1/2*(b_1+b_2)*h 
       

5) Write a function that will approximate the area under some algebraic function f(x). The area under f(x) will be approximated by slicing it up into trapezoids:

f(x) = sin(x) a,b,dx = 1/8*pi,7/8*pi,1/8*pi vertices = [[(x,0),(x,f(x)),(x+dx,f(x+dx)),(x+dx,0)] for x in [a,a+dx..b-dx]] show(plot(f,a,b)+sum([polygon(P,color=(random(),random(),random())) for P in vertices]), aspect_ratio=1, axes = False) 
       
def area(f,a,b,dx):