/* 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 |
---|---|---|---|
simple way to manipulate chinese characters in vb6 application. | 13 | 275 | |
Issue accessing member variable of atomic class | 16 | 110 | |
Using Diagnostic tools in VS2015: Unresoved allocations | 19 | 63 | |
operator overload: incompatible type and too many params | 5 | 62 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
21 Experts available now in Live!