Click to like/follow us on:
In my previous post, I made the algorithm that solve the Lagrange’s interpolation polynomial, very simple to understand. The algorithm was explained in a such a way that one  can develop a programme from the algorithm using any of the languages very easy.

In this post you will learn how to write a java programme that solve the Lagrange’s interpolation polynomial equation. I developed the java programme strictly from the Lagrange's interpolation algorithm given in my previous post. The name of the source file is LagrangePolynomial.java.
The type of problems that LagrangePolynomial.java will be solving is that which is similar to the one below.
Instance one:
From the table below determine/estimate the value of  y, when x = 2.3?
x1.11.73.0
F(x) = y10.615.220.3
From the table using the Newton method we can say that 2.3 lies between 1.7 and 3.0 in the x field. If this true it therefore means that y lies between 15.2 and 20.3. The Lagrange’s polynomial equation helps us to determine/solve a problem similar to the one above.
Solution:
From the above equations, we have that;
p(x) = Summation{Li(x)f(xi) = L0(x)f(x0) + L1(x)f(x1) + L2(x)f(x2)
L0(x) = [{(x - x1)(x - x2) / (x0 - x1)(x0 - x2)} * f(x0)]
L0(x) =  [{(2.3 - 1.7)(2.3 - 3.0) / (1.1 - 1.7)(1.1 - 3.0)} * (10.6)]
L0(x) = {(0.6)(-0.7) / (-0.6)(-1.9)} * 10.6 = (-0.42/1.4) * 10.6
L0(x) = -0.36 * 10.6 = -3.90526.
L1(x) = [{(x - x0)(x - x2) / (x1 - x0)(x1 - x2)} * f(x1)]
L1(x) = [{(2.3 - 1.1)(2.3 - 3.0) / (1.7 - 1.1)(1.7 - 3.0)} * (15.2)]
L1(x) = {(1.2)(-0.7) / (0.6)(-1.3)} * 15.2
L1(x) = (-0.84/-0.78) * 15.2
L1(x) = 1.076 * 15.2 = 16.3692.
L2(x) = [{(x - x0)(x - x1) / (x2 - x0)(x2 - x1)} * f(x2)]
L2(x) = [{(2.3 - 1.1)(2.3 - 1.7) / (3.0 - 1.1)(3.0 - 1.7)} * (20.3)}
L2(x) = {(1.2)(0.6) / (1.9)(1.3)} * 20.3
L2(x) = (0.72/2.47) * 20.3
L2(x) = 0.2914 * 20.3 = 5.91740.

From p(x) = Summation{Li(x)f(x) = L0(x)f(x0) + L1(x)f(x1) + L2(x)f(x2)
p(x) = -3.90526 + 16.3692 + 5.91740 = 18.38137.
Therefore When x = 2.3,  f(x) = 18.38137.

Our mission here is not to learn the arithmetic/mathematics as the one above but to develop a java code from already established algorithm that can solve the same problem or a similar problem as the one above, in all conditions; at any data points.

Here is the code:
/*
 ***************************************************************************
 * LagrangePolynomial.java                                                 *                                          
 ***************************************************************************
 * Programme to implement Lagrangian Method of interpolation polynomial    *
 *******************************************************************************************************
 * source: www.entangle-pair.blogspot.com/2014/12/lagranges-interpolation-polynomial-in-java.form.html *
 *******************************************************************************************************
 *************************
 *************************
 * @author Ezehlivinus  **
 * @date Dec 27, 2014   **
 * @time 5:15:16 AM     **
 *************************
 *************************
 */

 // This is the pachage name, created by Netbeans IDE, it is a folder 
 //where all the files concerning this programme will be stored.
package lagrangepolynomial;

import java.util.Scanner;

public class LagrangePolynomial
{
       public static void main(String[] args)
       {
           //Declaration of the scanner variable
           Scanner myScanner = new Scanner(System.in);
           
           //Declaration of variables
           int n; //Number of terms
           int count, count2; //Loop variables, for counting loops
           
           float [] arrayx = new float[200]; //Array limit 199
           float [] arrayy = new float[200]; //Array limit 199
           //The arbitrary value, x to be entered for
           //which the value of y can be known
           float x = 0; 
           float y = 0; //The corresponding value, f(x)=y
           float numerator; //The numerator
           float denominator;  //The denominator
   
           //Promt a user to enter a value
        System.out.print("Enter the number of terms n: "); 
        n = myScanner.nextInt(); //Store the value in n
    
           //Promt user to enter the array for X
        System.out.println("Enter the values that are in xi.");
           
            for(count = 0; count<n; count++) //Start the loop for X
            {
                 //Promp the user to enter the sequel for xi
                System.out.print("Enter the value for x" + count + ": ");
                //Store the sequel in the Array, arrayx
                arrayx[count] = myScanner.nextFloat(); 
            }
            //Promt user to enter the array for Y
            System.out.println("Enter the values that are in yi.");
            for(count = 0; count<n; count++) // loop for Y
            {
                //Promp the user to enter the sequel for yi
                System.out.print("Enter the value for y" + count + ": ");
                //Store the sequel in the Array, arrayy
                arrayy[count] = myScanner.nextFloat();
            }
            //Promp the user to enter any (the arbitray) 
            //value x to get the corresponding value of y
            System.out.print("Enter the arbitrary value x for which you want the value y: ");
            x = myScanner.nextFloat();  //Store the value in x
            
            //first Loop for the polynomial calculation
            for(count = 0; count<n; count++)
            {
                 //Initialisation of variable
                numerator = 1; denominator = 1;
                
                //second Loop for the polynomial calculation
                for(count2 = 0; count2<n; count2++)
                {
                    if (count2 != count)
                    {
                      numerator = numerator * (x - arrayx[count2]);
                      denominator = denominator * (arrayx[count] - arrayx[count2]);
                    }  
                }
                y = y + (numerator/denominator) * arrayy[count];
            }
            System.out.println("When x = " + x + "," + " y = " +  y);
    }
}

How the code solve the equation is as explained in the Lagrange's Interpolation Polynomial Algorithm. The code has being tested on Windows machined using Netbeans IDE and Command Prompt.
Using the same data provided in the above table at instance one, the output of the programme when run in Netbeans IDE is:

run:
Enter the number of terms n: 3
Enter the values that are in xi.
Enter the value for x0: 1.1
Enter the value for x1: 1.7
Enter the value for x2: 3.0
Enter the values that are in yi.
Enter the value for y0: 10.6
Enter the value for y1: 15.2
Enter the value for y2: 20.3
Enter the arbitrary value x for which you want the value y: 2.3
When x = 2.3, y = 18.381378
BUILD SUCCESSFUL (total time: 20 seconds)
SHARE THIS POST WITH YOUR FRIENDS...::

6 comments:

  1. Well, I have got the best information from here the site is fully stuffed with the knowledgeable information.
    web design websites

    ReplyDelete
    Replies
    1. Lagrange'S Interpolation Polynomial In Java Form ~ Entanglement >>>>> Download Now

      >>>>> Download Full

      Lagrange'S Interpolation Polynomial In Java Form ~ Entanglement >>>>> Download LINK

      >>>>> Download Now

      Lagrange'S Interpolation Polynomial In Java Form ~ Entanglement >>>>> Download Full

      >>>>> Download LINK sY

      Delete
  2. Superb way of explaining, and great blog to get wonderful information.
    experience design firm

    ReplyDelete
  3. Every week-end I used to pay a fast visit this site, because I’d like enjoyment, because this web site conations certainly fussy material.
    digital design agency San Francisco

    ReplyDelete
  4. Emperor Casino – Best Online Casino
    Play the best online casino games at Emperor 제왕 카지노 Casino. Our 바카라 사이트 casino offers some of the best bonuses in the worrione industry. You will find many of the best welcome

    ReplyDelete
  5. This code would have been a lot nicer if it had been split up into a method that actually finds the polynomial coefficients given points, and then another method that computes values based on those coefficients. As is right now, people looking for code to form the actual Lagrange polynomial will have a hard time turning this code into the "functionally important part" of Lagrange interpolation.

    ReplyDelete

All Posts

Access all posts here!

Categories

Unordered List

Sample Text

Lists of Jobs in the United States

List of Courses & Outlines For UNN Students

Popular Posts

Like us on Facebook

Recent Posts

    Text Widget