/* 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;
}
Title | # Comments | Views | Activity |
---|---|---|---|
returning a dereferenced pts in C++ | 10 | 130 | |
Would like to move button in a function | 3 | 68 | |
Grammars for C C++ and java | 1 | 108 | |
windows8.1. pro visual c++ 2015 setup failed 0x80240017 problem i tried many tut found on net but nothing work | 5 | 34 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
10 Experts available now in Live!