Muller’s method & FORTRAN features

Download Muller’s method & FORTRAN  features

Post on 15-Jan-2016




0 download

Embed Size (px)


Mullers method & FORTRAN features. Mullers method for solving non-linear equations & FORTRAN features. April 6, 2004. Mullers method & FORTRAN features. Polynomials. Recall: A polynomial of degree n will have n roots. The roots can be real or complex, and they - PowerPoint PPT Presentation


<ul><li><p>Mullers method for solving non-linear equations &amp; FORTRAN featuresApril 6, 2004</p></li><li><p>Polynomials</p><p>Recall: A polynomial of degree n will have n roots. The roots can be real or complex, and they might not be distinct. If the coefficients of the polynomial are real, then complex roots will occur inpairs that are conjugate, i.e. if x1 = a + bi is a root then x1 = a - bi will also be a root.When the coefficients are complex, the complex root need not be related.</p><p>Deflation of Polynomials</p><p>As each root r is found, the polynomial can be factored into a product involving the root and a reducedpolynomial of degree one less than the original:Since the roots of Q(x) are exactly the remaining roots of P(x), the effort of finding the additional rootsdecreases.</p></li><li><p>The SECANT METHODMullers method bears a similarity to the secant methodMany problems in engineering and science require the solution of a nonlinear equation or function.</p><p>Given the continuous nonlinear function f(x), find the value x = alpha such that f(alpha) = 0Figure, illustrating the secant methodThe nonlinear function f(x) is approximated locally bythe linear function g(x), which is the secant to f(x), andthe solution for g(x) = 0 is found. The solution of g(x) = 0is then taken as the next approximation to the solution f(x0 = 0.</p><p>Two initial approximations, x0 and x1 are required to initiate thesecant method. The slope of the secant passing through these twopoints isThe equation of the secant line is given bySolving for X i+1 we haveThe following convergence criteria are satisfied:</p></li><li><p>Mullers methodIn Mullers method, three approximations to the zero are required. The next approximationis the zero of the parabola that passes through the three points. Both real zeros and complex zeros can be found.Mullers method generalizes the secant method, but uses quadratic interpolation among three points instead of linear interpolation between two. Solving for the zeros of the quadratic allows the method to find complex pairs of roots. Given three previous guesses for the root Figure, illustrating the Mullers methodAnd the values of polynomial P(x) at those points, the next approximation is produces by the following formulasMullers method is also used for finding complex zeros of analytic functions(not just polynomials) in the complex plane.W.H.Press et alNumerical recipes in CThe art of Scientific ComputingJoe D. Hoffman Numerical Methods for Engineers and scientists McGraw-Hill, Inc. 1993</p></li><li><p>Mullers methodThis method may be used to find any prescribed number of zeros, real or complex, of an arbitrary function.The method is iterative, converges almost quadratically in the vicinity of the root and obtains both realand complex rootsIn Mullers method, the next approximation Xi+1 is fond as a zero of the parabola which goes through the three pointsThe functionis the unique parabola which agrees with the function f(x) at the three points. We used the notation:Using the following relationWe can also write p(x) in the form:</p></li><li><p>Mullers methodAny zero of the parabola p(x) satisfies (according to one version of the standart quadratic formula)</p><p>The next approximation to zero of f(x) is taken to beSamuel D. Conte/ Carl de BoorElementary Numerical AnalysisAn Algorithmic ApproachAlgorithm for Mullers method1. Let x0, x1, x2 be three approximations to a zero of f(x). Compute f(x0), f(x1), f(x2)2.Compute3. Set i=2 4. Compute5. Compute6. Set7. Compute 8. Set i=i+1 and repeat steps 4-7 until the convergence criteria are satisfied.</p></li><li><p>Beam-plasma instability: Linear AnalysesBeam injected into a stationary plasma. The beam density is much smaller than that of plasmaThusWeak beam modelFor the cold plasma and the cold beam the dispersion relation is:</p><p>The following equation is in Plasma physics Via computer simulation" p110 by Birdsall&amp;Langdon.Real K and complex W</p></li><li><p>include#include#define EPS 1e-10#define MAXIT 100#include "complex.h</p><p>int cmuller(double ar[], double ai[], int n ,double eps,int maxit) ;int main(){//FILE *outreal, *outimagi ;// natalie</p><p>static double ar[5] = {1.0,1.0, 1.0, 1.0,1.0} ; /* real coefficient */ static double ai[5] = {0.0,0.0, 0.0, 0.0,0.0} ; /* complex coefficient */ double cr[5],ci[5],er[5],ei[5], xr,xi,yr,yi,wr,wi ; double eps, wave, R, mu, eta;i int i,j,n,k,kk,maxit ; FILE *outreal, *outimagi ; outreal = fopen("real.dat","w"); if (outreal == NULL) return(1);</p><p> outimagi = fopen("imagi.dat","w"); if (outimagi == NULL) return(1);</p><p>Muller_4_corrected.c</p></li><li>Muller_4_corrected.c/*------------------------------------------------------------------*/ R=1e-3; /* Variable 'wave' means the normalized wave number K in the dispersion relation, D(W,K)=0. */ for (kk=1;kk</li><li><p>Muller_4_corrected.c/*------------------------------------------------------------------*/cmuller(ar , ai , n, eps , maxit);//for(k = 2 ; k &lt; 3 ; k++){// natalie</p><p>for(k = 0 ; k &lt; n ; k++){ if (ar[k]&gt;0.0001) { eta=2*(1-ar[k]/wave); mu=sqrt(wave)/ar[k]*sqrt(sqrt(2*R/eta/eta/eta)); printf("%g\t%8.6f\n",wave,ar[k]); fprintf(outreal, "%g\t%8.6f\n",wave,ar[k]); printf("%8.6f\t%8.6f\t%8.6f\n",ai[k],eta,mu);// fprintf(outimagi,"%8.6f\t%8.6f\t%8.6f\n",ai[k],eta,mu);// natalie fprintf(outimagi, "%g\t%8.6f\n",wave,ai[k]); }} } }} /* end of main */</p></li><li><p>Muller.for EXTERNAL FN LOGICAL FNREAL INTEGER MAXIT,NPREV,I REAL EP1,EP2,XX,XXmax COMPLEX ZEROS(4)C-------------------^C THE COMPLEX ZEROS' DIMENSION MUST BE THE SAME AS THE ORDER OF FNC FNREAL = .FALSE.C N = 4C---------^C THE NUMBER N MUST BE THE SAME AS THE ORDER OF POLYNOMIAL FUNCTION "FN</p><p> EP1 = 0.0 EP2 = 0.0 MAXIT = 1000 XXmax = 6.0 C-------------^ XX = 0.0 C----------^C XXmax DENOTES THAT X VARIES IN THE RANGE OF [0.0, XXmax] 15 CALL MULLER( FN, XX, FNREAL, ZEROS, N, NPREV, MAXIT, EP1, EP2 ) WRITE(6,20) XX, (ZEROS(I),I=1,N) 20 FORMAT( F10.4/ 4(2X,2E13.5 /))C--------------------^C THIS ITERATION NUMBER MUST BE SAME AS "N" IF (XX .LT. XXmax) THEN XX = XX + XXmax / 100.0C-------------------------^C THE DENOMINATOR UNDER THE XXmax IS THE DIVISION NUMBERC OF X COORDINATE, IE, XX, I.E., PRECISION. </p><p> GO TO 15 END IF STOP ENDCC************************************************************</p></li><li><p>Muller.forC************************************************************ SUBROUTINE FN ( Z, W, FZ) C----------------^-------^C W:X-axis valueC Z:Y-axis value that we are looking forC THIS VARIABLE "K" CORRESPONDS TO X-COORDINATE IN XY PLANEC Z IS A COMPLEX NUMBER, FZ IS THE FUNCTION OF Z. C THIS SUBROUTINE MUST BE GIVEN BY THE USER FOR ANY ORDER'S C REAL AND/OR COMPLEX FUNCTION HE WANTSC REAL W, C, Wb, F, GAMMA0, GAMMAz, Vz0, Kw, BETAw DOUBLE COMPLEX FZ, Z, C1, C2, C3, C4, KC K = Z C = 300000000.0 Wb =110000000000.0 F = 1.0 GAMMA0 = 6.0 GAMMAz = 5.4 Vz0 = 295000000.0 Kw = 314.159 BETAw = 0.000000000259C THE FOLLOWING EQUATION IS IN "PLASMA PHYSICS VIA COMPUTERC SIMULATION" P110 BY BIRDSALL.C C1 = W**2 -(C**2)*(K**2) -F*(Wb**2)/GAMMA0 print*,'c1=',c1 C2 = W -Vz0*(K+Kw) print*,'c2=',c2 C3 = Wb**2/(GAMMA0*(GAMMAz**2)) print*,'c3=',c3 C4 = F*(Wb**2)/GAMMA0 print*,'first c4=',c4 C4 = C4*(BETAw**2)*(C**2)*(K**2) print*,'c4=',c4 FZ = C1*(C2*C2 -C3) -C4 print*,'fz=',fz RETURN ENDC************************************************************C</p></li><li><p>Muller_4.cR=0.001Real and imaginary roots of the dispersion relationR=0.001</p></li><li><p>Muller_4.cR=0.01 Real and imaginary roots of the dispersion relationR=0.01The beam is stronger</p></li><li><p>Muller_6.cThis particular code has yet to be validated</p></li><li><p></p><p>Computational E&amp;M lectures by prof.Lee</p><p>6. Software &amp; lecture Materials</p><p>Program source for class</p><p>Dissolve folder</p><p>c/ fortran/ </p><p>In the present state FORTRAN code gives:floating-point error: overflow</p></li><li><p>Fortran evolutionIn early 50s the source code was written in numerical notation, so-called octal code Mnemonic codes were introduced (machine or assembly code)</p><p>FORTRAN is one of the earliest high level languageThe language is simple to learn as it is possible to write mathematical formulaealmost as they are usually written in textbooks.FORTRAN is FORmula TRANslation.</p><p>1966 - the first standart of FORTRAN -- Fortran 66</p><p>Still many dialects remained.</p><p>1978 - new standard - FOTRAN 77</p><p>Fortrans superiority had always been in the area of numerical, scientific, engineering and technical applications.</p><p>New standart FORTRAN 90 which includes FORTRAN 77 entirely.</p><p>FORTRAN 95 is a minor revision of FORTRAN 90</p><p>Michael Metcalf and John ReidFORTRAN 90/95 explained(second edition)OxfordUniversity press1999</p><p>T.M.R.Ellis FORTRAN 77programming(with an Introduction to theFortran 90 Standart) Second editionAddison_WesleyPublishing Company1991Books to learn FORTRAN</p></li><li><p>"Fortran 90 for Scientists and Engineers," by Brian Hahn, 1994Introduction to Fortran 90 for Scientists and EngineersThe Fortran 90 standard represents the first significant change in Fortran in over 20 years, and brings it into line with most modern structured programming languages. The particular set of rules for coding the instructions to a computer is called a programming language. There are many such languages, for example Fortran, BASIC, Pascal and C++. Fortran, which stands for FORmula TRANslation, was the first "high level" programming language. The idea of Fortran was proposed in late 1953 by John Backus, in New York, and the first Fortran program was run in April 1957. A computer program is a set of coded instructions for solving a particular problem.The Fortran statement READ* is for getting data into the computer.The Fortran statement PRINT* is for printing (displaying) results.! Our first Fortran 90 program!! Greetings!</p><p>CHARACTER NAME*20</p><p>PRINT*, 'What is your name?'READ*, NAMEPRINT*, 'Hi there, ', NAMEEND PROGRAM MONEY! Calculates balance after interest compounded REAL BALANCE, INTEREST, RATE</p><p> BALANCE = 1000 RATE = 0.09 INTEREST = RATE * BALANCE BALANCE = BALANCE + INTEREST PRINT*, 'New balance:', BALANCE</p><p>END PROGRAM MONEY</p></li><li><p>[PROGRAM program name] [declaration statements] [executable statements]END [PROGRAM [program name]] Program LayoutIf a statement is too long to fit on a line, it will be continued on the next line if the last non-blank character in it is an ampersand (&amp;): A = 174.6 * &amp; (T - 1981.2) ** 3The Fortran 90 standard requires five intrinsic (i.e. built-in) data types, which are divided into two classes. The numeric types are integer, real and complex. The non-numeric types are character and logical.The basic unit of information in a computer is a bit: something which has only two possible states, usually described as on and off. The binary digits 0 and 1 can therefore be used to represent these two states mathematically (hence the term digital computer). The word "bit" in a contraction of "binary digit". Numbers in a computer's memory must therefore be represented in binary code, where each bit in a sequence stands for a successively higher power of 2. The decimal numbers 0 to 15, for example, are coded in binary as follows:A byte is the amount of computer memory required for one character, and is eight bits long. Since each bit in a byte can be in two possible states, this gives 2 8, i.e. 256, different combinations.</p></li><li><p>Two forms of real constantsThe first form is fixed point0.09 37. 37.0 .0 -.6829135 The second form is called exponential form or floating point 2.0E2 (= 200.0)2E2 (= 200.0)4.12E+2 (= 412.0)-7.321E-4 (= -0.0007321) Real constantsFortran 90 is case insensitive, i.e. the names MYNAME and MyName represent the same thing. Type declaration and initialization INTEGER XREAL INTERESTCHARACTER LETTERREAL :: A = 1REAL A, BINTEGER I, JDATA A, B / 1, 2 / I, J / 0, -1/implicit type ruleVariables starting with the letters I to N inclusive are automatically specified with integer typeInterest_rate = 0.12</p><p>To guard against such errors it is strongly recommended that the statement IMPLICIT NONE be used. </p></li><li><p>PROGRAM Vertical! Vertical motion under gravity</p><p>IMPLICIT NONE</p><p>REAL, PARAMETER :: G = 9.8 ! acceleration due to gravityREAL S ! displacement (metres)REAL T ! timeREAL U ! initial speed (metres/sec) </p><p>PRINT*, ' Time Displacement'PRINT*U = 60T = 6S = U * T - G / 2 * T ** 2PRINT*, T, S</p><p>END PROGRAM VerticalOPEN( 1, FILE = 'DATA' )READ(1, *) A, B, CPRINT*, A, B, CEND Reading data from text files X = 123.4567 PRINT 10, X10 FORMAT( F8.2 ) Format specificationsOPEN( 2, FILE = 'prn' )WRITE(2, *) 'This is on the printer'PRINT*, 'This is on the screen'Sending output to the printerExamples</p></li><li><p>DO I = J, KblockEND DODO LoopsPROGRAM Invest! compound growth of an investment</p><p>IMPLICIT NONEREAL Bal ! balanceINTEGER Period ! period of investmentREAL Rate ! interest rateINTEGER Year ! year counter</p><p>PRINT*, 'Initial balance:'READ*, BalPRINT*, 'Period of investment (years):'READ*, PeriodPRINT*, 'Interest rate (per annum, as a decimal fraction):'READ*, RatePRINT*PRINT*, 'Year Balance'PRINT*</p><p>DO Year = 1, Period Bal = Bal + Rate * Bal PRINT*, Year, BalEND DO</p><p>END</p></li><li><p>PROGRAM Final_Mark! Final mark for course based on class record and exams</p><p>IMPLICIT NONEREAL CRM ! Class record markREAL ExmAvg ! average of two exam papersREAL Final ! final markREAL P1 ! mark for first paperREAL P2 ! mark for second paperINTEGER Stu ! student counter</p><p>OPEN( 1, FILE = 'MARKS' )PRINT*, ' CRM Exam Avg Final Mark'PRINT*</p><p>DO Stu = 1, 3 READ( 1, * ) CRM, P1, P2 ExmAvg = (P1 + P2) / 2.0 IF (ExmAvg &gt; CRM) THEN Final = ExmAvg ELSE Final = (P1 + P2 + CRM) / 3.0 END IF IF (Final &gt;= 50) THEN PRINT*, CRM, ExmAvg, Final, 'PASS' ELSE PRINT*, CRM, ExmAvg, Final, 'FAIL' END IFEND DOEND IF-THEN-ELSE statementconditional branchIF condition THEN block1[ELSE blockE]END IFIF (condition) statementThe IF statement</p></li><li><p>COMPLEX, PARAMETER :: i = (0, 1) ! sqrt(-1)COMPLEX X, YX = (1, 1)Y = (1, -1)PRINT*, CONJG(X), i * X * Y</p><p> Output: ( 1.0000000, -1.0000000) ( 0.0000000E+00, 2.0000000)Complex Type</p></li><li><p>PROGRAM ProjectileIMP...</p></li></ul>