Docsity

MAT 305 Assignment 3: Pseducode & Python for Math Functions - Prof. John Perry, Assignments of Mathematics

Toggle sidebar
bg1
INDIVIDUAL ASSIGNMENT 3
MAT 305 FALL 2009
Due date: 1 Dec 2009
For each problem, devise properly-formatted pseducode as well as Python code. Submit your
solutions as Sage worksheets, sharing with
mat305_fa2009
. Be sure that your name appears
somewhere on the worksheet. Where appropriate, split the task into subtasks and write separate
psuedocode and Python code for each subtasks. Each Python-coded function must include at
least one comment indicating the purpose of the function.
1. Write a non-recursive function to calculate n!, where nis a positive integer. The function
must accept nas an input.
2. Write a function to compute the Riemann sum approximation of ยดb
af(x)dx using the Mid-
point Rule: For the i-th rectangle on the interval ๎˜‚xiโˆ’1,xi๎˜ƒ, use the value f๎˜€mi๎˜at the mid-
point mi=๎˜€xiโˆ’1+xi๎˜/2, for the height of the rectangle. The function must accept as input
a function f, the number of partitions N, and the endpoints of the interval aand b.
3. Write an interactive function to compute the Riemann sum approximation of ยดb
af(x)dx
using the Midpoint Rule. This interactive function should accept values of f,a, and bin
input boxes, and Non a slider from 1 to 10. It should plot fand the rectangles used to
approximate the integral, then print the approximate area. The rectangles should appear in a
different color with some translucence so as to see the plot of f, but you need not include a
color selector. You can either print the approximate area below the graph, or (bonus!) display
it as a text object inside the plot.
4. For a point P=(a,f(a)) on the graph of f, the normal line at Pis the line through Pwhich
is perpendicular to the tangent line at P. Recall that two lines are perpendicular if and only
if their slopes are negative reciprocals of each other. Since the slope of the tangent line is
m=f0(a), the slope of the normal line is โˆ’1/m=โˆ’1/f0(a). Thus the equation for the
normal line at (a,f(a)) is
y=f(a)โˆ’1
f0(a)(xโˆ’a).
In the exceptional case when f0(a)=0 (i.e., the tangent line is horizontal), the normal line is
vertical and has equation x=a.
In analogy with the sequence of secant lines, write a function to plot a sequence of five normal
lines Nito the graph of fat points ๎˜€a+hi,f๎˜€a+hi๎˜๎˜,i=1, ..., 5, with h1>ยท ยท ยท >h5>0
and h5small. Also plot the normal line Nat (a,f(a)). By limiting the domains/ranges in the
plot
and
show
commands, choose suitable lengths for the normal lines Niso that (i) they all
intersect the normal line N, (ii) they start at the point ๎˜€a+hi,f๎˜€a+hi๎˜๎˜ on the curve, and
(iii) they are not so long as to make the graph of ftoo small. Plot the graph of f(on the given
interval [c,b]) and all six normal lines in the same picture and clearly label which normal line
corresponds to which value of hi.
1
pf2

Partial preview of the text

Download MAT 305 Assignment 3: Pseducode & Python for Math Functions - Prof. John Perry and more Assignments Mathematics in PDF only on Docsity!

INDIVIDUAL ASSIGNMENT 3

MAT 305 FALL 2009

Due date: 1 Dec 2009

For each problem, devise properly-formatted pseducode as well as Python code. Submit your solutions as Sage worksheets, sharing with mat305_fa2009. Be sure that your name appears somewhere on the worksheet. Where appropriate, split the task into subtasks and write separate psuedocode and Python code for each subtasks. Each Python-coded function must include at least one comment indicating the purpose of the function.

  1. Write a non-recursive function to calculate n!, where n is a positive integer. The function must accept n as an input.
  2. Write a function to compute the Riemann sum approximation of

ยด (^) b a f^ (x)^ dx^ using the Mid- point Rule: For the i-th rectangle on the interval

xiโˆ’ 1 , xi

, use the value f

mi

at the mid- point mi =

xiโˆ’ 1 + xi

/ 2, for the height of the rectangle. The function must accept as input a function f , the number of partitions N , and the endpoints of the interval a and b.

  1. Write an interactive function to compute the Riemann sum approximation of

ยด (^) b a f^ (x)^ dx using the Midpoint Rule. This interactive function should accept values of f , a, and b in input boxes, and N on a slider from 1 to 10. It should plot f and the rectangles used to approximate the integral, then print the approximate area. The rectangles should appear in a different color with some translucence so as to see the plot of f , but you need not include a color selector. You can either print the approximate area below the graph, or (bonus!) display it as a text object inside the plot.

  1. For a point P = (a, f (a)) on the graph of f , the normal line at P is the line through P which is perpendicular to the tangent line at P. Recall that two lines are perpendicular if and only if their slopes are negative reciprocals of each other. Since the slope of the tangent line is m = f โ€ฒ^ (a), the slope of the normal line is โˆ’ 1 / m = โˆ’ 1 / f โ€ฒ^ (a). Thus the equation for the normal line at (a, f (a)) is

y = f (a) โˆ’

f โ€ฒ^ (a)

(x โˆ’ a).

In the exceptional case when f โ€ฒ^ (a) = 0 (i.e., the tangent line is horizontal), the normal line is vertical and has equation x = a.

In analogy with the sequence of secant lines, write a function to plot a sequence of five normal lines Ni to the graph of f at points

a + hi , f

a + hi

, i = 1,... , 5, with h 1 > ยท ยท ยท > h 5 > 0 and h 5 small. Also plot the normal line N at (a, f (a)). By limiting the domains/ranges in the plot and show commands, choose suitable lengths for the normal lines Ni so that (i) they all intersect the normal line N , (ii) they start at the point

a + hi , f

a + hi

on the curve, and (iii) they are not so long as to make the graph of f too small. Plot the graph of f (on the given interval [c, b ]) and all six normal lines in the same picture and clearly label which normal line corresponds to which value of hi. 1

INDIVIDUAL ASSIGNMENT 3 2

(a) f (x) = (^14)

x^5 โˆ’ 2 x^3 โˆ’ 4 x^2 + x + 4

, with a = 0.3, c = โˆ’1, b = 2.

(b) f (x) = x^2 eโˆ’x^ , with a = โˆ’0.3, c = โˆ’1, b = 2. (c) f (x) = sin (x) โˆ’ 12 cos 3x, with a = 1, c = 0.2, b = 2.