# HW 4 - 1 HW 4 function[a,b,c,d]= NaturalSpline(x,y compute...

• Homework Help
• 5
• 100% (8) 8 out of 8 people found this document helpful

This preview shows page 1 - 3 out of 5 pages.

HW 4 1 function [a,b,c,d]= NaturalSpline(x,y) % compute natural spline 's coefficents % coefficents:a,b,c,d %S(x)=Sj(x)=a_j+b_j(x-x_j)+c_j(x-x_j)^2+d_j(x-x_j)^3,x in [x_j,x_i+1] % compute a n=length(x)-1; a=y; h=diff(x); % construct tridiagonal solver for i=1:n-1 alpha(i+1)=3/h(i+1)*(a(i+1+1)-a(i+1))-3/h(i)*(a(i+1)-a(i)); end l(0+1)=1; u(0+1)=0; z(0+1)=0; % solve c using tridiagonal solver % forward elimination for i=1:n-1 l(i+1)=2*(x(i+1+1)-x(i-1+1))-h(i-1+1)*u(i-1+1); u(i+1)=h(i+1)/l(i+1); z(i+1)=(alpha(i+1)-h(i-1+1)*z(i-1+1))/l(i+1); end l(n+1)=1; z(n+1)=0; c(n+1)=0; % backward solve for j=n-1:-1:0 c(j+1)=z(j+1)-u(j+1)*c(j+1+1); b(j+1)=(a(j+1+1)-a(j+1))/h(j+1)-h(j+1)*(c(j+1+1)+2*c(j+1))/3; %solve d d(j+1)=(c(j+1+1)-c(j+1))/(3*h(j+1)); end % select the coefficents of spline a=a(1:n); b=b(1:n); c=c(1:n); d=d(1:n); end question 1 S(x)=Sj(x)=a_j+b_j(x-x_j)+c_j(x-x_j)^2+d_j(x-x_j)^3, x in [x_j,x_i+1] a_j b_j c_j d_j 0 -2.5000 0 3.5000 1.0000 8.0000 10.5000 -3.5000
2 function [u,v]= fun(z) t=[0,0.618,0.935,1.255,1.636,1.905,2.317,2.827,3.33]; x=[1.5,0.9,0.6,0.35,0.2,0.1,0.5,1,1.5]; y=[0.75,0.9,1,0.8,0.45,0.2,0.1,0.2,0.25]; [a1,b1,c1,d1]= NaturalSpline(t,x); [a2,b2,c2,d2]= NaturalSpline(t,y); u=0; v=0; for j=1:8 if z<=t(j+1)