Chem 302 Lab Assignment

Post on 11-Jan-2016

31 views 0 download

Tags:

description

Tuesday 2:30 – 4:00 Colin Chris M Johnathan P Craig Jonathan E Alex. Tuesday 4:00 – 5:30 Curtis Matt Chris L Janet Cory Kristen. Chem 302 Lab Assignment. Chem 302 - Math 252. Chapter 1 Solutions of nonlinear equations. Roots of Nonlinear Equations. - PowerPoint PPT Presentation

Transcript of Chem 302 Lab Assignment

Chem 302 Lab Assignment

• Tuesday 2:30 – 4:00ColinChris MJohnathan PCraigJonathan EAlex

• Tuesday 4:00 – 5:30CurtisMattChris LJanetCoryKristen

Chem 302 - Math 252

Chapter 1Solutions of nonlinear equations

Roots of Nonlinear Equations

• Many problems in chemistry involve nonlinear equations

• Linear and quadratic equations are trivial, can be solved analytically

• Cubic and higher order solve numerically

• Present a overview of basic methods

• Not a complete discussion

Successive Approximations

• Simplest method• Want to solve

f(x)=0rearrange into the form

x=g(x)• Use as iteration formula

xi+1=g(xi)• Use initial guess and iterate until self consistent

Successive Approximations

2 3 2 1,2f x x x

2 2

3

xx g x

2

1

2

3i

i

xx

0

2

1

2

2

start with 0

0 20.666667

3

0.666667 20.814815

3

x

x

x

2 3 2 1,2f x x x

2 2

3

xx g x

2

1

2

3i

i

xx

i xi

0 0

1 0.666667

2 0.814815

3 0.887974

4 0.929500

5 0.954656

6 0.970456

7 0.980595

8 0.987189

9 0.991514

10 0.994367

11 0.996255

12 0.997508

13 0.998341

14 0.998895

15 0.999264

16 0.999509

i xi

17 0.999673

18 0.999782

19 0.999855

20 0.999903

21 0.999935

22 0.999957

23 0.999971

24 0.999981

25 0.999987

26 0.999991

27 0.999994

28 0.999996

29 0.999997

30 0.999998

31 0.999999

32 0.999999

Successive ApproximationsSecond root

0

2

1

2

2

2

2

start with 3

3 23.666667

3

3.666667 25.148148148

3

5.148148148 29.501143118

3

Diverges

x

x

x

x

Successive ApproximationsSecond root

0

2

1

2

2

2

2

start with 3

3 23.666667

3

3.666667 25.148148148

3

5.148148148 29.501143118

3

Diverges

x

x

x

x

Microsoft Excel Worksheet

Successive ApproximationsDifferent formula

2 3 2f x x x

3 2x x

Microsoft Excel Worksheet

Will find x=2, will not find x=1

Different formula – different results

No one formula is best

Slow to converge

-5

0

5

10

15

20

25

30

35

-4 -3 -2 -1 0 1 2 3 4 5 6 7 8

Successive ApproximationsHow to find initial guesses?

Grid search

2 3 2f x x x

Course to start

Get finer

Analysis of Convergence

• Each stage of iterationxi+1=g(xi)

• For convergence

x g x

1n nx x

• True root

• Intersection of two functions: x & g(x)

x

yy = x

y =g (x )

Analysis of Convergence

• Four possibilities– Monotonic convergence– Oscillating convergence– Monotonic divergence– Oscillating divergence

Analysis of Convergence

Monotonic convergence

x

yy = x

y =g (x )

x 1

x 2

g (x 1)g (x 0)

x 0

Oscillating convergence

x

y

y = x

y =g (x )

x 0

x 1

x 2

x 3

g (x 0)

g (x 1)

g (x 2)

Monotonic divergence

x

y

y = x

y =g (x )

x 2

x 1

x 0

g (x 2)

g (x 1)

g (x 0)

Oscillating divergence

x

y

y = x

y =g (x )

x 2

x 1

x 0

g (x 2)

g (x 1)

g (x 0)

Analysis of Convergence• Key is g(x)

• Mean Value Theorem– If g(x) and g(x) are continuous on the interval [a,b] then there exists

an (a<<b) such that g b g a

gb a

2 4 2

2 4

g x x x

g x

2 2

2 2

4 2 4 2

4 2 4 22 4 4

2

g a a a g b b b

b b a ag b g ag b a

b a b ab a

Analysis of Convergence g b g a

gb a

g b g a b a g

1

1 1

Let and i i

i i i i i

a x b x

g x g x x x g

2 1 1i i i ix x x x g

2 1 1i i i ix x x x g

If is bounded then g x g M

2 1 1i i i ix x M x x

Analysis of Convergence2 1 1i i i ix x M x x

2 1 1 0x x M x x

23 2 2 1 1 0x x M x x M x x

34 3 3 2 1 0x x M x x M x x

12 1 1 1 0

ii i i ix x M x x M x x

To converge LHS → 0

If M < 1 guaranteed to converge

1g x Sufficient but not necessary

Speed of Convergence

2 3

( ) ( ) ( )2! 3!

x x x xg x g x g x x x g x g x

Taylor expansion about root

2 3

2 3

1

( ) ( ) ( )2! 3!

( ) ( ) ( )2! 3!

i ii i

i ii i

x x x xg x g x g x x x g x g x

x x x xx x g x x x g x g x

For xi

1ix x When close to convergence

•Dominant term will be 1st nonzero derivative

•Order of Convergence

• One of most common methods

• Usually 2nd order convergence

• Generally superior to simple iteration

• Uses function and 1st derivative to predict root (assume f is linear)

Newton-Raphson Method

1 ( )

ii i i

i

f xx g x x

f x

( )if x f x x b

0 ( )

( )

( ) ( )

i

i i i

i i

f x f x x b

f x f x xbx

f x f x

Newton-Raphson Method

2 3 2

( ) 2 3i i i

i i ii i

f x x xg x x x

f x x

2 3 2 1,2f x x x i xi f(xi) f'(xi)0 1.400000 -0.240000 -0.2000001 0.200000 1.440000 -2.6000002 0.753846 0.306746 -1.4923083 0.959397 0.042251 -1.0812054 0.998475 0.001527 -1.0030505 0.999998 0.000002 -1.0000056 1.000000 0.000000 -1.0000007 1.000000 0.000000 -1.0000008 1.000000 0.000000 -1.0000009 1.000000 0.000000 -1.000000

10 1.000000 0.000000 -1.000000

Newton-Raphson Method

• Fairly robust• Need analytic expressions for f(x) and f'(x)

– May be complicated or not available

• Need to evaluate f(x) and f'(x) many times– Maximum efficiency

• f'(x) close to zero will cause problems– Especially important for multiple roots– Need to checks in program

• Value of f'(x)• Max iterations

Newton-Raphson Method

• Test conditions

1 1i ix x

i maxiter

2if x

3if x

Convergence

• NR but use numeric derivative

Secant Method

1

1

i ii

i i

f x f xf x

x x

11

1 1

1

( )i i

i i i i ii i i

i i i i

i i

f x f xg x x x x x

f x f x f x

x f x x f x

f x f x

• Need two points to start

• Similar to Secant Method• Uses two points (one on each side of root) (need search)• Find where function would be zero if linear between two points• During each iteration one point is held fixed (pivot), other is

moved• More stable but slower than NR• If pivot far from root then slow• If pivot close to root then denominator can be small

False-Position Method

• Algorithm– Pick xL & xR (xL < < xR)

– Evaluate

– Calculate

– Calculate

– If then xM is the root

– Replace xL or xR with xM (depends on sign of )

-130

-110

-90

-70

-50

-30

-10

10

30

50

2 2.5 3 3.5 4 4.5 5 5.5 6

x

f(x)

Lf x

False-Position Method

& L Rf x f x

x

L R R LM

R L

x f x x f xx

f x f x

Mf x

Mf x tol

Mf x

Rep

eat

xL xRxM

Mf x

Rf x

x L f(x L ) x R f(x R ) X M f(x M )2.5 55.938 5.5 -112.813 3.4944 29.983

3.4944 29.983 5.5 -112.813 3.9155 3.97643.9155 3.9764 5.5 -112.813 3.9695 0.1833.9695 0.183 5.5 -112.813 3.972 0.00663.972 0.0066 5.5 -112.813 3.9721 0.0002

3.9721 0.0002 5.5 -112.813 3.9721 8E-063.9721 8E-06 5.5 -112.813 3.9721 3E-07

False-Position Method

• Same as FP Method but xM is average of xL & xR

• Drawbacks of both FP & Bisection– Two initial guesses on opposite sides of root

• Multiple or close roots a problem

• f always same sign a problem

– Round-off error as xM gets close to root

• Secant, FP & Bisection methods do not require analtyic expression of f'(x)

Bisection Method

• Want as efficient an algorithm as possible– Efficiency of operations

• +• *• /• Power

– Naive method (10 ×, 4 +)– Most efficient method for polynomial of order m requires m additions and m multiplications

– Nesting (Horner’s Method)

Roots of Polynomials

4 3 24 3 2 1 0 4 3 2 1 0f x a x a x a x a x a a x x x x a x x x a x x a x a

• For polynomial of degree m

Horner’s Method

20 1 2

0

mm k

m kk

f x a a x a x a x a x

• Divide by (x-r)

210 1 2 0

1 2

mmm

m

f x a a x a x a x bb b x b x

x r x r x r

2 10 1 2 1 2 0

m mm mf x a a x a x a x x r b b x b x b

• b0 is f(r)

Horner’s Method 2 1

0 1 2 1 2 0m m

m mf x a a x a x a x x r b b x b x b

2 2 10 1 2 1 2 1

11 2 0

m m mm m m

mm

f x a a x a x a x b x b x b x b x

rb rb x rb x b

2 10 1 1 2 2 3 1

m mm m mb rb x b rb x b rb x b rb b x

1 1

2 2 3

1 1 2

0 0 1

m m

m m m

b a

b a rb

b a rb

b a rb

b a rb f r

m ×, m +

1 1, 2, ,1,0m m

j j j

b a

b a rb j m m

Horner’s Method 2 3 2f x x x

3

2 1 1

1 3 0

0 2 2

i i

f

i a b

1

2 1 1

1 3 4

0 2 6

i i

f

i a b

1

2 1 1

1 3 2

0 2 0

i i

f

i a b

1 is a root

Horner’s Method 3 23 4f x x x

1

3 1 1

2 3 2

1 0 2

0 4 2

i i

f

i a b

2 is a root

2

3 1 1

2 3 1

1 0 2

0 4 0

i i

f

i a b

• If b0 = 0 (i.e. r is a root)

Horner’s Method

• Have factored (x-r) from equation (i.e. reduced order, called deflating)

• Continue to find roots of reduced equation

11 1 2

mmf x b b x b x

2 10 1 2 1 2

m mm mf x a a x a x a x x r b b x b x

Horner’s Method

2 3 2f x x x

1

2 1 1

1 3 2

0 2 0

i i

f

i a b

11 1 2

mmf x b b x b x

1 2f x x

2

3 1 1

2 3 1

1 0 2

0 4 0

i i

f

i a b

21 2f x x x

21 2f x x x

2

2 1 1

1 1 1

0 2 0

i i

f

i a b

2 is a root (i.e. a double

root of original equation)

2 1f x x

Birge-Vieta Method• NR method with f(x) and f'(x) evaluated using Horner’s method

• Once a root is found, reduce order of polynomial 2 1

0 1 2 1 2 0 0m m

m mf x a a x a x a x x r b b x b x b x r h x b

1 1, 2, ,1,0m m

j j j

b a

b a rb j m m

f x h x x r h x

f r h r

1 21 2 2 3 1

m mm mh x b b x b x x r c c x c x c

1 1, 2, ,1m m

j j j

c b

c b rc j m m

0

1

f r b

f r h r c

Birge-Vieta Method

0

1

f r b

f r h r c

1 ( )

ii i i

i

f xx g x x

f x

01

1i i i

bx g x x

c

1

1

0 0 1

1, 2, ,1

m m m

j j i j

j j i j

i

c b a

b a x b

c b x c j m m

b a x b

Birge-Vieta Method 2 3 2f x x x

0 3

2 1 1 1

1 3 0 3

0 2 2

i i i

x

i a b c

01 0

1

2 73

3 3

bx x

c

1 7 / 3

2 1 1 1

1 3 0.667 1.667

0 2 0.444

i i i

x

i a b c

02 1

1

0.4442.333 2.067

1.667

bx x

c

2 2.067

2 1 1 1

1 3 0.933 1.133

0 2 0.071

i i i

x

i a b c

03 2

1

0.0712.067 2.004

1.133

bx x

c

Birge-Vieta Method 4 3 29 2 120 130f x x x x x

Birge-Vieta Method 4 3 29 2 120 130f x x x x x

3 21 12.6001 43.36219 36.1098f x x x x

Birge-Vieta Method

22 11.3715 29.39123f x x x

3 21 12.6001 43.36219 36.1098f x x x x

Example 3 211 39 45f x x x x

Example

• NR 3 211 39 45f x x x x

23 22 39f x x x

3 2

1 2

11 39 45

3 22 39i i

x x xx x

x x

•x0 4.33 3

•x0 > 4.33 5

•Third root?

Try it!!!

• BViteration xi

0 i ai bi ci 4.93 1 1 12 -11 -6.1 -1.21 39 9.11 3.230 -45 -0.361

1 i ai bi ci 5.01176473 1 1 12 -11 -5.988 -0.9761 39 8.988 4.0950 -45 0.048

2 i ai bi ci 5.0001363 1 1 12 -11 -6 -11 39 9 4.0010 -45 5E-04

3 i ai bi ci 53 1 1 12 -11 -6 -11 39 9 40 -45 7E-08

4 i ai bi ci 53 1 1 12 -11 -6 -11 39 9 40 -45 0

iteration xi0 i ai bi ci 4.9

2 1 1 11 -6 -1.1 3.80 9 3.61

1 i ai bi ci 3.952 1 1 11 -6 -2.05 1.90 9 0.902

2 i ai bi ci 3.4752 1 1 11 -6 -2.525 0.950 9 0.226

3 i ai bi ci 3.2382 1 1 11 -6 -2.763 0.4750 9 0.056

4 i ai bi ci 3.1192 1 1 11 -6 -2.881 0.2380 9 0.014

5 i ai bi ci 3.0592 1 1 11 -6 -2.941 0.1190 9 0.004

6 i ai bi ci 3.032 1 1 11 -6 -2.97 0.0590 9 9E-04

7 i ai bi ci 3.0152 1 1 11 -6 -2.985 0.030 9 2E-04

8 i ai bi ci 3.0072 1 1 11 -6 -2.993 0.0150 9 6E-05

9 i ai bi ci 3.0042 1 1 11 -6 -2.996 0.0070 9 1E-05

10 i ai bi ci 3.0022 1 1 11 -6 -2.998 0.004

6 i ai bi ci 3.032 1 1 11 -6 -2.97 0.0590 9 9E-04

7 i ai bi ci 3.0152 1 1 11 -6 -2.985 0.030 9 2E-04

8 i ai bi ci 3.0072 1 1 11 -6 -2.993 0.0150 9 6E-05

9 i ai bi ci 3.0042 1 1 11 -6 -2.996 0.0070 9 1E-05

10 i ai bi ci 3.0022 1 1 11 -6 -2.998 0.0040 9 3E-06

11 i ai bi ci 3.0012 1 1 11 -6 -2.999 0.0020 9 9E-07

12 i ai bi ci 32 1 1 11 -6 -3 9E-040 9 2E-07

3 211 39 45f x x x x

21 6 9f x x x 2 3f x x

Roots of Polynomials

• What about complex roots?– Occur in pairs– Have form + i & – i– Roots of quadratic equation

f(x) = x2 – 2x + 2 + 2

• BV method – we removed factors of x – r.

• Quadratic can be solved analytically – therefore best to remove quadratic factors

Lin-Bairstow Method

2 2 20 1 2 2 3 1 0

m mm mf x a a x a x a x x ux v b b x b x b x u b

2 22 3 1 0

2 3 2 12 3 2 1 1 0

2 3 2 12 3 4 1

2 3 22 3 3 3

2 30 1 2 1 2 3 2 3 4 3 4 5

2

mm

m m mm m m

m mm m

mm

m

x ux v b b x b x b x u b

b x b x b x b x b x b x u b

ub x ub x ub x ub x ub x

vb vb x vb x vb x vb x

b ub vb x b ub vb x b ub vb x b ub vb

x

12 1 1

m mm m m m m mb ub vb x b ub x b

1 1

1 2 2, 3, ,0

m m

m m m

j j j j

b a

b a ub

b a ub vb j m m

Lin-Bairstow Method

1 1

1 1

1 2

1 2

0 0 1 2

0 3 1 21 2

2 1 3

1 1 0 21 2

2 1 3

2, 3, ,1

m m m

m m i m

m m i m

j j i j i j

j j i j i j

i i

i i

i i

c b a

b a u b

c b u c

b a u b v bj m m

c b u c v c

b a u b v b

b c b cu u

c c c

b c b cv v

c c c

• Iteration Scheme

2 4

2

u u vx

Lin-Bairstow Method

• Algorithm– m > 3: determine quadratic roots, reduce order of problem by 2– m = 3: determine linear root then quadratic roots– m = 2: determine quadratic roots– m = 1: determine linear root

1. // Lin Biarstow.cpp

2. #include "stdafx.h"3. #include <fstream>4. #include <conio.h>5. #include <stdio.h>6. #include <math.h>7. #include <stdlib.h>8. #include <string>9. #include <dos.h>10. #include <iostream>11. using namespace std;

12. int _tmain(int argc, _TCHAR* argv[])13. {14. double t=1e-8, *a, *b, *c,u,v,du,dv,r1,r2,ep,f,d;15. int n,i;16. FILE *logfile;

17. if((logfile=fopen("polynomial.txt","wt"))==NULL)18. {19. cout<<"Could not open log file.\n\nPress any key to exit.";20. getch();21. return EXIT_FAILURE;22. }

23. cout<<"Enter order of polynomial: ";24. cin>>n;25. fprintf(logfile,"Roots of the polynomial");

26. a=new double[n+1];27. b=new double[n+1];28. c=new double[n+1];

29. for(i=0;i<n+1;i++){30. cout<<"\nInput coefficient a["<<i<<"] ";31. cin>>a[i];32. }

33. fprintf(logfile,"\n%lf %+lfx",a[0],a[1]);34. for(i=2;i<n+1;i++)fprintf(logfile," %+lfx^%d",a[i],i);

35. cout<<"\n\nRoots of the polynomial are:\n";36. fprintf(logfile,"\n\nRoots are:\n");

37. while (a[n]==0){n--;}

38. // make sure n>339. while(n>3)40. {41. u=0;v=0;42. b[n]=c[n]=a[n];ep=1;43. while(ep>t){44. b[n-1]=a[n-1]+u*b[n];45. c[n-1]=b[n-1]+u*c[n];46. for(i=n-2;i>0;i--)47. {48. b[i]=a[i]+u*b[i+1]+v*b[i+2];49. c[i]=b[i]+u*c[i+1]+v*c[i+2];50. }

26. b[0]=a[0]+u*b[1]+v*b[2];51. f=c[2]*c[2]-c[1]*c[3];52. if(f==0){du=dv=1;}53. else{du=(b[0]*c[3]-b[1]*c[2])/f;dv=(c[1]*b[1]-c[2]*b[0])/f;}

54. u+=du;55. v+=dv;56. ep=sqrt(du*du+dv*dv);57. }

58. d=u*u+4*v;59. if(d<0) //complex roots60. {61. r1=u/2;r2=sqrt(-d)/2;62. cout<<r1<<" + i"<<r2<<endl;63. cout<<r1<<" - i"<<r2<<endl;64. fprintf(logfile,"%lf + i%lf\n%lf - i%lf\n",r1,r2,r1,r2);65. }66. else // real roots67. {68. r1=u/2+sqrt(d)/2;69. r2=u/2-sqrt(d)/2;70. cout<<r1<<endl;71. cout<<r2<<endl;72. fprintf(logfile,"%lf\n%lf\n",r1,r2);73. }74. n-=2;75. for(i=0;i<n+1;i++)a[i]=b[i+2];76. }

77. if(n==3){78. u=0;79. b[n]=c[n]=a[n];ep=1;80. while(ep>t){81. for(i=n-1;i>0;i--)82. {83. b[i]=a[i]+u*b[i+1];84. c[i]=b[i]+u*c[i+1];85. }86. b[0]=a[0]+u*b[1];87. if(c[1]==0)du=1;88. else du=-b[0]/c[1];

89. u+=du;90. ep=sqrt(du*du);91. }92. cout<<u<<endl;93. fprintf(logfile,"%lf\n",u);94. n--;95. for(i=0;i<n+1;i++)a[i]=b[i+1];

96. }97. if(n==2){98. u=-a[1]/a[2];99. v=-a[0]/a[2];100. d=u*u+4*v;

101. if(d<0) //complex roots102. {103. r1=u/2;r2=sqrt(-d)/2;104. cout<<r1<<" + i"<<r2<<endl;105. cout<<r1<<" - i"<<r2<<endl;106. fprintf(logfile,"%lf + i%lf\n%lf - i%lf\n",r1,r2,r1,r2);107. }108. else109. {110. r1=u/2+sqrt(d)/2;111. r2=u/2-sqrt(d)/2;112. cout<<r1<<endl;113. cout<<r2<<endl;114. fprintf(logfile,"%lf\n%lf\n",r1,r2);115. }

116. }117. else if(n==1){118. r1=-a[0]/a[1];119. cout<<r1<<endl;120. fprintf(logfile,"%lf\n",r1);

121. }

122. delete[] a;delete[] b; delete[] c;123. fclose(logfile);124. cout<<"\n\nFinished finding roots. Press any key to exit.";getch();125. return EXIT_SUCCESS;126. }

• Roots of the polynomial

• -130.000000 +120.000000x -2.000000x^2 -9.000000x^3 +1.000000x^4

• Roots are:

• 3.972068

• -3.600135

• 7.399477

• 1.228589