📄 alg021.txt
字号:
> restart;
> # BISECTION ALGORITHM 2.1
> #
> # To find a solution to f(x) = 0 given the continuous function
> # f on the interval [a,b], where f(a) and f(b) have
> # opposite signs:
> #
> # INPUT: endpoints a,b; tolerance TOL;
> # maximum number of iterations NO.
> #
> # OUTPUT: approximate solution p or
> # a message that the algorithm fails.
> alg021 := proc() local F, OK, A, B, X, FA, FB, TOL, NO, FLAG, NAME, OUP, I, C, P, FP;
> printf(`This is the Bisection Method.\n`);
> printf(`Input the function F(x) in terms of x\n`);
> printf(`For example: cos(x)\n`);
> F := scanf(`%a`)[1];
> F := unapply(F,x);
> OK := FALSE;
> while OK = FALSE do
> printf(`Input endpoints A < B separated by blank\n`);
> A := scanf(`%f`)[1];
> B := scanf(`%f`)[1];
> if A > B then
> X := A;
> A := B;
> B := X;
> fi;
> if A = B then
> printf(`A cannot equal B\n`);
> else
> FA := F(A);
> FB := F(B);
> if FA*FB > 0 then
> printf(`F(A) and F(B) have same sign\n`);
> else
> OK := TRUE;
> fi;
> fi;
> od;
> OK := FALSE;
> while OK = FALSE do
> printf(`Input tolerance\n`);
> TOL := scanf(`%f`)[1];
> if TOL <= 0 then
> printf(`Tolerance must be positive\n`);
> else
> OK := TRUE;
> fi;
> od;
> OK := FALSE;
> while OK = FALSE do
> printf(`Input maximum number of iterations - no decimal point\n`);
> NO := scanf(`%d`)[1];
> if NO <= 0 then
> printf(`Must be positive integer\n`);
> else
> OK := TRUE;
> fi;
> od;
> if OK = TRUE then
> printf(`Select output destination\n`);
> printf(`1. Screen\n`);
> printf(`2. Text file\n`);
> printf(`Enter 1 or 2\n`);
> FLAG := scanf(`%d`)[1];
> if FLAG = 2 then
> printf(`Input the file name in the form - drive:\\name.ext\n`);
> printf(`For example: A:\\OUTPUT.DTA\n`);
> NAME := scanf(`%s`)[1];
> OUP := fopen(NAME,WRITE,TEXT);
> else
> OUP := default;
> fi;
> printf(`Select amount of output\n`);
> printf(`1. Answer only\n`);
> printf(`2. All intermediate approximations\n`);
> printf(`Enter 1 or 2\n`);
> FLAG := scanf(`%d`)[1];
> fprintf(OUP,`Bisection Method\n`);
> if FLAG = 2 then
> fprintf(OUP, ` I P F(P)\n`);
> fi;
> # Step 1
> I := 1;
> # Step 2
> OK := TRUE;
> while I <= NO and OK = TRUE do
> # Step 3
> # Compute P(I)
> C := (B - A) / 2.0;
> P := A + C;
> # Step 4
> FP := F(P);
> if FLAG = 2 then
> fprintf(OUP,`%3d %15.8e %15.7e \n`,I,P,FP);
> fi;
> if abs(FP) < 1.0e-20 or C < TOL then
> # Procedure completed successfully
> fprintf(OUP,`\nApproximate solution P = %11.8f \n`,P);
> fprintf(OUP,`with F(P) = %12.8f\n`,FP);
> fprintf(OUP,`Number of iterations = %3d`,I);
> fprintf(OUP,` Tolerance = %15.8e\n`,TOL);
> OK := FALSE;
> else
> # Step 5
> I := I+1;
> # Step 6
> # Compute A(I) and B(I)
> if FA*FP > 0 then
> A := P;
> FA := FP;
> else
> B := P;
> FB := FP;
> fi;
> fi;
> od;
> if OK = TRUE then
> # Step 7
> # Procedure completed unsuccessfully
> fprintf(OUP,`\nIteration number %3d`,NO);
> fprintf(OUP,` gave approximation %12.8f\n`,P);
> fprintf(OUP,`F(P) = %12.8f not within tolerance : %15.8e\n`,FP,TOL);
> fi;
> if OUP <> default then
> fclose(OUP):
> printf(`Output file %s created successfully`,NAME);
> fi;
> fi;
> RETURN(0);
> end;
> alg021();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -