We should use these packages:
using Plots using Roots Base.ctranspose(f::Function) = D(f)
ctranspose (generic function with 38 methods)
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.)
newton(f, x0, verbose=true)
.)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
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$
The constraint is $2w + h = 500$, the area is $wh$:
h(w)= 500 - 2w A(w) = w * h(w) # 0 <= w <= 250 plot(A, 0, 250) # max is at the lone critical point, near x = 125 a = fzero(A', 125) a, h(a) # dimensions
(125.0,250.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)*2pi(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)) A(x) = x * y(x) + 1/2 * pi * r(x)^2 # 0 <= x <= 12/pi plot(A, 0, 12/pi) # max at a critical point near 2.75 a = fzero(A', 2.75) a, y(a) # dimensions
(2.754697942669256,4.918232647556417)
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)
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
riemann
function from the project.)Need to copy-and-paste this:
function riemann(f::Function, a::Real, b::Real, n::Int; method="right") if method == "right" meth(f,l,r) = f(r) * (r-l) elseif method == "left" meth(f,l,r) = f(l) * (r-l) elseif method == "trapezoid" meth(f,l,r) = (1/2) * (f(l) + f(r)) * (r-l) elseif method == "simpsons" meth(f,l,r) = (1/6) * (f(l) + 4*(f((l+r)/2)) + f(r)) * (r-l) end xs = a + (0:n) * (b-a)/n as = [meth(f, l, r) for (l,r) in zip(xs[1:end-1], xs[2:end])] sum(as) end
riemann (generic function with 1 method)
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.)
f(x) = airy(x)
we wish to estimate the area under its curve between 0 and 2. Use a right-Riemann sum with $n=10$ to do so.riemann(airy, 0, 2, 10, method="right")
0.28120907426713126
f(x) = airy(x)
we wish to estimate the area under its curve between 0 and 2. Use a Julia's quadgk
function do so.ans, err = quadgk(airy, 0, 2)
(0.31253275578067924,3.414490912234669e-13)
quadgk
to approximate the integral $\int_0^1 \pi \cdot (2 - x^2)^2 dx$.f(x) = pi * (2 - x^2)^2 quadgk(f, 0, 1) # 9.005898940290741
(9.005898940290741,1.7763568394002505e-15)
quadgk
to approximate the integral $\int_0^1 \pi \cdot (2 - \sin(x)^2)^2 dx$. Compare the error to the last answer. Which is greater?f(x) = pi * (2 - sin(x))^2 quadgk(f, 0, 1)
(7.646274817115874,1.7763568394002505e-15)
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. In this case they are the same.
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)