Basic Theory:
Program:
The NewtonRaphson method is a powerful technique for solving equations numerically. It is based on the simple idea of linear approximation. The Newton Method, when properly used, usually comes out with a root with great efficiency. It converges, if it does, faster than other techniques. The single initial value used in this approximation is called x0. The Newton Method is usually very good if x0 is close to r, and can be horrid if it is not.
It uses the following formula for estimating next values and iterates the process until required precision is achieved.
x_{n+1 = }x_{n  f(}x_{n) / f '(}x_{n)}
It can be further explained by the following example:
We have following values,
f(x) = x^2  4
f '(x) = 2x
x0 = 6
We will assume that the process has worked accurately enough when our deltax becomes less than 0.1. This value of precision is specific to each situation. A much more, or a much less, precise value may be assumed when using the NewtonRaphson method in other contexts. The table below shows the execution of the process.
n

x_{n}

f(x_{n})

f '(x_{n})

x_{n+1}

dx

0

x_{0} = 6

f(x_{0}) = 32

f '(x_{0}) = 12

x_{1} = 3.33
 
1

x_{1} = 3.33

f(x_{1}) = 7.09

f '(x_{1}) = 6.66

x_{2} = 2.27

dx = 1.06

2

x_{2} = 2.27

f(x_{2}) = 1.15

f '(x_{2}) = 4.54

x_{3} = 2.01

dx = .26

3

x_{3} = 2.01

f(x_{3}) = 0.04

f '(x_{3}) = 4.02

x_{4} = 2.00

dx = 0.01

Thus, using an initial xvalue of six (6) we find one root of the equation f(x) = x^{2}4 is x=2. If we were to pick a different inital xvalue, we may find the same root, or we may find the other one, x=2.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x)
x*x4*x10
#define fd(x)
2*x4
#define E 0.0005
void main(){
clrscr();
float x0, x1,
f0, fd0, e;
printf("\t\t\tNEWTONRAPHSON
METHOD");
printf("\n\n
FINDING ROOT OF X^24X10");
printf("\n
Enter the initial value: ");
scanf("%f",
&x0);
printf("\n
f(x0)\t\tfd(x0)\t\tx1\t\tError");
printf("\n
");
begin:
f0 = f(x0);
fd0 = fd(x0);
x1 = x0 
(f0/fd0);
e
=fabs((x1x0)/x1);
if( e < E){
printf("\n\n
Approximate Root = %.5f", x1);
}
else{
printf("\n
%.2f\t\t%.3f\t\t%.3f\t\t%.4f",f(x0),fd(x0),x1,e);
x0 = x1;
goto begin;
}
getch();
}
Sample Output:
NEWTONRAPHSON METHOD
FINDING ROOT OF X^24X10
Enter the initial value: 0
f(x0) fd(x0) x1 Error

10.00 4.000 2.500 1.0000
6.25 9.000 1.806 0.3846
0.48 7.611 1.742 0.0364
0.00 7.484 1.742 0.0003
Approximate Root = 1.74166
Sample Output:
NEWTONRAPHSON METHOD
FINDING ROOT OF X^24X10
Enter the initial value: 0
f(x0) fd(x0) x1 Error

10.00 4.000 2.500 1.0000
6.25 9.000 1.806 0.3846
0.48 7.611 1.742 0.0364
0.00 7.484 1.742 0.0003
Approximate Root = 1.74166
The code is wonderful.
ReplyDeleteFor the first time ever i came across a code online which executes without errors .. helped to completed my home assignment. Thank You
it is absolutely fine code
ReplyDeletegood code
ReplyDeleteGreat that the code worked for you. However, I suggest everyone to spend some time to understand the theory, example, and the program too. You can base your solution around my code, but do not just copypaste the code to solve your assignments :)
ReplyDeleteCan someone help me out with a general code of Newton Raphson method that can solve a system of non linear equations with the Jacobian matrix inclusive in a general form. I have 8 model equations with 8 unknowns.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete