Lagrange interpolation in JAVA -
i have done search around, there isnt code available in java hence have write own , have encountered issue. got code c++ source , trying hard convert workable java program.
http://ganeshtiwaridotcomdotnp.blogspot.sg/2009/12/c-c-code-lagranges-interpolation.html
public static void main(string[] args) { int n; int i, j; int a; int x[] = null; int f[] = null; int sum = 0; int mult; scanner input = new scanner(system.in); system.out.println("enter number of point: "); n = input.nextint(); system.out.println("enter value x calculation: "); = input.nextint(); (i = 0; < n; i++) { system.out.println("enter values of x , corresponding functional vale: "); x = input.nextint(); f = input.nextint(); } (i = 0; <= n - 1; i++) { mult = 1; (j = 0; j <= n - 1; j++) { if (j != i) { mult *= (a - x[j]) / (x[i] - x[j]); } sum += mult * f[i]; } } system.out.println("the estimated value of f(x)= " + sum); }
int x[] = null; int f[] = null; ... (i = 0; < n; i++) { .... x = input.nextint(); f = input.nextint(); }
looks clear enough. create array instance x , f:
x = new int[n]; f = new int[n];
somewhere after:
n = input.nextint();
before above for
loop, , modify for
body:
... x[i] = input.nextint(); f[i] = input.nextint();
Comments
Post a Comment