solutions-learn-physics-with-fp-0.1.0.0
Copyright(c) Eric Zoerner 2023
LicenseBSD3
Maintainereric.zoerner@proton.me
Safe HaskellSafe-Inferred
LanguageGHC2021

Chapter04

Description

 
Synopsis

Documentation

type R = Double Source #

alias R to be a Double as an approximate of a real number

type Derivative = (R -> R) -> R -> R Source #

A derivative is a function of a real to real that yields another function of real to real

derivative :: R -> Derivative Source #

Numerical derivation given a small delta

Exercise 4.1

Why does derivative 0.1 not produce exactly the identity function on real numbers?

Because 0.1 cannot be represented exactly in a Double.

See: Why 0.1 Does Not Exist In Floating-Point

>>> derivative 10 f41 1
1.0
>>> derivative 1 f41 1
1.0
>>> derivative 0.1 f41 1
1.0000000000000002

f41 :: R -> R Source #

f(x) = ½x²

Exercise 4.2

(See functions f42, err42 below.)

Evaluating the derivative at different values of x (the second argument):

>>> err42 1 6
0.25
>>> err42 1 0.5
0.25
>>> err42 1 1
0.25

It appears empirically that the error does not depend on x at all, it only depends on a.

Exploring the error for different values of a:

>>> err42 1 1
0.25
>>> err42 0.1 1
2.500000000002167e-3
>>> err42 0.01 5
2.4999998515795596e-5
>>> err42 0.02 5
9.999999903698154e-5
>>> err42 0.03 5
2.249999984940132e-4

After some trial and error, it is deduced that it follows a pattern like:

err(a) = a²/4

This formula is the implementation of the errA function below.

When x = 4, Df (4) = 48. What value of a produces an error of 1 percent at x = 4?

Use the formula we came up with, and solving for a:

a²/4 = (0.01)(48)
a² = (0.01)(48)(4)
a = √ ((0.01)(48)(4))
>>> sqrt $ 0.01 * 48 * 4
1.3856406460551018

When x = 0.1, Df (0.1) = 0.03. What value of a produces an error of 1 percent at x = 0.1?

Again, solving for a:

a²/4 = (0.01)(0.03)
a² = (0.01)(0.03)(4)
a = √ ((0.01)(0.03)(4))
>>> sqrt $ 0.01 * 0.03 * 4
3.4641016151377546e-2

f42 :: R -> R Source #

f(x) = x³

df42 :: R -> R Source #

Derivative of f42, f(x) = 3x².

errF Source #

Arguments

:: (R -> R)

f: The original function

-> (R -> R)

df: The analytical derivative function

-> R

a: The delta ("dt") value

-> R

x: The x value of the function

-> R

Returns the error

Error function: Returns the error between a numerical derivative estimate and the provided analytical derivative.

err42 Source #

Arguments

:: R

a: The delta ("dt") value

-> R

x: The x value of the function

-> R

Returns the error

Error function for f42.

errA Source #

Arguments

:: R

a: The delta value

-> R

Returns the error

Returns the error in terms of a (the "delta"). The expression for the error is apparantly a²/4. This formula was just determined empirically by putting a bunch of a values into err42 and looking at the pattern of results.

Exercise 4.3

Using the expression errA a from Exercise 4.2, using 0.01 for the value of a, solve for x in errA 0.01 >= 0.10 * x.

0.01²/4 ≥ 0.10x
0.10x ≤ 0.01²/4
x ≤ 0.01²/4/0.10
>>> 0.01 ** 2 / 4 / 0.10
2.5e-4

Therefore: x ≤ 2.5e-4

Exercise 4.4

  • - TO DO

f44 :: R -> R Source #

df44 :: R -> R Source #

Exercise 4.5

  • - TO DO