60 likes | 222 Views
Lecture 16: Polynomial interpolation. Download polyfitexample.m. p=polyfit(x,y,n) creates list of interpolating polynomial coefficients of order n polyval(p,x) evaluates interpolating polynomial with coefficients p at x. Script polyfitexample %example of polyfit use x=[ -1 1 2];
E N D
Lecture 16: Polynomial interpolation Download polyfitexample.m p=polyfit(x,y,n) creates list of interpolating polynomial coefficients of order n polyval(p,x) evaluates interpolating polynomial with coefficients p at x.
Scriptpolyfitexample %example of polyfit use x=[ -1 1 2]; y=[1 3 -2]; n=length(y)-1; p=polyfit(x,y,n); xvec=linspace(-1,2,100); yvec=polyval(p,xvec); plot(xvec,yvec,x,y,'.')
Result: p = -2.0000 1.0000 4.0000
polyfitexample2.m function polyfitexample2%example of erro of polynomial interpolation xmin=0; xmax=5; nmax=10; %number of points which interpolate interval [xmin,xmax]; x=linspace(xmin,xmax,nmax); y=exp(x); n=length(y)-1; p=polyfit(x,y,n) xvec=linspace(xmin-1,xmax+2,500); yvec=polyval(p,xvec); yexact=exp(xvec); plot(xvec,yvec,x,y,'g+',xvec,yexact,'-'); legend('Interpolation','Interpolating points','Exact solution'); end
Number of interpolating points nmax=10