home *** CD-ROM | disk | FTP | other *** search
- naregu(xlow,xhi,eps,root,n)
-
- /*this subroutine finds the root of the equation f(x) = 0 */
- /*through the method of false position(regula falsi). */
-
- int *n;
- float eps,*root,xhi,xlow;
-
- {
- int nmax;
- float fx,fxlow,fxhi,fxnew,fxp,fxq,x,xnew,xp,xq;
- extern double fabs();
-
- nmax = *n;
- *n = 0;
- value(xhi,&fxhi);
- value(xlow,&fxlow);
- if(fxhi < 0.0)
- {
- xp = xlow;
- fxp = fxlow;
- xq = xhi;
- fxq = fxhi;
- }
- else
- {
- xp = xhi;
- fxp = fxhi;
- xq = xlow;
- fxq = fxlow;
- }
- reit: xnew = xp - fxp*(xp - xq)/(fxp - fxq);
- value(xnew,&fxnew);
- *n = *n + 1;
- if(fabs(fxnew) <= eps)
- {
- *root = xnew;
- *n = 1;
- return;
- }
- if(*n >= nmax)
- {
- *root = xnew;
- *n = 2;
- return;
- }
- if(fxnew > 0.0)
- {
- x = xnew - fxnew*(xnew - xp)/(fxnew - fxp);
- xp = xnew;
- fxp = fxnew;
- }
- else
- {
- x = xnew - fxnew*(xnew-xq)/(fxnew - fxq);
- xq = xnew;
- fxq = fxnew;
- }
- if(xp <= xq)
- {
- if(xq <= x || x <= xp) goto reit;
- }
- else if(xp <= x || x <= xq) goto reit;
-
- *n = *n + 1;
- value(x,&fx);
- if(fabs(fx) <= eps)
- {
- *root = x;
- *n = 1;
- return;
- }
- if(*n >= nmax)
- {
- *root = x;
- *n = 2;
- return;
- }
- if(fx <= 0.0)
- {
- xq = x;
- fxq = fx;
- }
- else
- {
- xp = x;
- fxp = fx;
- }
- goto reit;
- }