Pn.m code: function P = Pn(X,x,y) n = length(x)-1; dX = ones(n+1,1)*X - x\'*ones
ID: 3109437 • Letter: P
Question
Pn.m code:
function P = Pn(X,x,y)
n = length(x)-1;
dX = ones(n+1,1)*X - x'*ones(1,length(X));
for k = 1:n+1
i = [ 1:(k-1) (k+1):n+1];
L(k,:) = prod(dX(i,:)./((x(k)-x(i))'*ones(1,length(X))));
end
P = sum(y'*ones(1,length(X)).*L);
end
Legendre Polynomials and Chebyshev Polynomials You are asked to submit the following five functions: 1. function y f (x) 2. function P n (x,x,y) 3. function y LegPoly(n,x) 4. function L LegPolyApprox CX,n 5. function TestPoly (n) The first function f.m should be function y f (x) y exp(sin (x 5)); end The second function Pn.m can be copied from homework 3 solution. 1 overall Requirements We will run your code from matlab console by typing TestPoly (5) 0.6476 0.4001 0.2753 0.1912 In addition to these numbers, we expect to see two figures which should be essentially the same as Figure 1. We will test the code for any 2 S n S 100. In order to encourage students write reasonably efficient code, the program should not take more than 1 minute (which is much more than necessary).Explanation / Answer
function TestPoly(n)
for X= -1: 0.001 : 1
y = f(X)
x =X
C(x)= Pn(X,x,y,n)
L(x) = LogpolyApprox(X,y,n)
err(x) = //as given in question
end
plot(C,X)
plot(L,X)
print(err)
end
function P =Pn(..///given in question add n as argument
function LogpolyApprox(X,y,n)
sum = 0
for k = 1:n
b =integral(PF,-1,1,X,yk-1)
d= (2k-1)*b/2
sum = sum + d*P(X,x,y,k-1)
end
return sum
function PF(X,x,y,n)
return f(x) *Pn(X,x,yn)
function f(x)
return exp(sin(x*5))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.