Math 308: Bridge to Advanced Math Programming Assignment 1 solution

$15.00

Original Work ?

Download Details:

  • Name: Assignment_1.zip
  • Type: zip
  • Size: 106.41 KB

Category: You will Instantly receive a download link upon Payment||Click Original Work Button for Custom work

Description

5/5 - (3 votes)

Problems. Inside the file pa1.py place three functions as described below which solve each of the
following problems. Only include these three functions, and be sure to title them as described in
the problems. (Improperly titled functions will not be called properly.) To write these functions,
it should be sufficient to understand the Basic mathematics document on the course programming
page.
1. Write a function quadratic maximum(b,c) which takes two numbers b and c. It should return
the maximal value of the function f(x) = c + bx − x
2
.
Examples of successful input/output:
>>> quadratic_maximum(0,10)
10.0
>>> quadratic_maximum(2,0)
1.0
>>> quadratic_maximum(5,13)
19.25
2. An integer p ≥ 2 is prime if the only positive numbers which divide it evenly are itself and one.
Write a function is prime(p) which takes as input an integer p ≥ 2 and returns True if p is
prime and False if p is not prime.
Hints: The number p is not prime if and only if there is an integer n with 2 ≤ n < p so that
the remainder when dividing p by n is zero.
Example of successful input/output:
>>> is_prime(7)
True
>>> is_prime(25)
False
3. Newton’s method is a very efficient way to find a root of a differentiable function f starting with
a point near the root. The method gives a sequence x0, x1, x2, . . . of numbers which rapidly
approach the root if the initial point is sufficiently close to the root. The value x0 is the
starting point. Given xk the value of xk+1 by intersecting the x-axis with tangent line to the
graph of f at the point
xk, f(xk)

. That is,
xk+1 = xk −
f(xk)
f
0
(xk)
.
An illustration of this process is shown at the end of this question.
Write a function newtons method(f,df,x0,n) which takes as input a function f : R → R, its
derivative df = f
0
(also a function from R to R), an initial point x0 and an integer n ≥ 1. The
function should return the value xn obtained by iterating Newton’s method n times.
Example of successful input/output:
>>> def f(x):
return 2-x**2
>>> def df(x):
return -2*x
>>> newtons_method(f, df, 1, 10)
1.414213562373095
>>> newtons_method(math.sin, math.cos, 3, 10)
3.141592653589793
Page
Page 3