restart: # MULLER'S ALGORITHM 2.8 # # To find a solution to f(x) = 0 given three approximations x0, x1 # and x2: # # INPUT: x0,x1,x2: tolerance TOL: maximum number of iterations NO. # # OUTPUT: approximate solution p or message of failure. # # This implementation allows for a switch to complex arithmetic. # The coefficients are stored in the vector A, so the dimension # of A may have to be changed. print(`This is Mullers Method.`): print(`Input the Polynomial P(x) in terms of x `): print(`For example: x^2-2*x+2 `): P := scanf(`%a`)[1]:print(`P(x) = `):print(P): P := unapply(P,x): OK := TRUE: if degree(P(x)) = 1 then pp := -tcoeff(P(x))/coeff(P(x),x): print(`Polynomial is linear: root is `):print(pp): OK := FALSE: fi: if degree(P(x)) < 1 then print(`Polynomial is a constant function. `): OK := FALSE: fi: if OK = TRUE then OK := FALSE: while OK = FALSE do print(`Input tolerance `): TOL := scanf(`%a`)[1]:print(`Tolerance = `):print(TOL): if TOL <= 0 then print(`Tolerance must be positive `): else OK := TRUE: fi: od: OK := FALSE: while OK = FALSE do print(`Input maximum number of iterations - no decimal point `): M := scanf(`%d`)[1]:print(`Maximum number of iterations = `):print(M): if M <= 0 then print(`Must be positive integer `): else OK := TRUE: fi: od: OK := FALSE: while OK = FALSE do print(`Input the first of three starting values `): X[0] := scanf(`%f`)[1]:print(`First starting value = `):print(X[0]): print(`Input the second of three starting values `): X[1] := scanf(`%f`)[1]:print(`Second starting value = `):print(X[1]): print(`Input the third starting value `): X[2] := scanf(`%f`)[1]:print(`Third starting value = `):print(X[2]): if ((abs(X[0]-X[1]) < 1.0e-20) or (abs(X[0]-X[2]) < 1.0e-20) or (abs(X[2]-X[1]) < 1.0e-20)) then print(`The starting values must be distinct. `): else OK := TRUE: fi: od: fi: F[0] := P(X[0]): F[1] := P(X[1]): F[2] := P(X[2]): J := -1: if F[0] = 0 then J := 0: fi: if F[1] = 0 then J := 1: fi: if F[2] = 0 then J := 2: fi: if J > -1 then print(`Solution is the initial approximation `):print(X[J]): OK := FALSE: fi: if OK = TRUE then print(`Select output destination `): print(`1. Screen `): print(`2. Text file `): print(`Enter 1 or 2 `): FLAG := scanf(`%d`)[1]:print(`Input = `):print(FLAG): if FLAG = 2 then print(`Input the file name in the form - drive:\134\134name.ext `): print(`For example: A:\134\134OUTPUT.DTA `): NAME := scanf(`%s`)[1]:print(`Input = `):print(NAME): OUP := fopen(NAME,WRITE,TEXT): else OUP := default: fi: fprintf(OUP, `MULLER'S METHOD \134n`): fprintf(OUP, `The output is i, approximation x(i), f(x(i)) \134n `): fprintf(OUP,`Three columns means the results are real numbers,\134n `): fprintf(OUP,`Five columns means the results are complex\134n`): fprintf(OUP,`numbers with real and imaginary parts of x(i)\134n`): fprintf(OUP,`followed by real and imaginary parts of f(x(i)).\134n\134n`): I1:=0:fprintf(OUP, `%d %a %a\134n`, I1, X[I1], F[I1]): I1:=1:fprintf(OUP, `%d %a %a\134n`, I1, X[I1], F[I1]): I1:=2;fprintf(OUP, `%d %a %a\134n`, I1, X[I1], F[I1]): # Step 1 H[0] := X[1]-X[0]: H[1] := X[2]-X[1]: DEL1[0] := (F[1]-F[0])/H[0]: DEL1[1] := (F[2]-F[1])/H[1]: DEL := (DEL1[1]-DEL1[0])/(H[1]+H[0]): I1 := 3: # Step 2 while I1 <= M and OK = TRUE do # Step 3 B := DEL1[1]+H[1]*DEL: D1 := B*B-4*F[2]*DEL: # Test to see if straight line if abs(DEL) <= 1.0e-20 then # straight line - test if horizontal line if abs(DEL1[1]) <= 1.0e-20 then print(`Horizontal Line `): OK := FALSE: else # Straight line, but not horizontal X[3] := (F[2]-DEL1[1]*X[2])/DEL1[1]: H[2] := X[3]-X[2]: fi: else # Not a straight line D1 := sqrt(D1): # Step 4 E := B+D1: if abs(B-D1) > abs(E) then E := B-D1: fi: # Step 5 H[2] := -2*F[2]/E: X[3] := X[2]+H[2]: fi: if OK = TRUE then F[3] := P(X[3]): fprintf(OUP, `%d %a %a\134n`, I1, X[3], F[3]): fi: # Step 6 if ( abs(H[2]) < TOL or F[3] = 0 ) then fprintf(OUP, `\134nMethod is successfull. The approximation\134n`): fprintf(OUP, `%a is within %.10e\134n`,X[3], TOL): fprintf(OUP, `in %d iterations.\134n`, I1): OK := FALSE: else # Step 7 for J from 1 to 2 do H[J-1] := H[J]: X[J-1] := X[J]: F[J-1] := F[J]: od: X[2] := X[3]: F[2] := F[3]: DEL1[0] := DEL1[1]: DEL1[1] := (F[2]-F[1])/H[1]: DEL := (DEL1[1]-DEL1[0])/(H[1]+H[0]): if abs(DEL) <= 1.0e-20 then if abs(DEL1[1]) <= 1.0e-20 then print(`Horizontal Line `): OK := FALSE: fi: fi: fi: I1 := I1+1: od: # Step 8 if I1 > M and OK = TRUE then fprintf (OUP, `Final approximation not within tolerance %.10e\134n`,TOL): fi: if OUP <> default then fclose(OUP): print(`Output file `, NAME,` created successfully`): fi: fi: SThUaGlzfmlzfk11bGxlcnN+TWV0aG9kLkc2Ig== SUlJbnB1dH50aGV+UG9seW5vbWlhbH5QKHgpfmlufnRlcm1zfm9mfnh+RzYi SThGb3J+ZXhhbXBsZTp+eF4yLTIqeCsyfkc2Ig== SShQKHgpfj1+RzYi LCwqJEkieEc2IiIiJSIjOyokRiQiIiQhI1MqJEYkIiIjIiImRiQiIz8iIiciIiI= STFJbnB1dH50b2xlcmFuY2V+RzYi SS1Ub2xlcmFuY2V+PX5HNiI= JCIiIiEiJg== SVdJbnB1dH5tYXhpbXVtfm51bWJlcn5vZn5pdGVyYXRpb25zfi1+bm9+ZGVjaW1hbH5wb2ludH5HNiI= SUBNYXhpbXVtfm51bWJlcn5vZn5pdGVyYXRpb25zfj1+RzYi IiM6 SUpJbnB1dH50aGV+Zmlyc3R+b2Z+dGhyZWV+c3RhcnRpbmd+dmFsdWVzfkc2Ig== SThGaXJzdH5zdGFydGluZ352YWx1ZX49fkc2Ig== JCIiJiEiIg== SUtJbnB1dH50aGV+c2Vjb25kfm9mfnRocmVlfnN0YXJ0aW5nfnZhbHVlc35HNiI= STlTZWNvbmR+c3RhcnRpbmd+dmFsdWV+PX5HNiI= JCEiJiEiIg== SUBJbnB1dH50aGV+dGhpcmR+c3RhcnRpbmd+dmFsdWV+RzYi SThUaGlyZH5zdGFydGluZ352YWx1ZX49fkc2Ig== JCIiIUYj STtTZWxlY3R+b3V0cHV0fmRlc3RpbmF0aW9ufkc2Ig== SSsxLn5TY3JlZW5+RzYi SS4yLn5UZXh0fmZpbGV+RzYi SS5FbnRlcn4xfm9yfjJ+RzYi SSlJbnB1dH49fkc2Ig== IiIi MULLER'S METHOD The output is i, approximation x(i), f(x(i)) Three columns means the results are real numbers, Five columns means the results are complex numbers with real and imaginary parts of x(i) followed by real and imaginary parts of f(x(i)). 0 .5 13.2500 1 -.5 3.2500 2 0. 6. 3 -.5555555558+.5983516452*I -29.40070112-3.89872475*I 4 -.4354502836+.1021012488*I 1.332224758-1.193096726*I 5 -.3906314607+.1418522322*I .375057928-.670168356*I 6 -.3576984290+.1699262690*I -.146750080-.7446238e-2*I 7 -.3560506666+.1628560137*I -.1840221e-2+.538456e-3*I 8 -.3560617022+.1627583074*I .1648e-5+.893e-6*I 9 -.3560617617+.1627583828*I .2e-8+.1e-8*I Method is successfull. The approximation -.3560617617+.1627583828*I is within 1.0000000000e-05 in 9 iterations. JSFH