/* Newton's method example.
http://en.wikipedia.org/wiki/Newton%27s_method */
#include <stdio.h>
#include <math.h>
/* the function */
#define f(x) exp(2*x)+exp(3*x)-5
/* the derivative on x */
#define f_(x) 2*exp(2*x)+3*exp(3*x)
/* the first quess */
#define x0 -1
/* precision */
#define epsilon 1e-9
int main()
{
double prev,curr,delta;
prev=curr=x0;
while(1)
{
curr = prev - (f(prev)/f_(prev));
delta = fabs(curr-prev);
printf(">> %.10f e=%.10f\n",curr,delta);
if (delta < epsilon)
break;
prev = curr;
}
printf("%.10f\n",curr);
return 0;
}
/* Newton's method example.
http://en.wikipedia.org/wiki/Newton%27s_method */
#include <stdio.h>
#include <math.h>
/* the function */
#define f(x) exp(2*x)+exp(3*x)-5
/* the derivative on x */
#define f_(x) 2*exp(2*x)+3*exp(3*x)
/* the first guess */
#define x0 -1
/* precision */
#define epsilon 1e-9
int main()
{
double prev,curr,delta;
prev=curr=x0;
while(1)
{
curr = prev - (f(prev))/(f_(prev));
delta = fabs(curr-prev);
/*printf(">> %.10f e=%.10f\n",curr,delta);*/
if (delta < epsilon)
break;
prev = curr;
}
printf("root=%.10f, f(root)=%.10f\n",curr,(f(curr)));
return 0;
}
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
Need some help with listview limit text | 20 | 105 | |
C++ BOOL WINAPI ReadFile fails on windows 10 when reading from USB cable | 9 | 433 | |
c++ substatte a varabe for a string in a LPCTSTR statment | 8 | 87 | |
I could not build boost code, | 10 | 88 |
Join the community of 500,000 technology professionals and ask your questions.