Legendre polynomials for solving 2nd order ODE

How can i employ legendre polynomials for numerical examples? how to map the boundary conditions?

1 Comment

The code i wrote is this:
clear all;
close all;
clc
warning off;
%initialization
syms x;
int_initial=0; % x=0 mapped [Mapping has been done! 0<x<1, VVI]
int_final=pi/2; % x=1 mapped
N=5; % number of coeffienct= N+1
% Bernstein Polynomial
a=int_initial;
b=int_final;
for i=1:N+1
leg_poly(1,i)=collect(1/factorial(i)*diff((x^2-x)^i,i)-(-1)^i)*(x-1);
end
leg_poly
% least square function
fi(x)=1-4*x/pi; % initial function
u=[fi(x) leg_poly]% polynomial function fi(x)+ fi_N(x)
funct(x)=[cos(2*x) zeros(1,N+1)] % given function
deriv_u=(diff(u,x,2))
% % funct2(x)=[(x^2) zeros(1,N+1)]
% %diff(u, x,2)
% l= x*diff(u, x,2)
%
% m=diff(u, x,1) %*transpose(funct1(x))
% %m=transpose(funct(x))*diff(u,x,1)
% deriv_u=l+m
%
% %H=(-deriv_u+funct(x))
% % double derivative
R=simplify(-deriv_u-9*u+funct(x)) % residual
size(R)
for n=1:N+1 %N+1
% WR(n,:)=vpa(int(R(n+1)*R,x,int_initial,int_final))
WR(n,:)=vpa(int(R(n+1)*R,x,int_initial,int_final));
end
WR; % coefficient matrix with equation
% solving system of equations
B=-1*WR(:,1)
A=WR(:,2:end)
% A\B
%
% %adjustment of matrix A and B due to Berstein polynomial
A(:,[1 end])=0;
A([1 end],:)=0
A;
B([1 end],:)=0
B;
% solving for systems of equations
C=linsolve(A,B) % coefficient matrix
leg_poly.*C'
%plugging the values of coefficient in least squares expression
u_least(x)=expand(sum([fi(x) leg_poly.*C']))
% evaluating polynomials
u_least_val=@(x)u_least(x)
format long
value= 0:0.01:pi/2;
least_square_method_sol=double((u_least_val(value))')
exact_fun=(4/5)*cos(3*x)+(4/5)*sin(3*x)+(1/5)*cos(2*x)
exact_sol_exp=@(x)((4/5)*cos(3*x)+(4/5)*sin(3*x)+(1/5)*cos(2*x)); % exact solution taken for 0:0.1:2
exact_sol=exact_sol_exp(value)'
value_list=value'
error=least_square_method_sol-exact_sol
% comparison with exact solution
T=table(value_list,least_square_method_sol,exact_sol,error)

Sign in to comment.

Answers (0)

Asked:

on 4 Feb 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!