Find the root of a equation using Newton Raphson
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define EPS 0.000001
#define MAXIT 20
#define F(x) (x) * (x) * (x) - 2 * (x) - 5
#define FD(x) 3 * (x) * (x) - 2
void main()
{
int count;
float x0, xn, fx, fdx;
clrscr();
printf("Input initial value of x : ");
scanf("%f", &x0);
count = 1;
begin:
fx = F(x0);
fdx = FD(x0);
xn = x0 - fx / fdx;
if(fabs((xn - x0) / xn) < EPS)
{
printf("\nRoot = %f", xn);
printf("\n\nNumber of iterations = %d", count);
}
else
{
x0 = xn;
count = count + 1;
if(count < MAXIT)
{
goto begin;
}
else
{
printf("\nSolution does not coverage in %d iterations", MAXIT);
}
}
getch();
}
Output:
Input initial value of x : 0
Root = 2.094552
Number of iterations = 19
-
UpdatedDec 31, 2019
-
Views5,913
You May Like
Program to maintain employee details using structures
Program to maintain student details using structures
Check whether the person is eligible to vote or not
Print the numbers that are divisible by a given no.
Program to generate magic square
To sort the given numbers in ascending & descending order