Escolar Documentos
Profissional Documentos
Cultura Documentos
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
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
[f(l) - y(l)Y
5 Determine Ch
C2'
and
C3
fl
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
,--
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
Math 481A
Assignment 1
Joshua Cook
September 4, 2014
4c
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)))
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
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
= 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.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