-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy paths_convergence.java
More file actions
46 lines (45 loc) · 1.24 KB
/
Copy paths_convergence.java
File metadata and controls
46 lines (45 loc) · 1.24 KB
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
import Classes.*;
import java.util.*;
public class s_convergence
{
public static void main(String[] args)
{
s_function f;
String exp;
Scanner reader = new Scanner(System.in);
System.out.print("Enter expression of the function : ");
exp = reader.nextLine();
f = new s_function(exp);
Float value;
System.out.print("Enter value of f for wich x has to be calculated : ");
value = reader.nextFloat();
Float x = 1f;
s_variable_table table = f.get_variables_table();
table.add_variable("x",Float.toString(1f));
Float diff = f.get_value(table) - value;
System.out.println("\nInitial difference is "+diff);
int iteration = 1;
float ans=value;
while(diff != 0f)
{
try
{
x = x-((diff/100f)*x);
ans = f.get_value(table);
table.add_variable("x",Float.toString(x));
diff = f.get_value(table)-value;
}
catch(Exception e)
{
break;
}
//System.out.printf("\nIteration%d , f = %.6f, x = %.6f\n",iteration,f.get_value(table),x);
// System.out.printf("diff = %.9f",diff);
//reader.nextLine();
iteration++;
if(iteration == 200)
break;
}
System.out.printf("\nf(%.9f) = %.9f\n",x,ans);
}
}