*/
Got something to write about? Check out our Article Builder.
*/

View \c03c1A.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  - 7.0);
}
char  feq[] = "x**2 - 7.0";
/* -------------------------------------------------------------------------- */
double Df(
double x)
{
       return( 2.0*x);
}
char Dfeq[] = "2.0*x";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

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

   int                  n = 6;
double                  a = 7.0;
double FirstApproximation = 2.5;
/*-------------------------------- PROGRAM ---------------------------------- */
 P_f      =  f;
 PDf = Df;

 clrscrn();
 printf(" Use Newton's method to approximate sqrt(%.1f).       \n\n",a);
 printf(" This is equivalent to find the positive real root of :\n\n");
 printf(" f : x-> %s\n\n", feq);
                       /* plot [xmin:xmax] [ymin:ymax] */
       gplt_f(-4,      /* xmin  */
               4,      /* xmax  */
              -8,      /* ymin  */
               2,      /* 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 positive real root\n");
 printf(" of f is between 2.0 and 3.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();
 printf(" The Newton's method give :                \n\n");
 printf("         x = %.15f\n\n",
          Newton_s_Method(FirstApproximation,
                                           n,
                                   (*P_f),
                                   (*PDf)));
 printf(" The function sqrt(%.1f) :                \n\n",a);
 printf("         x = %.15f\n\n",sqrt(a));

 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.