0% found this document useful (0 votes)
96 views

Lab Exercise 1

This document provides instructions for 4 numerical methods exercises involving polynomial interpolation. 1) Use Vieta's formula to compute sequences that converge to pi. 2) Write a function to compute polynomial interpolation using the Vandermonde matrix. 3) Download and test functions for Lagrange polynomial interpolation. Modify scripts to visualize different basis functions and interpolation problems. 4) Write a function to compute Lagrange polynomial interpolation symbolically using loops. Create a script to test it on sample data.

Uploaded by

leopard9987
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

Lab Exercise 1

This document provides instructions for 4 numerical methods exercises involving polynomial interpolation. 1) Use Vieta's formula to compute sequences that converge to pi. 2) Write a function to compute polynomial interpolation using the Vandermonde matrix. 3) Download and test functions for Lagrange polynomial interpolation. Modify scripts to visualize different basis functions and interpolation problems. 4) Write a function to compute Lagrange polynomial interpolation symbolically using loops. Create a script to test it on sample data.

Uploaded by

leopard9987
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Numerical Methods LAB EXERCISE 1: NUMERICAL METHODS

BISHNU P. LAMICHHANE

(1) This question is based upon Vi etes formula for calculating . Let the sequences {xn } n=0 and {yn }n=0 be dened by xn+1 = 2 + xn 2yn yn+1 = xn+1 and where x0 = 0 and y0 = 2. (a) Use a for loop to nd the rst 20 or so terms in each sequence and use this to guess what the limit is for at least one of the sequences. Type edit on the matlab prompt and press enter. Then type the following code and save the le as viete.m. You can now press the green arrow on the menu bar to execute the code. x(1)=0;y(1)=2; for n=1:19 x(n+1)=sqrt(2+x(n)) y(n+1)=y(n)*2/x(n+1) end Now add the plotting command to plot the vectors x and y against the vector t=1:20 in the same le. Note that x and y both are vectors with 20 components here. (b) Use a while loop to evaluate both sequences until you have 10 signicant digits of the limit you guessed. If you guess incorrectly or made some other mistake and it seems the loop will never end, then press ctrl+c to force the calculation to stop. Here the sequence for yn converges to . You can use a while loop to compute the sequence until you get 10 signicant digits in this way: x=0;y=2; while abs(pi-y)/pi > 0.5*10^(-10) x=sqrt(2+x) y=y*2/x end (2) An interpolation routine needs three inputs (all column vectors): xdata, ydata, and xvalues. Here xdata and ydata are vectors with ith components xi and yi , where {(xi , yi )}n i=0 is the set of interpolation points; and xvalues are the points where you want to compute the interpolant. The output poly of the routine is the vector giving the values of the interpolant at xvalues. Write a matalb function InterpVand.m with inputs xdata, ydata, and xvalues; and output poly to give the values of the polynomial interpolant at xvalues. In short, perform this task in the following steps: (a) Set up the Vandermonde matrix V
1

BISHNU P. LAMICHHANE

(b) Solve the linear system of equation Va = ydata using the backslash operator in matlab: a = V\ydata. (c) Compute the interpolant p(x) = a(1) + a(2)x + a(3)x2 + + a(n)xn1 at xvalues. (d) Visualise the interpolation polynomial for the data (4, 33.47), (3, 22.25), (2, 2.02), (2, 18.02), (3, 13.75), (4, 30.53). Note that you will create two les in this process: a function le (three steps below) and a script le (last step below). Previous four steps are now explained in detail: (a) Type edit and press enter at matlab prompt and start the le with the function denition function poly = InterpVand(xdata,ydata,xvalues) You set up the Vandermonde matrix by entering the following matlab codes: n = length(xdata); V = ones(n,n); for j=2:n % Set up column j. V(:,j) = xdata.*V(:,j-1); end (b) You then solve the Vandermonde system using the command a = V\ydata;. The vector a provides all the coecients for the monomial basis. Thus the interpolant is given by p(x) = a(1) + a(2)x + a(3)x2 + + a(n)xn1 , where a(i) is the ith component of the vector a. (c) It is easy to compute p(x) if x is a scalar. But if x is a vector, it is not that easy. We do this using a for loop %compute the interpolant p(x) at vector xvalues m=length(xvalues); poly=a(1)*ones(m,1) for k=2:n poly= poly + a(k)*xvalues.^(k-1); end Save your function le as InterpVand.m. (d) Now visualise the result of the function by writing the following codes in the script le PlotInterpVand.m. Try to understand each line of the code. figure; xvalues=linspace(-4,4); xdata=[-4,-3,-2,2,3,4]; ydata=[33.47,22.25,-2.02,18.02, 13.75,30.53] ypv=InterpVand(xdata,ydata,xvalues); plot(xvalues,ypv,r-,xdata,ydata,ok,linewidth,3); le=legend(interpolant,interpolation points); set([gca,le],fontsize,12); legend boxoff;

LAB EXERCISE 1: NUMERICAL METHODS

(3) Download the following programs from blackboard (Week 2): lagrangeb.m, plotlagrangeb.m, lagrangepoly.m and plotlagrangepoly.m into your folder Documents/MATLAB. Please do not change the name of these les unless you are condent to handle the changed names. You can right click on the le and select save target as to download the le. Here lagrangeb.m and lagrangepoly.m are function les and cannot be executed directly. They have to be called by their names. The two les: plotlagrangeb.m and plotlagrangepoly.m are script les and they can be executed by clicking the green arrow button on top of the editor. Understand how these scripts call functions lagrangeb.m and lagrangepoly.m. Now check whether you can run scripts plotlagrangeb.m and plotlagrangepoly.m by opening them and clicking the green arrow button. If you do not see any graphical output, there are some mistakes either in the name of these les or they are not all in the same directory. (a) Change the xdata in script plotlagrangeb.m to xdata=[-2,-1,1,2]. Then you have only four basis functions. Change the script accordingly to visualise these four basis functions. Plot all the points in xdata along the x-axis using the command plot(xdata,0,o,linewidth,5,markersize,12);. Use hold on command to add other graphs to the existing graph. (b) Consider the script plotlagrangepoly.m. Change the anonymous function f to f (x) = 12 sin(2x) cos(4x) + 2x2 + 3x + 12 and use 4 equidistant points in the interval [1, 1] as interpolation points. Compare the polynomial interpolant and the exact function using this script. Change the number of points to 5 and then to 6 to see the dierence. Change the interval of interpolation and visualisation to [4, 4] and use 14 interpolation points. What do you see? (4) This exercise is related to computing the interpolation polynomial in a symbolic form. Using loops, write a function LInterp.m that will take vectors xdata and ydata of length n as input, calculate the Lagrange basis functions for interpolating points with x-coordinates xdata and y -coordinates ydata and output the n 1-degree interpolating polynomial. You will need to calculate n x xj li (x) = for i = 1, . . . , n. x xj j =1 i
j =i

If you store these polynomials in the vector L, then you can more easily calculate the n interpolating polynomial pn (x) = i=1 yi li (x). In contrast to the notation in lecture notes, we are using here i = 1, , n as we have only n points. You might begin the le with the following code so that the variables x and L remain symbolic. function poly=LagrangeInterp(xdata,ydata) syms x L; n=length(xdata); L(1:n)=1; Create a script le called LagrangeExample.m that uses the function LagrangeInterp.m to compute a polynomial interpolant in symbolic form using some xdata and ydata. Plot your data and the polynomial interpolant. You can use the command ezplot to plot your interpolant.

You might also like