CS 314 Notes for 7/7/2003 to 7/11/2003
Quick Links:
7/7/2003
Solutions of Non-linear Equations
Newton’s Method
- Extend Newton-Raphson method to equation with 2 variables
- Assume the Taylor expansion around x0,0 in function with 2 variables
- Example of Newton's Method:
- f1(x,y) = x2-y-.2 = 0
- f2(x,y) = y2-x-.3 = 0
Interpolation and Polynomial Approximation
--We Want to approximate functions using polynomials
--Used to computer other functios such as sin(x), cos(x), etc.
Taylor Expansion
- Expresses a function with a polynomial of the form
- If we want to approximate the fuction using a polynomial of degree N
- Disadvantages
- Higher order derivatives must be know and maybe hard to compute
Horner's Method for evaluating polynomials (also called Nested Multiplication)
- f(x) = x4 + 2x3 + 3x2 + 2x
- This as 9 multiplications
- f(x) = (((x+2)x+3)x+2)x+1
- But this only has 3 multiplications
7/8/2003
Methods for evaluating Polynomials
Lagrange Approximation
- Assume 2 points (x0,y0) and (x1,y1), let's build the line that passes through these points.
- m = (y1-y0)/(x1-0) = (y-y0)/(x-0)
- y = y0 + (x-x0)(y-y0)/(x1-x0)
- Lagrange used a different method to obtain this polynomial.
- P1(x) = y = y0((x-x0)/(x0-x1)) + y1((x-x0)/(x1-x0))
- From y0((x-x0)/(x0-x1)) you get (1) x = x0 and (0) x = x1
- From y1((x-x0)/(x1-x0)) you get (0) x = x0 and (1) x = x1
- P1(x) = y0(1) + y1
- The terms L1,0 = (x - x1)/((x0-x1) and L1,1 = (x - x0)/((x1-x0) are called "Lagrange Polynomials"
- The same idea can be used for polynomials of degree 2,3,...,n
- P2(x) --> (x0,y0), (x1,y1), and (x2,y2)
- P2(x) = y0((x-x1)(x-x2))/((x0-x1)(x0-x2)) + y1((x-x0)(x-x2))/((x1-x0)(x1-x2)) + y2((x-x0)(x-x1))/((x2-x0)(x2-x1))
- In general:
- Example:
7/9/2003
Methods for evaluating Polynomials
Newton Polynomial Approximation
- In Lagrange, pn-1(x) and pn(x) are not related
- Newton Polynomials can be built incrementally so pn-1(x) can be used to build pn(x)
- p1(x) = a0 + a1(x-x0)
- p2(x) = a0 + a1(x-x0) + a2(x-x0)(x-x1)
- p3(x) = a0 + a1(x-x0) + a2(x-x0)(x-x1) + a3(x-x0)(x-x1)(x-x2)
- pn(x) = a0 + a1(x-x0) + . . . + an(x-x0)...(x-xn-1)
- How to build Pn(x)
- Assume we want to build P1(x) that passes through the points (x0,f(x0)) and (x1,f(x1))
7/10/2003
Polynomial Approximation Continued
Pade Approximations
- They are qutients of polynomials that are used to approximate functions:
- Rn,m = (Pn(x)) / (Qm(x))
- Where 1) Pn(x) = p0 + p1x + p2x2 + . . . + pnxn
- Where 2) Qm(x) = 1 + q1x + q2x2 + . . . + qmxm
- The polynomials are constructied so f(X) and Rn,m agree at x = 0 and also their derviatives agree at (up to N+M) x = 0
- 3) f(x) * Qm(x) - Pn(x) = Z(x)
- Where z(x) is the error function and z(x) goes to zero if:
- Assume that f(x) has Taylor Expansion around x = 0 (Maclavin expansion:)
- 4) f(x) = a0 + a1x + a2x2 + . . . + akxk
- Substitute (4) f(X), (1) Pn(x), and (2) Qm(x) into (3):
- (a0 + a1x + a2x2 + . . . + akxk)(1 + q1x + q2x2 + . . . + qmxm) - (p0 + p1x + p2x2 + . . . + pnxn) = C0x0 + C1x1 + . . . + Cn+mxn+m
- We want the C0x0 + C1x1 + . . . + Cn+mxn+m to go to 0
- Performing multiplications of polynomials and factoringto make left side as small as possible
- (a0-p0) + x(a1+q1a0-p1)+x2(a2+q1a1-q2a0-p2) + . . . = C0x0 + C1x1 + . . . + Cn+mxn+m
- The intial coefficents can be:
- Node 0 a0-p0 = 0
- a1+q1a0-p1 = 0
- . . .
- qman-m + qm-1an-m-1 + . . . + an-pn = 0
- . . .
- qman-m+1 + qm-1an-m+2 + . . . + an+1= 0
- qman + qm-1an+1 + . . . + qn+M= 0
- We get N+m+1 equations and we have N+m+1 variables to compute. Solve first the last 2 equations and you will get the polynomials P and Q.
Example of Pade Approximation Part a: Find Pade's approximation R2,2(x) for f(x) = ln(1+x)/x
- Start with Maclauren expansion:
- f(X) = 1 - (x/2) + (x2/3) - (x3/4) + (x4/5) + . . .
- RN,M(x) = (PN(x)) / (QM(x))
- R2,2(x) = (P2(x)) / (Q2(x))
- From that we get:
- R2,2(x) = (P0 + P1x + P2x2) / (1 + q1x + q2x2)
- Now you must put it into the form f(x)*(QM(x))-(PN(x)) to get:
- x * (1 - (x/2) + (x2/3) - (x3/4) + (x4/5) + ...)(1 + q1x + q2x2) - (P0 + P1x + P2x2) = 0
Multiply through to get :
- (1 - (x/2) + (x2/3) - (x3/4) + (x4/5) + q1x - (q1x2)/2 + (q1x3)/3 - (q1x4)/4 + (q2x2) - (q2x3)/2 + (q2x4)/3) - (P0 + P1x + P2x2) = 0
- From here you obtain 5 equations (By some algebra work)
- (A) 1-p0 = 0
- (B) -.5 + q1 - p1 = 0
- (C) (1/3) - (q1)/2 + q2 - p2 = 0
- (D) (-1/4) + (q1)/3 - (q2)/2 = 0
- (E) (1/5) - (q1)/4 + (q2)/3 = 0
- To solve q1 and q2 multiply (D) by 3 and (E) by 4 and add (D) and (E) together.
- This results in: (1/20) - (q2)/6 = 0 therefore q2 = (3/10)
- Next plug q2 into (D) and you get q1 = (6/5)
- Then plug q2 and q1 it (C) and you obtain p2 = (1/30)
- Plug q1 into (B) and you get p1 = (7/10)
- From (A) you have p0 = 1
- R2,2(x) = (P0 + P1x + P2x2) / (1 + q1x + q2x2)
Plug in values:
- R2,2(x) = (1 + (7/10)x + (1/30)x2) / (1 + (6/5)x + (3/10)x2)
For x=1
- R2,2(x) = (1 + (7/10)(1) + (1/30)(1)2) / (1 + (6/5)(1) + (3/10)(1)2) = .6933
Example Part b: Give Approximation from above obtain an approximation for ln(1+x)
- ln(1+x)/x = R2,2(x) -----> ln(1+x) = R2,2(x) * x
- x * ((1 + (7/10)x + (1/30)x2) / (1 + (6/5)x + (3/10)x2))
Multiply the x through to get:
- (x + (7/10)2 + (1/30)x3) / (x + (6/5)x2 + (3/10)x3)
Multiply by (30/30) to get rid of the fractions and you find that ln(x + 1) is approximately equal to:
- (30x + 212 + x3) / (30x + 36x2 + 9x3)
7/11/2003
Curve fitting
Given a set of points (x0,y0)...(xn,y0) come up with a curve that best fits the points.
In polynomial approximation the polynomial passes through all points, but in curve fitting it does not pass through all points but atleast close to them.
Ex: An experimental procedure gives a set of points and you would like to obtain a function y = f(x) that relates all the points.
Least Squares Line
- Assume that you have recorded the points (x0,y0)...(xn,y0) and we want to find the line y = Ax + B that best fits the recorded data.
- We have:
- We can express the approximation error as the distance from the line to the point:
- There are several norms that we can use to tell us how far the curve f(x) is from the data.
- Given the points (x0,y0)...(xn,y0) the least square line y = f(x) = Ax + B is the line that mimizes the square error (3) we want to minimize this error so we apply te partial derivatives wit respect to A,B
- Solve (1) and (2) to Obtain A,B for the line y = Ax + B that best fits the data.
Example of Least Square Line
Given xk and yk (Use these values to find the missing values in equations (1) and (2)
k |
xk |
yk |
xk2 |
xk*yk |
1 |
6 |
7 |
36 |
42 |
2 |
9 |
6 |
81 |
54 |
3 |
14 |
3 |
196 |
42 |
4 |
17 |
1 |
289 |
17 |
5 |
21 |
0 |
441 |
0 |
Sum: |
67 |
17 |
1043 |
155 |
- Using (1):
- 1043A + 67B - 155 = 0 (3)
- Using (2):
- Multiply (3) by -5 and (4) by 67 and add them together to get:
- -726A - 364 = 0 ----> A = -.5014
- Plug A into (3) to get:
- (1043)(-.5014) + 67B - 155 = 0 ----> B = 10.11
- Thus:
Curve fitting to the curve: f(x) = Axm
- Sometimes however, instead of fitting to a function f(x) = Ax + B we would like to fit the data points to the curve f(x) = Axm where m is known and A is unknown.
- We also want to minimize the square error so we take the partial derivative with respect to A
Example:
- y = f(x) = Ax3
- Given xk and yk solve for the other variables you need:
k |
xk |
yk |
xk6 |
xk3*yk |
1 |
2 |
5.9 |
47.2 |
64 |
2 |
2.3 |
8.3 |
100.86 |
14.7 |
3 |
2.6 |
10.7 |
188.06 |
308.92 |
4 |
2.9 |
13.7 |
334.13 |
599.82 |
5 |
3.2 |
17 |
557.06 |
1073.74 |
Sum: |
|
|
1227.44 |
2056.28 |
- Plug in the values to get:
- A = (10227.44) / (2056.28) ----> A = .5969
- Thus y = f(x) = .5969x3