Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chem 302 Lab Assignment 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.

Similar presentations


Presentation on theme: "Chem 302 Lab Assignment 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."— Presentation transcript:

1 Chem 302 Lab Assignment 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

2 Chem 302 - Math 252 Chapter 1 Solutions of nonlinear equations

3 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

4 Successive Approximations Simplest method Want to solve f(x)=0 rearrange into the form x=g(x) Use as iteration formula x i+1 =g(x i ) Use initial guess and iterate until self consistent

5 Successive Approximations

6 ixixi 00 10.666667 20.814815 30.887974 40.929500 50.954656 60.970456 70.980595 80.987189 90.991514 100.994367 110.996255 120.997508 130.998341 140.998895 150.999264 160.999509 ixixi 170.999673 180.999782 190.999855 200.999903 210.999935 220.999957 230.999971 240.999981 250.999987 260.999991 270.999994 280.999996 290.999997 300.999998 310.999999 320.999999

7 Successive Approximations Second root

8 Successive Approximations Second root

9 Successive Approximations Different formula Will find x=2, will not find x=1 Different formula – different results No one formula is best Slow to converge

10 Successive Approximations How to find initial guesses? Grid search Course to start Get finer

11 Analysis of Convergence Each stage of iteration x i+1 =g(x i ) For convergence True root Intersection of two functions: x & g(x)

12 Analysis of Convergence

13 Four possibilities –Monotonic convergence –Oscillating convergence –Monotonic divergence –Oscillating divergence Analysis of Convergence

14 Monotonic convergence

15 Oscillating convergence

16 Monotonic divergence

17 Oscillating divergence

18 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

19 Analysis of Convergence

20 To converge LHS 0 If M < 1 guaranteed to converge Sufficient but not necessary

21 Speed of Convergence Taylor expansion about root For x i When close to convergence Dominant term will be 1 st nonzero derivative Order of Convergence

22 One of most common methods Usually 2 nd order convergence Generally superior to simple iteration Uses function and 1 st derivative to predict root (assume f is linear) Newton-Raphson Method

23

24 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

25 Newton-Raphson Method Test conditions Convergence

26 NR but use numeric derivative Secant Method Need two points to start

27 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

28 Algorithm –Pick x L & x R (x L < < x R ) –Evaluate –Calculate –If then x M is the root –Replace x L or x R with x M (depends on sign of ) False-Position Method Repeat xLxL xRxR xMxM

29 False-Position Method

30 Same as FP Method but x M is average of x L & x R 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 x M gets close to root Secant, FP & Bisection methods do not require analtyic expression of f'(x) Bisection Method

31 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 (Horners Method) Roots of Polynomials

32 For polynomial of degree m Horners Method Divide by (x-r) b 0 is f(r)

33 Horners Method m ×, m +

34 Horners Method 1 is a root

35 Horners Method 2 is a root

36 If b 0 = 0 (i.e. r is a root) Horners Method Have factored (x-r) from equation (i.e. reduced order, called deflating) Continue to find roots of reduced equation

37 Horners Method 2 is a root (i.e. a double root of original equation)

38 Birge-Vieta Method NR method with f(x) and f'(x) evaluated using Horners method Once a root is found, reduce order of polynomial

39 Birge-Vieta Method

40

41

42

43

44 Example

45 NR x 0 4.33 3 x 0 > 4.33 5 Third root? Try it!!!

46 BV

47 Roots of Polynomials What about complex roots? –Occur in pairs –Have form + i & – i –Roots of quadratic equation f(x) = x 2 – 2 x + 2 + 2 BV method – we removed factors of x – r. Quadratic can be solved analytically – therefore best to remove quadratic factors

48 Lin-Bairstow Method

49 Iteration Scheme

50 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

51 1.// Lin Biarstow.cpp 2.#include "stdafx.h" 3.#include 4.#include 5.#include 6.#include 7.#include 8.#include 9.#include 10.#include 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>3 39. 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. }

52 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 roots 60. { 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 roots 67. { 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;

53 101.if(d<0) //complex roots 102. { 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. else 109. { 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.}

54

55 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


Download ppt "Chem 302 Lab Assignment 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."

Similar presentations


Ads by Google