Algorithm:: Experiment No: 03 Experiment Name: Write A Program Implementation of Aim
Algorithm:: Experiment No: 03 Experiment Name: Write A Program Implementation of Aim
Algorithm:: Experiment No: 03 Experiment Name: Write A Program Implementation of Aim
Step-3:
( ) ()
Find f x0 and f′ x0
x =x -f(x )f′(x )
1 0 0 0
Step-4:
( )
If f x1 =0 then x1 is an exact root,
else x0=x1
Step-5:
()
Repeat steps 3 and 4 until f xi =0 or |f(x )|≤Accuracy
i
Program Cord:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
float x0, x1, f0, f1, g0, e;
int step = 1, N;
clrscr();
/* Inputs */
printf("\nEnter initial guess:\n");
scanf("%f", &x0);
printf("Enter tolerable error:\n");
scanf("%f", &e);
printf("Enter maximum iteration:\n");
scanf("%d", &N);
/* Implementing Newton Raphson Method */
printf("\nStep\t\tx0\t\tf(x0)\t\tx1\t\tf(x1)\n");
do
{
g0 = g(x0);
f0 = f(x0);
if(g0 == 0.0)
{
printf("Mathematical Error.");
exit(0);
}
x1 = x0 - f0/g0;
printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,f0,x1,f1);
x0 = x1;
step = step+1;
if(step > N)
{
printf("Not Convergent.");
exit(0);
}
f1 = f(x1);
}while(fabs(f1)>e);
Output:
Enter initial guess:
1
Enter tolerable error:
0.00001
Enter maximum iteration:
10