⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 alg025.txt

📁 Numerical Anaysis 8th Edition Burden and Faires (Maple Source)
💻 TXT
字号:
> restart;
> # METHOD OF FALSE POSITION ALGORITHM 2.5
> #
> # To find a solution to f(x) = 0 given the continuous function
> # f on the interval [p0,p1], where f(p0) and f(p1) have
> # opposite signs:
> #
> # INPUT:   endpoints p0, p1; tolerance TOL;
> #          maximum number of iterations N0.
> #
> # OUTPUT:  approximate solution p or
> #          a message that the algorithm fails.
> alg025 := proc() local F, OK, P0, P1, X, Q0, Q1, TOL, NO, FLAG, NAME, OUP, I, P, Q;
> printf(`This is the Method of False Position\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 P0 < P1 separated by a blank space\n`);
> P0 := scanf(`%f`)[1]; 
> P1 := scanf(`%f`)[1];
> if P0 > P1 then
> X := P0;
> P0 := P1;
> P1 := X;
> fi;
> if P0 = P1 then
> printf(`P0 cannot equal P1\n`);
> else
> Q0 := F(P0);
> Q1 := F(P1);
> if Q0*Q1 > 0 then
> printf(`F(P0) and F(P1) have the 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, `METHOD OF FALSE POSITION OR REGULA FALSII\n\n`);
> if FLAG = 2 then
> fprintf(OUP, `  I    P                 F(P)\n`);
> fi;
> # Step 1
> I := 2;
> OK := TRUE;
> Q0 := F(P0);
> Q1 := F(P1);
> # Step 2
> while I <= NO and OK = TRUE do
> # Step 3
> # Compute P(I)
> P := P1-Q1*(P1-P0)/(Q1-Q0);
> Q := F(P);
> if FLAG = 2 then
> fprintf(OUP,`%3d%15.8e%15.8e\n`,I,P,Q);
> fi;
> # Step 4
> if abs(P-P1) < TOL then
> # Procedure completed successfully
> fprintf(OUP,`\nApproximate solution P = %12.8f\n`,P);
> fprintf(OUP,`with F(P) = %12.8f\n`,Q);
> 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 P0(I) and P1(I) 
> if Q*Q1 < 0 then
> P0 := P1;
> Q0 := Q1;
> fi;
> # Step 7
> # Procedure completed unsuccessfully
> P1 := P;
> Q1 := Q;
> fi;
> od;
> if OK = TRUE then
> 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`,Q,TOL);
> fi;
> if OUP <> default then
> fclose(OUP):
> printf(`Output file %s created successfully`,NAME);
> fi;
> fi;
> RETURN(0);
> end;
> alg025();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -