forked from wedusk101/NumericalAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Forward Driver
83 lines (77 loc) · 3.29 KB
/
Forward Driver
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.Scanner; // importing the Scanner class of the 'util' package to accept necessary inputs from the user
import NewtonInterpolation.*; // importing the created package which contains the methods to accept and calculate the process
class InvalidNodeException extends Exception
{
// class to deal with the exception of the 'pivot' node not lying between the interpolating points
}
class InvalidStepLengthException extends Exception
{
// class to deal with the exception of the 'step-length' not being uniform
}
public class ForwardDriver
{
public static void main(String[] args)
{ // start of main
Scanner obj=new Scanner(System.in); //creating object of the scanner class
// declaring and initializing necessary variables
double x[],y[][],pivot=0,h,temp;
int n;
boolean check=true;
System.out.println("\nPlease enter number of nodes...\n");
n=obj.nextInt(); // accepting from the user the number of nodes to be inserted
// creating the arrays
x=new double[n];
y=new double[n][n];
for(int loop=0;loop<n;++loop)
{
System.out.print("\nEnter the value of x"+loop+": ");
x[loop]=obj.nextDouble(); // accepting interpolating points
System.out.print("\nEnter the value of y"+loop+": ");
y[0][loop]=obj.nextDouble(); // accepting corresponding functional values
}
h=x[1]-x[0]; // calculating step-length
h=Double.parseDouble(String.format("%.1f",h)); // for some inputs the floating point representations can hamper the desired result; so setting precision for such floating point numbers
System.out.println("\nEnter x for finding f(x): ");
pivot=obj.nextDouble(); // accepting the point at which the functional value is required
try
{
if(pivot<x[0] || pivot>x[n-1]) // if condition to check if the user given point is an extrapolating point
{
check=false; // changing the value of the check variable to keep a note that an exception has taken place
InvalidNodeException ine=new InvalidNodeException();
throw ine;
}
}
catch(InvalidNodeException obj1) // corresponding catch block
{
System.out.println(obj1); // print exception
}
try
{
for(int loop=0;loop<n-1;++loop)
{
temp=x[loop+1]-x[loop];
temp=Double.parseDouble(String.format("%.1f",temp)); // for some inputs the floating point representations can hamper the desires result; so setting precision for such floating point numbers
if(temp!=h) // if condition to check if the interpolating points are not equi-space
{
check=false; // changing the value of the check variable to keep a note that an exception has taken place
InvalidStepLengthException iine=new InvalidStepLengthException();
throw iine;
}
}
}
catch(InvalidStepLengthException obj2) // corresponding catch block
{
System.out.println(obj2); // print exception
}
if(check) // if no exception has occurred
{
Forward forward=new Forward(x,y,n,pivot); // create an object of the 'Forward' class and pass the accepted inputs through the parameterized constructor
forward.forwardCal(); // call the method to perform Newton's Forward Interpolation using the recently object created
}
else // if exception has occurred
{
System.out.println("\nINHERENT ERROR.\n"); // there are some errors in the entered entries
}
}
}