sample questions for test - 3

We should use this package:

using MTH229

Newton's method

f(x) = x^2 - x - 1
x = 2
x = x - f(x) / f'(x)
1.6666666666666667
f(x) = x^3 - 2x - 5
x = 2
x = x - f(x) / f'(x)
x = x - f(x) / f'(x)
x = x - f(x) / f'(x)
x = x - f(x) / f'(x)
x = x - f(x) / f'(x)
x, f(x)
(2.0945514815423265,-8.881784197001252e-16)
f(x) = x^6 - x^5 - x^4 - x^3 - x^2 - x - 1
x = 2
x = x - f(x) / f'(x)
x = x - f(x) / f'(x)
x = x - f(x) / f'(x)
x = x - f(x) / f'(x)
x, f(x)
(1.9835828434243263,6.661338147750939e-16)

(It only takes 4 steps. If you do another, x's value isn't changed.)

f(x) = x^20 - 1
x = 2
newton(f, x, verbose=true)  # 19 steps
1.0

We need to plot to find a good starting point:

f(x) = exp(x)
g(x) = x^6
h(x) = f(x) - g(x)
plot(h, 10, 20) # can't tell but looks like h(16) is negative
plot(h, 16, 20) # answer is around 17 -- use that
newton(h, 17)
16.99888735229605

extrema

f(x) = 3x^4 - 26x^3 + 60x^2 - 11
plot(f, 1, 5)   # two critical points
cps = fzeros(f', 1, 5)
f(1), f(2.5), f(4.0), f(5)
(26,74.9375,53.0,114)

Absolute maximum is at the endpoint $x=5$, absolute minimum is also at a critical point $x=1$

Let $w$ be the length of a partition, $h$ the height. Then $2h + 5w = 500$. Area is just $h\cdot w$:

h(w)= (500 - 5w)/2
A(w) = w * h(w) # 0 <= w <= 100
plot(A, 0, 100)  # max is lone critical point -- we can use bisection
a = fzero(A', 0, 100)
a, h(a)   # dimensions
(50.0,125.0)

We have square cut from corner of size $x$. When assembled box will have height $x$ and $(3-2x)$ by $(4-2x)$. So:

V(x) = x * (3 - 2x) * (4 - 2x)  # 0 <= x <= 3/2
plot(V, 0, 3/2)  # max volume is at a critical point near x=0.5
a = fzero(V', 0.5)
a, (3-2a), (4 - 2a) # dimensions
(0.5657414540893351,1.8685170918213299,2.86851709182133)

We have perimeter is 12. If we label the rectangle with y for height, x for width, then $P = x + 2y + (1/2) \cdot 2\pi(x/2)$ (why?) and the area is $A = x\cdot y + 1/2 \pi (x/2)^2$. (Again, as r of the semicircle is $x/2$.

r(x) = x/2
y(x) = ( 12 - (x + (1/2)*2 * pi * r(x)) ) / 2
A(x) = x * y(x) + 1/2 * pi * r(x)^2  # 0 <= x <= 12/(1 + pi/2)
plot(A, 0, 12/(1 + pi/2))  # max at a critical point near 3.0
a = fzero(A', 3.0)
a, y(a)   # dimensions
(3.360594921069344,1.680297460534672)
f(x) = sqrt(x)
x0, y0 = 4, 0
d(x) = sqrt( (x - x0)^2 + (f(x) - y0)^2 )
plot(d, 1, 8)   # smallest value at critical point near 3.5
a = fzero(d', 3.5)
a, f(a)
(3.5,1.8708286933869707)

Integration

f(x) = x^2
delta = (1-0)/3
f(0)*delta + f(1/3)*delta + f(2/3)*delta

Left riemann sum:

f(x) = x^2
delta = (1-0)/3
lr = f(0)*delta + f(1/3)*delta + f(2/3)*delta
0.18518518518518517

Right-Riemann sum

f(x) = x^2
delta = (1-0)/3
rr = f(1/3)*delta + f(2/3)*delta + f(3/3)*delta
0.5185185185185185
rr - lr
0.3333333333333333
tr = riemann(f, 0, 1, 3, method="trapezoid")
sr = riemann(f, 0, 1, 3, method="simpsons")
tr, sr
(0.35185185185185186,0.33333333333333326)
lr - 1/3, rr - 1/3, tr - 1/3, sr - 1/3  # Simpson's wins
(-0.14814814814814814,0.18518518518518517,0.018518518518518545,-5.551115123125783e-17)

(This isn't really fair. Simpsons is exact for quadratic polynomials.)

riemann(airy, 0, 2, 10, method="right")
0.28120907426713126
ans, err = quadgk(airy, 0, 2)
(0.31253275578067924,3.414490912234669e-13)
f(x) = pi * (2 - x^2)^2
quadgk(f, 0, 1) # 9.005898940290741
(9.005898940290741,1.7763568394002505e-15)
f(x) = pi * (2 - sin(x)^2)^2
quadgk(f, 0, 1)
(9.529465228074825,2.673417043297377e-12)

The quadgk function is exact for polynomials, but not so for non-polys. So we would be really surprised if the last one were smaller.

r(h) = (1 + h)^(3/2)
dv(h) = pi * r(h)^2
quadgk(dv, 0, 10)
(11498.229112138642,0.0)
r(h) = 1 - (1/2) * exp(h/20) * sin(3pi/20 * h)
dv(h) = pi * r(h)^2
quadgk(dv, 0, 10)
(32.88695672273968,8.27523827240384e-10)