Newton raphson method in c
👉 NEWTON RAPHSON METHOD:
Let, x0 be an approximation of a root of the given equation f(x)=0, which may be algebraic or transcendental.
Let x0+h be the exact value or the better approximation of the corresponding root,h being a small quantity.
Then,f(x0+h)=0.
Expanding it by Taylor`s theorem,we get,
f(x0+h)=f(x0)+hf `(x0)+h*h f ``(x0)+..........=0
h=-f(x0) / f `(x0)
x1=x0+h
x2=x1+h
condition for convergence of newton raphson method:
|1-(f `(x)*f `(x))-[ f(x)*f ``(x)] /f `(x)*f `(x)| <1
|f(x)*f ``(x)|<|f `(x)*f `(x)|
coding:
- #include<stdio.h>
- #include<math.h>
- float fun(float x)
- {
- return x*x-2*x+3;
- }
- float diff1fun(float x)
- {
- return 2*x-2;
- }
- void main()
- {
- int itr, maxmitr;
- float h, x0, x1, error;
- printf("\nEnter x0");
- scanf("%f", &x0);
- printf("\nEnter maximum ittreation\n");
- scanf("%d",&maxmitr);
- printf("\nEnter error");
- scanf("%f",&error);
- for (itr=1; itr<=maxmitr; itr++)
- {
- h=fun(x0)/diff1fun(x0);
- x1=x0-h;
- printf(" At Iteration no. %3d, x = %9.6f\n", itr, x1);
- if (fabs(h) < error)
- {
- printf("After %3d iterations, root = %8.6f\n", itr, x1);
- return 0;
- }
- x0=x1;
- }
- printf("iterations are insufficient\n");
- return 1;
- }
best
ReplyDeleteSuperb👌
ReplyDelete