Published
Edited
Aug 17, 2019
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
sqrt2 = function(e) {
// for each precision e, we want to estimate sqrt2
// by applying the "secant method" on the function x^2-2
if (e<0) return undefined
let p=1, q=2
while (q-p>e) {
p=(2*p+2)/(p+2)
q=(2*q+2)/(q+2)
}
return [p, q]
}
Insert cell
sqrt2(1/10000)
Insert cell
pi = function(e) {
// Leibniz's formula for pi
// i.e., Taylor series of arctan
if (e<1e-8) return undefined
e/=4
let p=1, q=1-1/3, n=3
while (1/n>e) {
p=q+1/(n+2)
q=p-1/(n+4)
n+=4
}
return [q*4, p*4]
}
Insert cell
pi(1/10000000)
Insert cell
ln2 = function(e) {
// as integral of 1/x from 1 to 2
if (e<1e-7) return undefined
let lower=1/2, upper=1, n=2
while (upper-lower>e) {
for (let i=1; i<n; i+=2) {
lower+=(1/(1+i/n)-1/(1+(i+1)/n))/n
upper-=(1/(1+(i-1)/n)-1/(1+i/n))/n
}
n*=2
}
return [lower, upper]
}
Insert cell
ln2(1/10000)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
exp = function(q, e) {
// QQt is the class for rational functions (see Appendix),
// defined by the coefficients of the numerator
// (and optionally those of the denominator)
if (e<1e-7) return undefined
if (q<0) {
let upper=new QQt(1), lower=new QQt([1, 1])
let n=1, n_fact=1, qn=-q
while (qn/n_fact>e) {
upper.num.push(1/n_fact, 1/(n_fact*(n+1)))
lower.num.push(1/(n_fact*(n+1)), 1/(n_fact*(n+1)*(n+2)))
qn*=-q
n_fact*=(n+1)*(n+2)
n+=2
}
return [lower, upper]
}
else {
let lower=new QQt(1), error=new QQt(0)
let n=1, n_fact=1, qn=q
while (n<=q || qn/n_fact*q/(n-q)>e) {
lower.num.push(1/n_fact)
error.num.push(0)
n++
n_fact*=n
qn*=q
}
lower.num.push(1/n_fact)
error.num.push(1/n_fact)
let upper=lower.add(error.div([1, -1/n]))
return [lower, upper]
}
}
Insert cell
{
let x=1, e1=exp(x, 0.01)
// return e1
return [e1[0].eval(x), Math.E, e1[1].eval(x)]
}
Insert cell
Insert cell
Insert cell
Insert cell
delta = function(q, e) {
if (q==0 || e<=0)
return undefined
else
return [new QQt(q*e, [0, 1]), new QQt(0)] // i.e., qe/t
}
Insert cell
Insert cell
delta1 = function(q, e) {
if (q==0 || e<=0)
return undefined
if (q<0)
return [new QQt(-q*e, [0, 0, 1]), new QQt(0)] // i.e., -qe/t^2
if (q>0)
return [new QQt(0), new QQt(-q*e, [0, 0, 1])]
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more