Transcript
Page 1: Computer Science Assignment Help

Computer Science Assignment Help

e-Assignmenthelp

© e-Assignmenthelp, 2014

Page 2: Computer Science Assignment Help

Topic: Cubic Spline Interpolation using MATLAB

Problem statement: Write MATLAB codes to perform the following actions

(a) Read a given number of points from the command prompt and apply cubic spline

interpolation(b) Generate plots of the cubic splines

generated by MATLAB from part (1)(c) Calculate the areas under individual

splines

© e-Assignmenthelp, 2014

Page 3: Computer Science Assignment Help

Solutions

(1) 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. • The second derivatives at the starting and

ending points are zero.• The second derivatives and first derivatives at

the shared nodes are equal.• The Spline is continuous at all the nodes.

© e-Assignmenthelp, 2014

Page 4: Computer Science Assignment Help

MATLAB Codeprompt= ' 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

© e-Assignmenthelp, 2014

Page 5: Computer Science Assignment Help

MATLAB Code (contd.)

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

© e-Assignmenthelp, 2014

Page 6: Computer Science Assignment Help

MATLAB Code (contd.)

© e-Assignmenthelp, 2014

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;

Page 7: Computer Science Assignment Help

MATLAB Code (contd.)

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

© e-Assignmenthelp, 2014

Page 8: Computer Science Assignment Help

Solutions(contd.)

(2)The following scripts were used in

the plotting of the three splines. The scripts use the formula

© e-Assignmenthelp, 2014

Page 9: Computer Science Assignment Help

MATLAB Code

CubicSpline2 for i=1:20q(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));endplot(q,y1);hold onclear q;for i=1:10q(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

© e-Assignmenthelp, 2014

Page 10: Computer Science Assignment Help

MATLAB Code (contd.)

© e-Assignmenthelp, 2014

plot(q,y2);hold onclear q;for i=1:10q(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));endplot(q,y3);hold onclear q;for i=1:10q(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

Page 11: Computer Science Assignment Help

MATLAB Code (contd.)plot(q,y4);hold onclear q;plot([1,3,4,5,6],[2,3,2,1,5],'o');hold onAreaSpline(n,x,a,b,c,d)

CubicSpline2for i=1:10q(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));endplot(q,z1);

© e-Assignmenthelp, 2014

Page 12: Computer Science Assignment Help

MATLAB Code (contd.)hold onclear q;for i=1:5q(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));endplot(q,z2);hold onclear q;for i=1:15q(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));

© e-Assignmenthelp, 2014

Page 13: Computer Science Assignment Help

MATLAB Code (contd.)endplot(q,z3);hold onclear q;for i=1:10q(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));endplot(q,z4);hold onclear q;for i=1:10q(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

© e-Assignmenthelp, 2014

Page 14: Computer Science Assignment Help

MATLAB Code (contd.)

plot(q,z5);hold onclear q;for i=1:20q(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));endplot(q,z6);hold onclear q;plot([4,5,5.5,7,8,9,11],[6,6,8,3,6,3,9],'x');hold on

© e-Assignmenthelp, 2014

Page 15: Computer Science Assignment Help

Solutions(contd.)Plot of splines with their respective points

© e-Assignmenthelp, 2014

Page 16: Computer Science Assignment Help

Solutions(contd.)The area under each spline was found out by integrating the spline under the

bounds. This is done by the formula:

Where n is the number of splines/. This area was found using MATLAB script AreaSpline.

© e-Assignmenthelp, 2014

Page 17: Computer Science Assignment Help

MATLAB Code (contd.)

function D= AreaSpline(n,x,a,b,c,d)Area=0;for j=1:n-1 l(j)= x(j+1)-x(j); Area=Area+ a(j)+(b(j)*(l(j)^2)/2)+(c(j)*(l(j)^3)/3)+(d(j)*(l(j)^4)/4);end D=Area; End

© e-Assignmenthelp, 2014