You are on page 1of 3

> ## CHRIS TERRY

## INTRO TO NUMERICAL COMPUTING 2011


## QUESTION 1 A)
evalf[2](5*(1/6));
evalf[2](1001-2);
0.83
1000.
> ## QUESTION 1 B)
## k 1
1
2
3/2
## i
1
2
3
4

(1)

## return 3/2, 4
> ## QUESTION 1 C)
doubFactorial:=proc(n::integer)
local i, factsum:
if irem(n,2)=0 then error("n must be odd.") end if:
if n<0 then error("n must be non-negative.")end if:
factsum:=1:
for i from 1 to n by 2 do
factsum:=factsum*i:
end do:
return factsum:
end proc:
> ## QUESTION 2 A)
S_0:=evalf(ln(2));
S_1:=evalf(2*S_0-1/(0+1));
S_2:=evalf(2*S_1-1/(1+1));

(2)
> ## QUESTION 2 B)
## It's possible to make the procedure, but because of the
magnification of the truncation error in the approximation of ln
(2) by 8s.f, the procedure's output becomes unreliable very
quickly (unstable iteration). So it's not really possible.
> ## QUESTION 2 C)
S[11]:=2:
for i from 10 to 0 by -1 do
S[i]:=evalf((S[i+1]+1/(i+1))/2);
end do:
## Backwards iteration is stable because the trunction error
decreases each iterate.
> ## QUESTION 3 A)
phi:=x->x-(cos(x^2)-x^2)/(-2*sin(x^2)-2*x);

>
> ## QUESTION 3 B)
with( LinearAlgebra):
with(VectorCalculus):
phi:=(x::Vector) -> <x[1],x[2]>- (Jacobian([cos(x[1])+sin(x[2]),
sin(x[1])+cos(x[2])],[x[1],x[2]]))^(-1).<cos(x[1])+sin(x[2]),sin
(x[1])+cos(x[2])> ;
(3)

> ## QUESTION 3 C)
INVf:=proc(x::realcons, tol::realcons)
local xold, xnew, diff, i, imax, phi:
xold:=x:
diff:=tol+1:
imax:=10000:
phi:=z->z -(z+exp(z)-x)/(1+exp(z)):
for i from 1 to imax do
xnew:=phi(xold):
diff:=abs(xold-xnew):
if diff<tol then
return xnew:
else
xold:=xnew:
end if:
end do:
error("Too many iterations."):
end proc:
> ## QUESTION 4 A)
simple_riemann:=proc(f,a,b,n)
local h;
h:=(b-a)/n;
return add(f(a+h*k),k=0..n-1)*h;
end proc:
simple_trapeze:=proc(f,a,b,n)
local h,s;
h:=(b-a)/n;
s:=f(a)+f(b);
s:=s+2*add(f(a+k*h),k=1..n-1);
return s*h/2;

end proc:
simple_simpson:=proc(f,a,b,n)
local h,m,s;
h:=(b-a)/n;
m:=n/2;
s:=f(a)+f(b);
s:=s+2*add(f(a+2*k*h),k=1..m-1);
s:=s+4*add(f(a+(2*k+1)*h),k=0..m-1);
return s*h/3;
end proc:
g:=x->sqrt(1+sin(Pi*x)):
> I_riemann:=evalf(simple_riemann(g, 0.0, 1.0, 2));
I_trapeze:=evalf(simple_trapeze(g, 0.0, 1.0, 2));
I_simpson:=evalf(simple_simpson(g, 0.0, 1.0, 2));
evalf(int(g(x), x=0..1));

1.273239544

(4)

1.273239544

(5)

> ## QUESTION 4 B)
h:=x-> (x-2)^2:
k:=2:
difference:=10:
xold:=evalf(simple_simpson(h, 0.0, 1.0, k)):
while difference>10^(-2) do
k:=k+2:
difference:=abs(evalf(simple_simpson(h, 0.0, 1.0, k))-xold):
xold:=evalf(simple_simpson(h, 0.0, 1.0, k)):
end do:
k;
4
> # 4 intervals.

(6)

You might also like