How can I convert this pseudo code to matlab?

35 views (last 30 days)
Diego Dranuta
Diego Dranuta on 10 Feb 2020
Answered: Koushik Vemula on 5 Mar 2020
Hi guys,
I am working on this little math problem where I have to convert a pseudo code to matlab to see how accurate Lagrange Interpolation is.
The pseudo code I am trying to covnert is:
# given: N, x[i],f[i] for i=0...N; x
sum = 0
do i=0,N # loop over i from 0 to N by 1
pp = 1;
do j=0,N # loop over j from 0 to N by 1
if (i not equal to j)
pp = pp * (x x[j]) / (x[i] x[j])
end if
end do
sum = sum + pp*f[i]
end do
The code I have gotten so far is:
%Assume X, N, [a,b] given
N=1000;
X = 1.0;
Dx=(10-0)/N;
xv=0:Dx:10;
f=xv.^2.*sin(pi*xv);
f_new=[];
for k = 1:length(xv)
sum = 0 ;
for i=0+1:N+1
pp=1;
for j=0+1:N+1
if j~=i
pp = pp *((X-xv(j))/(xv(i)-xv(j))); %li(x)
end
end
sum=sum+ f(i)*pp;
end
f_new(k) = sum;
end
plot(xv,f,xv,f_new)
The issue I have seen and I cannot get to solve is that the f_new is getting 0 value while it should be colse to f.
Here is some useful information for better understanfing of the problem
Implement Lagrange interpolation for the function
f(x) = x^2*sin(Pi*x)
using the input points {x0, x1., ⋯ xn} = {0,1, ⋯ 10}.
(a) Plot the function f(x) and the interpolating polynomial Pn(x) on the same plot, using at
least 1000 sample points uniformly distributed over the interval [0,10]
(b) Compute the absolute error |f((x) − Pn(x)| and plot it (separately) using the same sample
points.
(c) Repeat (a)(b) for different sets of input points: N = 5,20,50,100 and <xj=10j/N) j=0...N
Compute the maximum error over the sample points (for each set of input points), that is:
Emax(n) = max|f(xj)-Pn(xj)|
Plot the maximum error vs N.
The theory behind mathwise is:
MathTherory.PNG
Any ideas of how to solve it is much appreciated!
  1 Comment
Rik
Rik on 10 Feb 2020
I'm on mobile so testing code is difficult, but I do notice some things: you are using sum as a variable name, which shadows the builtin function, which is a bad idea. You are also using i and j, which are common, but should be avoided to avoid potential confusion with the imaginary units.
I also noticed your for loop syntax should be for n=0:1:N or simply for n=0:N.

Sign in to comment.

Answers (1)

Koushik Vemula
Koushik Vemula on 5 Mar 2020
According to what I understand the pseudo code you’ve written is correct with some minor points to be taken under consideration.
If the value of the variable ‘N’ is a multiple of 10 that is ‘1/Dx’ is an integer then there will be a case in your loop for every iteration of k-for loop where the value of the variable ‘xv(j)’ is equal to ‘X’ which will give you the result as zero .
Also you don’t need to have a for loop which uses the variable k as it gets the same answer for every iteration, In other words you are not using the variable kin the for loop so whatever you are expecting as the output of each iteration will be the same.

Tags

Community Treasure Hunt

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

Start Hunting!