You are on page 1of 2

EXPERIMENT No.

12
The Legendre polynomials (Pn(x)) are defined by the following recurrence relation
(n+1) Pn+1(x) ‐ (2n+1) Pn(x) + n Pn‐1(x) = 0
with P0(x) = 1, P1(x) = x and P2(x) = (3x2 ‐ 1)/2. Compute the next three Legendre
polynomials and plot all 6 over the interval [‐1,1].

% ------------------------------------------------------------------------------
% PROGRAM TO COMPUTE THE "LEGENDRE POLYNOMIAL"
% (n+1) Pn+1(x) - (2n+1) Pn(x) + n Pn-1(x) = 0
% with P0(x) = 1, P1(x) = x and P2(x) = (3x2 - 1)/2.
% where, Pn(x)=((2n-1)/n) xPn-1(x) + ((1-n)/n) Pn-2(x)
% ----------------------------------------------------------------------------
clc,clear all; close all;
n=input('get the power of polynomials:');
[x]=input('get the vector:');
figure;
hold all
for i=0:n
p=rlegendre(i); %call function,return coeff of polynomial
p_val=polyval(p,x); %calculating the polynomial value
plot(x, p_val,'LineWidth',i+1 )
end
grid on
i=0:n;
le=int2str(i');
legend(le(:))
xlabel('-1<=x<=1');ylabel('Pn(x)');
title('legendre polynomial', 'FontSize',15)
RESULT

get the power of polynomials:5


get the vector:-1:0.01:1

You might also like