// Legendre.java Legendre polynomials and quadrature // // P0(x)=1 P1(x)=x Pn(x)=x*((2n-1)/n)*Pn-1(x) - ((n-1)/n)*Pn-2(x) // or Pn+1(x)=x*)((2n+1)/(n+1))Pn(x)- (n/(n+1))Pn-1(x) // P2(x)=3/2 x^2 - 1/2 P3(x)=5/2 x^3 - 3/2 x P4(x)=35/8 x^4 -30/8 x^2 + 3/8 // int -1 to 1 of f(x)dx = sum i=1,n w[i] F(x[i]) // w[i]= -2/((n+1)P'n(x[i])Pn+1(x[i])) // x[i]= roots of Pn(x) // // n=2 x= +/- .5773502692 w=1.0 // n=3 x= +/- .7745966692 w= .5555555556 // 0.0 .8888888889 // n=4 x= +/- .8611363116 w= .3478546451 // +/- .3399810436 .6521451549 // n=5 x= +/- .9061798459 .2369268851 // +/- .5384693101 .4786286705 // 0.0 .5688888889 // // conversion/scaling int a to b of f(x)dx = // (b-a)/2 int -1 to 1 of f((a+b+x*(b-a))/2)dx // // Gauss-Legendre quadrature, integrate f(x) from -1 to 1 // int -1 to 1 f(x) dx = sum i=0,n-1 w[i]f(x[i]) package myjava; import java.awt.*; import java.awt.event.*; import myjava.*; public class Legendre extends Frame { int N=15; // change to max polynomial size double P[][] = new double[N+1][N+1]; // coefficients of Pn(x) double DP[][] = new double[N][N]; // derivitives of Pn(x) double R[][] = new double[N][N]; // roots of Pn(x) the x[i] double W[][] = new double[N][N]; // weights of Pn(x) the w[i] public Legendre() { System.out.println("Legendre.java running"); // generate family of Legendre polynomials Pn(x) // as P[degree][coefficients] // build coefficients of Pn(x) for(int n=0; n0) P[n][i]=P[n][i]+((2*n-1)/(double)n)*P[n-1][i-1]; System.out.println("P["+n+"]["+i+"]="+P[n][i]); } System.out.println(); } // compute derivitives of Pn(x) for(int n=0; n4.95) ynew=4.95; if(ynew<-4.95) ynew=-4.95; if(x!=-1.0) // not first time { g.drawLine((int)(f2xC+(xold/1.0)*sca), (int)(f2yC-(yold/5.0)*sca), (int)(f2xC+(xnew/1.0)*sca), (int)(f2yC-(ynew/5.0)*sca)); } xold=xnew; yold=ynew; } } g.setColor(Color.red); Font cur16 = new Font("courier", Font.BOLD, 16); g.setFont(cur16); g.drawString("y=Pn(x)", 55, 50); g.drawString("y=P'n(x)", 355, 50); g.setColor(col[0]); g.drawString("P2(x) ____", 55, 310); g.setColor(col[1]); g.drawString("P3(x) ____", 55, 330); g.setColor(col[2]); g.drawString("P4(x) ____", 55, 350); g.setColor(col[3]); g.drawString("P5(x) ____", 55, 370); g.setColor(col[0]); g.drawString("P'2(x) ____", 355, 310); g.setColor(col[1]); g.drawString("P'3(x) ____", 355, 330); g.setColor(col[2]); g.drawString("P'4(x) ____", 355, 350); g.setColor(col[3]); g.drawString("P'5(x) ____", 355, 370); g.setColor(Color.black); g.drawString("Legendre Polynomials and derivatives", 100, 390); } private static double Evaluate(int n, double P[], double x) { double value=P[n]; for(int i=n-1; i>=0; i--) value = value*x+P[i]; return value; } public static void main (String[] args) // demo { new Legendre(); // construct and execute } }