*/
Know a good article or link that we're missing? Submit it!
*/

View \c03c4B.c

Calculus (Derivative:Newton's method). v04

Submitted By: xhunga
Rating: (Not rated) (Rate It)


/*         .c                       freeware                   [[Email Removed]]
*/

/* --------------------------------- INCLUDES ------------------------------- */
#include "xa_hfile.h"

/* ------------------------------------- FUNCTION --------------------------  */
/* Do    :                                                                    */
/*                                                                            */
/* -------------------------------------------------------------------------- */
double f(
double x)
{
        return(x*x*x - 3.0*x + 1.0);
}
char  feq[]"x**3 - 3.0*x + 1.0";
/* -------------------------------------------------------------------------- */
double Df(
double x)
{
       return( 3.0*x*x - 3.0);
}
char Dfeq[] = "3.0*x**2 - 3.0";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

/* --------------------------------- MAIN ----------------------------------- */
int main(void)
{
double (*P_f)(double x);
double (*PDf)(double x);

   int n = 8;

double x_0;
double FirstApproximation = -1.5;
/*-------------------------------- PROGRAM ---------------------------------- */
 P_f      =  f;
 PDf = Df;

 clrscrn();
 printf(" Use Newton's method to approximate .       \n\n");
 printf(" the smallest negative real root of :\n\n");
 printf(" f : x-> %s\n\n", feq);
                       /* plot [xmin:xmax] [ymin:ymax] */
       gplt_f(-4,      /* xmin  */
               4,      /* xmax  */
              -4,      /* ymin  */
               4,      /* ymax  */
               feq
              );
 printf(" To see the graph of f, open the file \"f_Df.plt\" with Gnuplot.\n\n");
 printf(" You can see that, the smallest negative real root\n");
 printf(" of f is between -2.0 and -1.0.\n\n");
 printf(" Choose x = %.1f as a first approximation.\n\n",FirstApproximation);
 getchar();

 clrscrn();
 printf(" As a first approximation x = %.1f \n\n",FirstApproximation);
 printf(" The Newton's method give :                \n\n");
  p_Newton_s_Method(FirstApproximation,
                   n,
           (*P_f),
           (*PDf));
 getchar();

 clrscrn();
 x_0 = Newton_s_Method(FirstApproximation,
                                           n,
                                   (*P_f),
                                   (*PDf));

                       /* plot [xmin:xmax] [ymin:ymax] */
    gplt_root(-4,      /* xmin  */
               4,      /* xmax  */
              -4,      /* ymin  */
               4,      /* ymax  */
             feq,
          (*P_f),
             x_0
              );

 printf(" To see the result, open the file \"f_Df.plt\" with Gnuplot.\n\n");

 printf("\n Press return to continue");
 getchar();

 return 0;
}

corner
© 1996-2008 CommunityHeaven LLC. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
North American business development: Nicolai Wadstrom. Publisher: Lars Hagelin.