*/
If you have a PH account, you can customize your PH profile.
*/

View \c03c8B.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(sin(x) - cos(x));
}
char  feq [] = "sin(x) - cos(x)";
/* -------------------------------------------------------------------------- */
double Df(
double x)
{
          return(cos(x) + sin(x));
}
char  Dfeq [] = "cos(x) + sin(x)";
/* -------------------------------------------------------------------------- */
/* ------------------------------------- FUNCTION --------------------------  */
/* Do    :                                                                    */
/*                                                                            */
/* -------------------------------------------------------------------------- */
double g(
double x)
{
        return(sin(x));
}
char  geq [] = "sin(x)";
/* -------------------------------------------------------------------------- */
double Dg(
double x)
{
          return(cos(x));
}
char  Dgeq [] = "cos(x)";
/* -------------------------------------------------------------------------- */
/* ------------------------------------- FUNCTION --------------------------  */
/* Do    :                                                                    */
/*                                                                            */
/* -------------------------------------------------------------------------- */
double h(
double x)
{
        return(cos(x));
}
char  heq [] = "cos(x)";
/* -------------------------------------------------------------------------- */
double Dh(
double x)
{
          return(- sin(x));
}
char  Dheq [] = "- sin(x)";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

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

   int                  n = 5;
double                x_0;
double FirstApproximation = -2.5;
/*-------------------------------- PROGRAM ---------------------------------- */
 P_f      =  f;
 PDf = Df;

 clrscrn();
 printf(" Use Newton's method to approximate the intersection point of :\n\n");
 printf(" g : x-> %s\n\n", geq);
 printf(" and\n\n");
 printf(" h : x-> %s\n\n", heq);

                       /* plot [xmin:xmax] [ymin:ymax] */
      gplt_gh(-4,      /* xmin  */
               4,      /* xmax  */
              -4,      /* ymin  */
               4,      /* ymax  */
               geq,
               heq
              );

 printf(" To see the graphs of g and h, open the file \"f_Df.plt\" with Gnuplot.\n\n");
 printf(" You can see that, the intersection point is between -3.0 and -2.0.\n\n");
 printf(" Choose x = %.1f as a first approximation.\n\n",FirstApproximation);
 getchar();


 clrscrn();
 printf(" In fact we want find sin(x) = cos(x) or sin(x) - cos(x) = 0.\n\n");
 printf(" We want find a root of\n\n");
 printf(" f : x-> %s\n\n", feq);
 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));

   P_f      =  g;
                       /* plot [xmin:xmax] [ymin:ymax] */
     gplt_gxh(-4,      /* xmin  */
               4,      /* xmax  */
              -4,      /* ymin  */
               4,      /* ymax  */
               geq,
               heq,
         (*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.