forked from bueler/bueler.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpint.jl
More file actions
30 lines (26 loc) · 661 Bytes
/
expint.jl
File metadata and controls
30 lines (26 loc) · 661 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# plot the integrand and approximate
# the integral
# / 1
# | exp(-x^2/pi) dx
# / 0
# by left-hand, right-hand, and
# trapezoid rules
# example:
# julia> Pkg.add("Winston") # if needed
# julia> using Winston
# julia> include("expint.jl")
N = 1000
dx = (1 - 0) / N;
x = linspace(0,1,N+1)
y = exp(- x.^2 / pi)
p = plot(x,y)
#axis([0 1 0 1]), grid
file(p,"expint-julia.png")
lhand = dx * sum(y[1:end-1])
@printf("lhand = %.15f\n", lhand)
rhand = dx * sum(y[2:end])
@printf("rhand = %.15f\n", rhand)
trap = (dx/2) * sum(y[1:end-1] + y[2:end])
@printf("trap = %.15f\n", trap)
exact = (pi/2) * erf(1/sqrt(pi))
@printf("exact = %.15f\n", exact)