Você está na página 1de 10

Math 481A

Assignment 1

Joshua Cook
September 4, 2014

Section 1.2
1a Code:
from numpy import matrix as mat
from numpy.linalg import inv
A = np.mat("1. 0. 0.; 1. .5 .25;1. 1. 1.")
b = np.mat("1; .666667; .5")
x = inv(A)*b
x
In [1]: from numpy import matrix as mat
In [2]: from numpy.linalg import inv
In [3]: A = mat("1. 0. 0.; 1. .5 .25;1. 1. 1.")
In [4]: b = mat("1; .666667; .5")
In [5]: x = inv(A)*b
In [6]: x
Out[6]:
matrix([[ 1.
],
[-0.833332],
[ 0.333332]])

f (0) = y(0) = 1 = A0 + A1 0 + A2 0
 
 
1
2
1
1
1
=y
= = A0 + A1 + A2
f
2
2
3
2
4
1
f (1) = y(1) = = A0 + A1 1 + A2 1
2


1
1 0 0
A0
1 1 1 A1 = 2
3
2
4
1
A2
1 1 1
2
1

1
1 0 0
A0
A1 = 1 1 1 2
3
2
4
1
A2
1 1 1
2

1
A0
A1 = 5
6
1
A2
3

Math 481A
Assignment 1

Joshua Cook
September 4, 2014

1b
f (x) = (1 + x)1
f 0 (x) = (1 + x)2
f 00 (x) = 2(1 + x)3
y(x) = A0 + A1 x + A2 x2
y 0 (x) = A1 + 2A2 x
y 00 (x) = 2A2
f (0) = y(0) = 1 = A0
0

f (0) = y 0 (0) = 1 = A1
f 00 (0) = y 00 (0) = 2 = 2A2

A0
1
A1 = 1
A2
1

Math 481A
Assignment 1

Joshua Cook
September 4, 2014

1c Code:
from numpy import matrix as mat
from numpy.linalg import inv
A = np.mat("1. .5 .25; 0. 1. 1.; 0. 0. 2.")
b = np.mat("0.6666666666666666;-0.4444444444444444; 0.5925925925925926")
x = inv(A)*b
x
In [1]: from numpy import matrix as mat
In [2]: from numpy.linalg import inv
In [3]: A = mat("1. .5 .25; 0. 1. 1.; 0. 0. 2.")
In [4]: b = mat("0.6666666666666666;-0.4444444444444444; 0.5925925925925926")
In [5]: x = inv(A)*b
In [6]: x
Out[6]:
matrix([[ 0.96296296],
[-0.74074074],
[ 0.2962963 ]])

f (x) = (1 + x)1
f 0 (x) = (1 + x)2
f 00 (x) = 2(1 + x)3
y(x) = A0 + A1 x + A2 x2
y 0 (x) = A1 + 2A2 x
y 00 (x) = 2A2
 
 
 
 
1
2
1
1
1
=y
= = A0 + A1
+ A2
f
2
2
3
2
4
 
 
 
1
4
1
1
= y0
= = A1 + 2A2
= A1 + A2
f0
2
2
9
2
 
 
1
1
16
= y 00
=
= 2A2
f 00
2
2
27

2
A0
1 12 14
3
0 1 1 A1 = 4
9
16
A2
0 0 2
27

1 2
A0
1 12 14
3
A1 = 0 1 1 4
9
16
A2
0 0 2
27

A0
0.96296296
A1 = 0.74074074
A2
0.2962963

Math 481A
Assignment 1

40

Joshua Cook
September 4, 2014

INTRODUCTION TO NUMERICAL ANALYSIS

4 Determine that member y(x) of the set of all linear functions which best approximates the function lex) = x 2 over [0, 1] in the sense that each of the following
quantities is minimized:
(a)

[lex) - y(X)]2 dx

(b) [/(0) - y(O)Y + [I(t) - y(tW


(c) max If(x) - y(x)1
(d)

[f(l) - y(l)Y

x(1 - x)[/(x) - y(x)Y dx

5 Determine Ch

C2'

and

C3

fl

in such a way that the formula

f (x) = x2
= cd(-I) + cd(O) +
y(x) = ax + b

w(x)f(x) dx

c3 f(l)

yields an exact result when/ex) is 1, X, x 2 , and x 3 , and hence also when/ex) is any
linear combination of those functions, for each of the following weighting functions:

4a Code

,--

= I; (b) w(x) = 'V 1 - x 2; (c) w(x) = ,


'V 1 - x 2
integrate
Symbol
Section
1.3
init_printing
latex
6 Let S = Uo + UI + ... + Uk + R k for k = 0,1, .... By noticing that
diff
matrix as mat
deduce inv
that if R n and R n - I have opposite signs, then R n is smaller than Un in magimport
(a) w(x)

from sympy import


from sympy import
from sympy import
from sympy import
from sympy import
from numpy import
from numpy.linalg
nitude, and is %pposite sign, whereas ifalso R n and R n + 1 have opposite signs, then
init_printing
R n is also smaller than U n+ I in magnitude, and is of the same sign. (This is often
x = Symbol(x)
known as Steffensen's error test.)
a = Symbol(a)
7 Let Sk = Vo - VI + V2 - ... + (-l)k-I vk _ 1 for k = 1, ... , where all v's are
b = Symbol(b)
positive. Assume also that vk+ I < Vk for all k, and that Vk -+ 0 as k -+ 00. Show
def f(x):
that S2k is positive and increasing with k, but that S2k cannot exceed vo. Hence
deduce that S2k tends to a limit as k -+ 00. Show also that S2k+1 tends to the
return x**2
same limit, and hence that the series
(-l)k vk then converges to a limit S.
def y(x,a,b):
Finally, show that the truncation error R k is of the same sign as the first neglectreturn a*x+b
ed term and is smaller than that term in magnitude. (Notice that any finite
def f1(a,b):
number of terms not satisfying the stated requirements may be added to the
return integrate((f(x)-y(x,a,b))**2,(x,0,1))
series initially, without impairing its convergence.)
8 Suppose that the alternating series
print(latex(diff(f1(a,b),a)))
print(latex(diff(f1(a,b),b)))
S = Vo - VI + V2 - ... '"
(-l)kvk
k=O
A = np.mat(".6666666 1.; 1. 2.")
b = np.mat(".5; .6666666")
inv(A)*b

2
3

1

2
a2
a
2b 1
f (x) y(x) dx =
+ ab + b2
+
3
2
3
5
0
2a
1
f1a =
+b
3
2
2
f( 1b) = a + 2b
3
   1
1 a
= 22
2 b
3
1  1 
  2
a
1
2
= 3
2
b
1 2
3
   
1
a
=
b
16
Z

f1 =

Math 481A
Assignment 1

Joshua Cook
September 4, 2014

4b
  
 2

2

2

1
1
f (0) y(0) + f
+ f (2) y(2) = b2 +
y
2
2
f1a = 8.5a + 5.0b 16.25

8.5
5

f1b = 5.0a + 6b 8.5


  

5 a
16.25
=
6 b
8.5
  

a
2.11538
=
b
0.34615

from sympy import integrate


from sympy import Symbol
from sympy import init_printing
from sympy import latex
from sympy import diff
from numpy import matrix as mat
from numpy.linalg import inv
init_printing
x = Symbol(x)
a = Symbol(a)
b = Symbol(b)
def f(x):
return x**2
def y(x,a,b):
return a*x+b
def f1(a,b):
return (f(0)-y(0,a,b))**2+(f(.5)-y(.5,a,b))**2+(f(2)-y(2,a,b))**2
print(latex(diff(f1(a,b),a)))
print(latex(diff(f1(a,b),b)))
A = mat("8.5 5.; 5. 6.")
b = mat("16.25; 8.5")
inv(A)*b

Math 481A
Assignment 1

Joshua Cook
September 4, 2014

4c

f3 (x) = max |f (x) y(x)|


0x1

f3 (0) = b
f3 (a/2) = a b + 1
f3 (1) =

a2
b
4

b
max f3 (x) =
a b + 1

a2 b
4

0
a
+
b
=1
min max f3 (x) =
2
a = 4b
from sympy import integrate
from sympy import Symbol
from sympy import init_printing
from sympy import latex
from sympy import diff
from numpy import matrix as mat
from numpy.linalg import inv
init_printing
x = Symbol(x)
a = Symbol(a)
b = Symbol(b)
def f(x):
return x**2
def y(x,a,b):
return a*x+b
def f1(x,a,b):
return f(x)-y(x,a,b)
print(latex(f1(0,a,b)))
print(latex(f1(1,a,b)))
print(latex(f1(a/2,a,b)))

: b < a 1 and b < a4


2
: b a 1 and a4 < a 1
2
: b a 1 and a4 a 1
: f3 (x) = b
: f3 (x) = a b + 1
2
: f3 (x) = a4 b

Math 481A
Assignment 1

Joshua Cook
September 4, 2014

5
f (x) =
-1
0
1

1
1
1
1

x
-1
0
1

x2
1
0
1

x3
-1
0
1

5a
1

f (x) dx = c1 f (1) + c2 f (0) + c3 f (1)


1

2 = c1 + c2 + c3
0 = c1 + c3
2
= c1 + c3
3
0 = c1 + c3

c1
1/3
c2 = 4/3
1/3
c3
5b
Z

1 x2 f (x) dx = c1 f (1) + c2 f (0) + c3 f (1)

= c1 + c2 + c3
2
0 = c1 + c3

= c1 + c3
8
0 = c1 + c3


/16
c1
c2 = 3/8
c3
/16
5c
Z

1
f (x) dx = c1 f (1) + c2 f (0) + c3 f (1)
1 x2
= c1 + c2 + c3
0 = c1 + c3

= c1 + c3
2
0 = c1 + c3

c1
/4
c2 = /2
c3
/4

Math 481A
Assignment 1

Joshua Cook
September 4, 2014

Section 1.3
8

1
1
1
1
1X
v0 + (v0 v1 ) (v1 v2 ) + . . . = v0 +
(1)k (vk vk+1 )
2
2
2
2
2
=
=

k=0

1
1
v0 +
2
2

k=0

1
1
v0 +
2
2

1X
(1)k vk+1
2

(1)k vk

1
2

(1)k vk

k=0

k=0

(1)k1 vk1+1

k1=0

k=1

k=1

1
1
1X
1X
= v0 + v0 +
(1)k vk +
(1)k vk
2
2
2
2
= v0 +

(1)k vk

k=1

(1)k vk

()

k=0

S=

(1)k

k=0

=
=

1
k+1




1
1 1X
1
(1)k
+

2 2
k+1 k+2
1 1
+
2 2

k=0

(1)k

k=0

1
(k + 1)(k + 2)

"

1 1 1 1X
2
= +
+
(1)k
2 2 4 2
(k + 1)(k + 2)(k + 3

k=0

1X

5
+
8 2

(1)k

k=0

1
(k + 1)(k + 2)(k + 3)




5 1 1
1X
3
= +
+
(1)k
8 2 12 2
(k + 1)(k + 2)(k + 3)(k + 4)
k=0

3X

1
(k + 1)(k + 2)(k + 3)(k + 4)
k=0


2 3 1
1
1
1
1
= +

+
3 4 24 120 720 5040 40320
=

2
+
3 4

(1)k

= 0.692578125

Math 481A
Assignment 1

Joshua Cook
September 4, 2014

Section 1.4 and 1.5


Given f (x) = x1/64 ,

|E(f (N ))| |f 0 ()max |E(N )|


1
|2.460001/64 2.464991/64 |
(2.46499)63/64 0.00499
64
0.0 3.16304528619e 05

Section 1.6
24
a) machine result: 163.678
actual result: 163.638
absolute error: .4e-1
relative error: 0.000244442000024
from math import log10, floor
def round_to_4(x):
return round(x, 3-1*int(floor(log10(x))))
x_1 = 0.1234e3
x_2 = 0.3456e2
x_3 = 0.5678e1
op_1 = x_1+x_2
rounded = round_to_4(op_1)
all_ops = x_1+x_2+x_3
op_2 = rounded+x_3
abs_err = abs(all_ops-op_2)
rel_err = abs(abs_err/all_ops)
print op_1,rounded,all_ops,op_2,abs_err,rel_err
a) machine result: 163.64 actual result: 163.638
absolute error: .2e-1
relative error: 1.22221000013e-05
from math import log10, floor
def round_to_4(x):
return round(x, 3-1*int(floor(log10(x))))
x_1 = 0.1234e3
x_2 = 0.3456e2
x_3 = 0.5678e1
op_1 = x_3+x_2
rounded = round_to_4(op_1)
all_ops = x_1+x_2+x_3

Math 481A
Assignment 1
op_2 = rounded+x_1
abs_err = abs(all_ops-op_2)
rel_err = abs(abs_err/all_ops)
print op_1,rounded,all_ops,op_2,abs_err,rel_err
a) machine result: 24214.989312 actual result: 24216.67
absolute error: 1.680688 relative error: 6.94069271864e-05
from math import log10, floor
def round_to_4(x):
return round(x, 3-1*int(floor(log10(x))))
x_1 = 0.1234e3
x_2 = 0.3456e2
x_3 = 0.5678e1
op_1 = x_1*x_2
rounded = round_to_4(op_1)
all_ops = x_1*x_2*x_3
op_2 = rounded*x_3
abs_err = abs(all_ops-op_2)
rel_err = abs(abs_err/all_ops)
print op_1,rounded,all_ops,op_2,abs_err,rel_err

Joshua Cook
September 4, 2014

Você também pode gostar