-
Notifications
You must be signed in to change notification settings - Fork 0
/
MullersMethod.java
64 lines (51 loc) · 1.86 KB
/
MullersMethod.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package non_linear_equation;
import java.util.Scanner;
public class MullersMethod {
static double a,b,c,d,x,x3,x1,x2,h0,h1,h2,d0,d1,d2,a0,a1,a2,y;
public static double f(double x0){
return a*x0*x0*x0+b*x0*x0+c*x0+d;
}
public static void main(String[] args) {
System.out.println("Enter Coeft for Equation: ax^3+bx^2+cx+d = 0");
Scanner sc= new Scanner(System.in);
System.out.print("a= ");
a=sc.nextDouble();
System.out.print("b= ");
b=sc.nextDouble();
System.out.print("c= ");
c=sc.nextDouble();
System.out.print("d= ");
d=sc.nextDouble();
System.out.println("Initial Point :");
System.out.print("x1= ");
x1=sc.nextDouble();
System.out.print("x2= ");
x2=sc.nextDouble();
System.out.print("x3= ");
x3=sc.nextDouble();
h1=x1-x3; d1=f(x1)-f(x3);
h2=x2-x3; d2=f(x2)-f(x3);
a0=f(x3);
a1=(d2*h1*h1-d1*h2*h2)/h1*h2*(h1-h2);
a2=(d1*h2-d2*h1)/h1*h2*(h1-h2);
y=a1*a1-4*a2*a0;
h0=(a0>0)? -2*a0/(a1+Math.sqrt(y)) : -2*a0/(a1-Math.sqrt(y));
x=x3+h0;
int i=0;
while(f(x) <=0.00001 ){
if(f(x) !=0.00001){
h1=x1-x3; d1=f(x1)-f(x3);
h2=x2-x3; d2=f(x2)-f(x3);
a0=f(x3);
a1=(d2*h1*h1-d1*h2*h2)/h1*h2*(h1-h2);
a2=(d1*h2-d2*h1)/h1*h2*(h1-h2);
h0=(a0>=0)? -2*a0/(a1+Math.sqrt(a1*a1-4*a2*a0)) : -2*a0/(a1-Math.sqrt(a1*a1-4*a2*a0));
x=x3+h0;
x1=x2; x2=x3; x3=x;
}
System.out.println("x"+(i+1)+"= "+x+" f(x"+(i+1)+")= "+f(x));
i++;
}
System.out.println("Root is "+x);
}
}