Chapter16 - Problem 16.11 Runge's function is written as f...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Problem 16.11 Runge's function is written as f ( x) 1 1 25 x 2 Generate five equidistant points in the interval [1, 1] evaluate the function there and fit it using various options. First the data: >> x=linspace(1,1,5) x = 1.0000 0.5000 0 0.5000 1.0000 >> runge=@(x) 1./(1+25*x.^2) runge = @(x)1./(1+25*x.^2) >> y=runge(x) y = 0.0385 0.1379 1.0000 0.1379 0.0385 Let us first use the crudest interpolation, which is to use the value of the nearest point >> xx=linspace(1,1); >> yy=runge(xx); >> yexact=runge(xx); >> yy=interp1(x,y,xx,'nearest'); >> plot(x,y,'o',xx,yy,xx,yexact,'') 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 Next we will do linear interpolation, which is also linear spline, and the default method of interp1 >> ylin=interp1(x,y,xx); >> plot(x,y,'o',xx,ylin,xx,yexact,'') 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 Next we will fit it with a cubic spline. We can do that by choosing `spline' in the method in interp1, or with the spline function (which gives additional flexibility on end conditions) >> yspl=spline(x,y,xx); >> plot(x,y,'o',xx,yspl,xx,yexact,'') 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 We can reduce the oscillation by specifying zero slope end conditions >> yspl0=spline(x,[0 y 0],xx); >> plot(x,y,'o',xx,yspl0,xx,yexact,'') 1.2 1 0.8 0.6 0.4 0.2 0 -0.2 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 Next we do Hermite (cubic interpolation) >> yher=interp1(x,y,xx,'cubic'); >> plot(x,y,'o',xx,yher,xx,yexact,'') 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 Finally, we use polyfit to do a single quartic polynomial 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 yquart=polyval(p,xx); >> plot(x,y,'o',xx,yquart,xx,yexact,'') ...
View Full Document

This note was uploaded on 03/27/2012 for the course EGM 3344 taught by Professor Raphaelhaftka during the Spring '09 term at University of Florida.

Ask a homework question - tutors are online