Sample questions for test 4

using MTH229

Newton

f(x) = x^x - e^x
plot(f, 2, 3.2)
plot!(zero, 2, 3.2)
scatter!([3], [f(3)])

ANS: It is about 2.8. Note you are guessing where the tangent line would hit the graph – not the actual intersection point which is about 2.71.

ANS: Five steps means you need to do x=x-f(x)/f'(x) 5 times:

f(x) = 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 = x - f(x)/f'(x)
x, f(x)
(1.618033988749895,0.0)

The last line displays the value for the approximation, x, and the value of the function at this, f(x). That f(x) is 0.0 says the algorithm has converged and 1.618033988749895 is the approximate zero found.

ANS: This is the same pattern:

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)

Again, the small value for f(x) indicates this algorithm has converged.

ANS: We can do this with newton:

f(x) = x^6 - x^5 - x^4 - x^3 - x^2 - x - 1
x = 2
newton(f, x)
1.9835828434243263

ANS: It takes 19 iterations. This is because the second derivative is large until x gets close to the answer of 1.

ANS: Let $h(x) = e^x - x^6$. A graph shows the answer is around 16:

h(x) = e^x - x^6
newton(h, 16)
16.99888735229605

(This problem is kinda fussy. If you pick a bad starting point, say 15, you will not get this answer, but a different one.)

f(x) = x^5 - x - 1
x = 0
plot(f, -1, 1.5)
plot!(zero, -1, 1.5)
scatter!([0], [f(0)])

ANS: No, and really, no. Why? Because the initial point is on the other side of a minimum than the zero. The algorithm may get trapped and it does, try it.

Optimization

ANS: A point $(x,y)$ on the circle determines a rectangle with height $y$ and width=$2x$, so we have $A = 2x \cdot y$ with $x,y$ related by being on a circle: $x^2 + y^2 = 1^2$. So...

A(x) = 2x * sqrt(1 - x^2)
A (generic function with 1 method)

Graphing will show a peak around $0.7$, which we find by looking at the value where the derivative is $0$:

x0 = fzero(A', 0.7)
2*x0, sqrt(1 - x0^2), A(x0)  # width, height, area
(1.414213562373095,0.7071067811865476,1.0)

ANS: Well, we can get the critical points by finding when the derivative is 0:

f(x) = 3x^4 - 26x^3 + 60x^2 -11
cps = fzeros(f', 1, 5)
2-element Array{Float64,1}:
 2.5
 4.0

So we need to look at the function values at the endpoints 1 and 5 and the critical points 2.5 and 4:

f(1), f(5), f(2.5), f(4)
(26,114,74.9375,53)

The largest and smallest happen at endpoints – not critical points!

ANS: Okay, this problem is not my favorite, but it shouldn't be hard: we have the main function $A=x\cdot y$ and the constraint $P=500 = x + 2y$, allowing us to solve for $A$ as a function of $y$ alone by substituting or $x$:

A(y) = (500 - 2y)*y
A (generic function with 1 method)

Oh boy, a parabola with zeros at 0 and 250 – the max is in the middle, so occurs when $y=125$. This makes $x=250$. Don't believe that? Well, maybe this will convince you:

fzero(A', 0, 250)
125.0

ANS: We cut of $d$ by $d$ squares in each corner and will get the resulting volume:

V(d) = (3 - 2d) * (4 - 2d) * d
V (generic function with 1 method)

As all dimensions must be non-negative, we have $0 \leq d \leq 3/2$, so we can look:

cps = fzeros(V', 0, 1.5)
1-element Array{Float64,1}:
 0.565741

The max must occur at this lone critical point, as the values of $V$ at the endpoints are 0:

x0 = cps[1]
V(0), V(1.5), V(x0)
(0,0.0,3.0323024659641433)

ANS: Let the rectangle have dimensions $x$ by $y$ then the circle will have radius $r=x/2$. The perimeter will be $x + 2y + (1/2)\cdot 2\pi r = x + 2y + \pi\cdot x/2$. As this is 12, we see that $y = (12 - x - \pi\cdot x/2)$ giving:

A(x) = x * (12 - pi * x/2)
A (generic function with 1 method)

Again, this is a parabola with zeros at $0$ and $24/\pi$, so the midpoint is the peak and will be at $12/\pi$. So $x$ is $12/\pi$ and:

x = 12/pi
y = (12 - pi * x/2)
6.0

ANS: The distance formula means:

x0, y0 = 4, 0
f(x) = sqrt(x)
d(x) = sqrt((x - x0)^2 + (f(x) - y0)^2)
d (generic function with 1 method)

We start with a guess of $x=4$, and see:

cp = fzero(d', 4)
3.5

The point is then

cp, f(cp)
(3.5,1.8708286933869707)

Integration

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

ANS: by hand means we have:

x0, x1, x2, x3 = 0, 1/3, 2/3, 1
f(x) = x^2
delta = (1-0)/3
lft = f(x0) * delta + f(x1)*delta + f(x2) * delta
rht = f(x1)*delta + f(x2) * delta + f(x3) * delta
lft, rht, rht - lft
(0.18518518518518517,0.5185185185185185,0.3333333333333333)

The difference can be seen or you could directly compute

f(x3) * delta - f(x0) * delta  # why?
0.3333333333333333

ANS: Find the values:

f(x) = x^2
trap = riemann(f, 0, 1, 3, method="trapezoid")
simp = riemann(f, 0, 1, 3, method="simpsons")
trap - simp # their difference
0.0185185185185186

ANS: We just see:

lft - 1/3, rht - 1/3, trap - 1/3, simp - 1/3
(-0.14814814814814814,0.18518518518518517,0.018518518518518545,-5.551115123125783e-17)

(Simpson's will be exact for quadratic and cubic polynomials, the trapezoid exact for linear functions.)

ANS: We can use riemann for this:

riemann(airy, 0, 2, 10)
0.28120907426713126

ANS: quadgk will give a better estimate:

quadgk(airy, 0, 2)
(0.31253275578067924,3.414490912234669e-13)
f(x) = pi * (2 - x^2)^2
quadgk(f, 0, 1)
(9.005898940290741,1.7763568394002505e-15)
f(x) = pi * (2 - sin(x)^2)^2
quadgk(f, 0, 1)
(9.529465228074825,2.673417043297377e-12)

The one with the sin function. A fact about polynomial functions is that quadgk should be fully accurate.

ANS: we first define $f$:

f(x) = exp(x)
f (generic function with 1 method)

We do not do this integral quadgk(f, 0, 4), as that would give the area. Rather, we integrate the related function:

l(x) = sqrt(1 + f'(x)^2)
quadgk(l, 0, 4)
(54.05615249432967,3.033418991549297e-7)

That's it!

ANS: Similarly:

f(x) = sqrt(1 + x^4)
l(x) = sqrt(1 + f'(x)^2)
quadgk(l, -1, 1)
(2.2941933656397393,3.921931668315892e-10)

ANS: This follows the same pattern:

f(x) = sqrt(1 + x^2)
l(x) = sqrt(1 + f'(x)^2)
quadgk(l, -1, 1)
(2.199374827478408,9.352554286579107e-10)