Professional Documents
Culture Documents
2010 Numerical Computing
2010 Numerical Computing
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_*
>
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