Spline Interpolation and End Function Essay

Submitted By yackir
Words: 1906
Pages: 8

Numerical methods and MATLAB assignment

MATLAB Code for Cubic Spline Interpolation:
For this code, the points are first read from the user by means of the input function at the prompt. Then, the boundary conditions are applied. 1. The second derivatives at the starting and ending points are zero. 2. The second derivatives and first derivatives at the shared nodes are equal. 3. The Spline is continuous at all the nodes.

prompt= ' Enter the number of points for which the spline is to be constructed\n'; n=input(prompt); fprintf('The number of points for which the spline is to be constructed is %d\n',n); x=[]; y=[]; for i=1:n prompt= 'Enter the x coordinate of point :\n'; x(i)= input(prompt); prompt='Enter the y coordinate of point :\n'; y(i)=input(prompt); end for i=1:n-1 h(i)= x(i+1)-x(i); end for i=1:n a(i)=y(i); end for i=2:n-1 A(i)= (((3*(a(i+1)-a(i)))/h(i))-((3*(a(i)-a(i-1)))/h(i-1))); end I(1)=1; M(1)=0; Z(1)=0; for i=2:n-1 I(i)=2*(x(i+1)-x(i-1))-(h(i-1)*M(i-1)); M(i)=(h(i)/I(i)); Z(i)= ((A(i)-(h(i-1)*Z(i-1)))/I(i)); end

I(n)=1; c(n)=0; Z(n)=0; for j=n-1:-1:1 c(j)=Z(j)-(M(j)*c(j+1)); b(j)=((a(j+1)-a(j))/h(j))-((h(j)*(c(j+1)+(2*c(j))))/3); d(j)=((c(j+1)-c(j))/(3*h(j))); end

for j=1:n-1 a(j) b(j) c(j) d(j) end

The following scripts were used in the plotting of the three splines. The scripts use the formula


CubicSpline2 for i=1:20 q(i)=1+(i/10); y1(i)= a(1)+(b(1)*(q(i)-1))+(c(1)*((q(i)-1)^2))+(d(1)*((q(i)-1)^3)); end plot(q,y1); hold on clear q; for i=1:10 q(i)= 3+(i/10); y2(i)= a(2)+(b(2)*(q(i)-3))+(c(2)*((q(i)-3)^2))+(d(2)*((q(i)-3)^3)); end plot(q,y2); hold on clear q; for i=1:10 q(i)= 4+(i/10); y3(i)= a(3)+(b(3)*(q(i)-4))+(c(3)*((q(i)-4)^2))+(d(3)*((q(i)-4)^3)); end plot(q,y3); hold on clear q; for i=1:10 q(i)= 5+(i/10); y4(i)= a(4)+(b(4)*(q(i)-5))+(c(4)*((q(i)-5)^2))+(d(4)*((q(i)-5)^3)); end plot(q,y4); hold on clear q; plot([1,3,4,5,6],[2,3,2,1,5],'o'); hold on AreaSpline(n,x,a,b,c,d)

CubicSpline2 for i=1:10 q(i)=4+(i/10); z1(i)= a(1)+(b(1)*(q(i)-4))+(c(1)*((q(i)-4)^2))+(d(1)*((q(i)-4)^3)); end plot(q,z1); hold on clear q; for i=1:5 q(i)= 5+(i/10); z2(i)= a(2)+(b(2)*(q(i)-5))+(c(2)*((q(i)-5)^2))+(d(2)*((q(i)-5)^3)); end plot(q,z2); hold on clear q; for i=1:15 q(i)= 5.5+(i/10); z3(i)= a(3)+(b(3)*(q(i)-5.5))+(c(3)*((q(i)-5.5)^2))+(d(3)*((q(i)-5.5)^3)); end plot(q,z3); hold on clear q; for i=1:10 q(i)= 7+(i/10); z4(i)= a(4)+(b(4)*(q(i)-7))+(c(4)*((q(i)-7)^2))+(d(4)*((q(i)-7)^3)); end plot(q,z4); hold on clear q; for i=1:10 q(i)= 8+(i/10); z5(i)= a(5)+(b(5)*(q(i)-8))+(c(5)*((q(i)-8)^2))+(d(5)*((q(i)-8)^3)); end plot(q,z5); hold on clear q; for i=1:20 q(i)= 9+(i/10); z6(i)= a(6)+(b(6)*(q(i)-9))+(c(6)*((q(i)-9)^2))+(d(6)*((q(i)-9)^3)); end plot(q,z6); hold on clear q; plot([4,5,5.5,7,8,9,11],[6,6,8,3,6,3,9],'x'); hold on AreaSpline(n,x,a,b,c,d)

CubicSpline2 for i=10:-1:1 q(i)=3-(i/10); t1(i)= a(1)+(b(1)*(q(i)-3))+(c(1)*((q(i)-3)^2))+(d(1)*((q(i)-3)^3)); end plot(q,t1); hold on clear q; for i=10:-1:1 q(i)= 2-(i/10); t2(i)= a(2)+(b(2)*(q(i)-2))+(c(2)*((q(i)-2)^2))+(d(2)*((q(i)-2)^3)); end plot(q,t2); hold on clear q; for i=1:40 q(i)= 1+(i/10); t3(i)= a(3)+(b(3)*(q(i)-1))+(c(3)*((q(i)-1)^2))+(d(3)*((q(i)-1)^3)); end plot(q,t3); hold on clear q; plot([3,2,1,5],[4,2,6,3],'v'); hold on


‘o’ denotes 1st set of points.
‘x’ denotes 2nd set of points.
‘v’ denotes 3rd set of points.

The area under each spline was found out by integrating the spline under the bounds. This is done…