File tree 3 files changed +56
-0
lines changed
mathematical/NewtonRaphson
out/production/NewtonRaphson/com/company
3 files changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <module type =" JAVA_MODULE" version =" 4" >
3
+ <component name =" NewModuleRootManager" inherit-compiler-output =" true" >
4
+ <exclude-output />
5
+ <content url =" file://$MODULE_DIR$" >
6
+ <sourceFolder url =" file://$MODULE_DIR$/src" isTestSource =" false" />
7
+ </content >
8
+ <orderEntry type =" inheritedJdk" />
9
+ <orderEntry type =" sourceFolder" forTests =" false" />
10
+ </component >
11
+ </module >
Original file line number Diff line number Diff line change
1
+ package com .company ;
2
+
3
+ import java .util .Scanner ;
4
+
5
+ import static java .lang .System .in ;
6
+
7
+ /**
8
+ *The Newton-Raphson method (also known as Newton's method) is a way to quickly find a good approximation for the
9
+ * root of a real-valued function f(x)=0f(x) = 0f(x)=0.
10
+ * It uses the idea that a continuous and differentiable function can be approximated by a straight line tangent to it.
11
+ */
12
+ public class Main {
13
+ public static double EPSILON = 0.0001 ;
14
+
15
+ double Func (double x ){
16
+ return (x *x *x ) - (3 *x ) - 5 ;
17
+ }
18
+
19
+ double derivFunc (double x ){
20
+ return 3 * (x * x ) - 3 ;
21
+ }
22
+
23
+ public static void main (String [] args ) {
24
+ // write your code here
25
+ Main obj = new Main ();
26
+
27
+ double x_n , x_n1 ;
28
+
29
+ System .out .print ("Enter initial value of x (x0)=" );
30
+ Scanner sc = new Scanner (in );
31
+ String x_0 = sc .nextLine ();
32
+ x_n = Double .parseDouble (x_0 ); // initial double value of X0
33
+ x_n1 = x_n ;
34
+
35
+ int iteration_count =1 ;
36
+ double h = obj .Func (x_n ) / obj .derivFunc (x_n );
37
+ while (Math .abs (h ) >= EPSILON ){
38
+ h = obj .Func (x_n ) / obj .derivFunc (x_n );
39
+ x_n1 = x_n - h ;
40
+ x_n = x_n1 ;
41
+ System .out .printf ("x_%d=%.4f\n " , iteration_count ++ , x_n1 );
42
+ }
43
+ System .out .printf ("\n Root of the equation (using Newton Raphson's method): %.4f" , x_n1 );
44
+ }
45
+ }
You can’t perform that action at this time.
0 commit comments