Vous êtes sur la page 1sur 4

> ## CHRIS TERRY

## 2010 NUMERICAL COMPUTING


## QUESTION 1 A)
evalf[2]((1/3)-(1/4));
evalf[2](101-3);
0.083
98.
> ##
##
##
##
##
> ##

QUESTION 1 B)
k 1/2
3
i
1
2

7/3

(1)

3+3/7
3

return 3+3/7, 4
QUESTION 1 C)

my_fib:=proc(n::integer)
local old_1, old_2, fnew, i:
if n>=0 then
old_1:=1:
old_2:=1:
fnew:=2:
if n=0 or n=1 then
return 1:
else
for i from 3 to n do
old_1:=old_2:
old_2:=fnew:
fnew:=old_1 + old_2:
end do:
return fnew:
end if:
else
error("The parameter n must be non-negative."):
end if:
end proc:
5
> ##
##
##
##
##
##
##

QUESTION 2 A)
phi_A(x) = x+ (f(x))^2, so at x_*
phi_A( x_*) = x_* + 0^2 = x_*
hence phi_A(x) satisfies the fixed point condition.
phi_B(x) = x + f(x)/(1 + (f'(x))^2), so at x_*

## phi_B(x) = x_* + 0/(1 + (f'(x))^2).


## This has potential to be undefind if f'(x)=i, but assuming f'
(x) is real, phi_B(x) satisfies the fixed point condition.
##
> ## QUESTION 2 B)
##
## stability condition: |phi'(x_*)| < 1.
##
## phi_A'(x_*)= 1 which is not less than 1 meaning the error does
not decrease but stays the same.
##
##################################################
## |phi_B'(x_*)| = |1 + (f'(x_*))/(1+(f'(x_*))^2)|
##
## f'(x)< -1/2
## 2Bii) NOT CHECKED WITH JUST
##################################################
##
##
> ## QUESTION 2 C)
phi_x:= x -> x - (x^2+1- exp(x))/(2*x-exp(x)):
> ## QUESTION 3 A)
##
## matrix i) would not converge as the modulus of the eigenvalues
are not distinct.
## matrix ii) would fail as its eigenvalues are complex and our
version of the power method does not cater for complex
eigenvalues.
##
##
##
##
##
> simple_pm:=proc(A,v,tol)
local w,u,lambda;
# transcribe input vector
w:=v;
# assign values to lambda and u so that loop starts
lambda:=0.0;
u:=v;
# iteration loop with termination condition
while LinearAlgebra[VectorNorm](w-lambda*u,Euclidean)>tol do
# normalisation
u:=w/LinearAlgebra[VectorNorm](w,Euclidean);
# iteration of vector
w:=A.u;
# estimate of eigenvalue
lambda:=w.u;
end do;
# output
return lambda,u;
end proc:
> pindex:=proc(v)

>
local i,ndim;
# length of input vector
ndim:=LinearAlgebra[Dimension](v);
# loop over all components
for i from 1 to ndim do
# checks for nonzero component
if abs(v[i])>0 then
# output
return i;
end if;
end do;
end proc:
> simple_deflat:=proc(A,u)
local p,ap;
# computes component
p:=pindex(u);
# pth row of input matrix
ap:=A[p];
# output, deflated matrix
return A-(u.ap)/u[p];
end proc:
> ## QUESTION 3 B)
A:=<<2| -3| 6> , < -3/2| 7/2| -3> , <-3/2| 3| -4>>:
u:=<0,2,1>:
B:=simple_deflat(A, u);
B := Matrix(3, 3, {(1, 1) = 2, (1, 2) = -3, (1, 3) = 6, (3, 1) =
-3/4, (3, 2) = 5/4, (3, 3) = -5/2}, datatype = anything, storage
= rectangular, order = Fortran_order, shape = [])
> ## QUESTION 3 C)
C:=<<5/6 | 1/3>, <1/6 | 2/3>>:
v:=<1,0>:
simple_pm(C, v, 0.001)[2];
Vector[column](2, {1 = (41/553780506069556451727106)*
11958532409^(1/2)*749175737^(1/2)*47046905^(1/2)*2968985^(1/2)*
189289^(1/2)*73^(1/2)*5^(1/2)*65^(1/2)*26^(1/2), 2 =
(7/189650858242998784838050)*11958532409^(1/2)*749175737^(1/2)*
47046905^(1/2)*2968985^(1/2)*189289^(1/2)*73^(1/2)*5^(1/2)*65^
(1/2)*26^(1/2)}, datatype = anything, storage = rectangular,
order = Fortran_order, shape = [])
> ## 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->1/(sqrt(1+x^4)):
> I_riemann:=evalf(simple_riemann(g, -1.0, 1.0, 2));
I_trapeze:=evalf(simple_trapeze(g, -1.0, 1.0, 2));
I_simpson:=evalf(simple_simpson(g, -1.0, 1.0, 2));
evalf(int(g(x), x=-1..1));
I_riemann := 1.707106781
I_trapeze := 1.707106782
I_simpson := 1.804737854
1.854074677
> ## QUESTION 4 B)
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:
f:=x->x:
while 0.5-simple_riemann(f, 0.0, 1.0, i)>0.01 do
i:=i+1:
end do:
i;
50

Vous aimerez peut-être aussi