1. Write an M file spline.m such that spline(m(k),m(k+1),p(k), q(k),x0(k), x0(k+1) , x )...

1. Write an M file spline.m such that spline(m(k),m(k+1),p(k), q(k),x0(k), x0(k+1) , x ) computesSk(x) for x in the interval [x0(k) ,x0(k+1)]2. Write a matlab program in an M filespline_nat.mthat computes thenatural spline approximation of the set of data points (x0(k) , y0(k) ),k=0,1,..n. asspline_nat(x0,y0)that(a) Accepts x0 and y0 data points.(b) defines a(i), i=1,2,..,n, b(i), i=1,2,..n-1, c(i),i=1,2,..n-1 and theright hand side u(k), k=0,1,2,..n-2.(c) calls gesolve from question I to solve the resulting system. Com-pute p(i), i=0,...,n-1, and q(i), i=0,..,n-1.(d) calls spline.m to evaluateSk(x) and plot the spline approximation.On the same plot show the data points x0 and y0 asplot(x0,y0,’*’).3. Test your program on an example treated in class to make sure that itis working properly. x = [ -2,-1,0,1,2], y = [ 2,1,0,1,2], m =[ 0 , -6/7 ,24/7, -6/7, 0], p = [ 2,8/7, -4/7, 8/7], q =[ 8/7, -4/7 , 8/7, 2 ].4. Run the matlab smile.m program that reads each of the attached datafiles and callsspline_natseveral times to plot its natural spline ap-proximation. Plot the spline approximation of all data files on the samefigure to have a smiling face. Data files and the m-file ’smile.m’ will beposted in Canvas under Files.Problem III1. Write an M file dspline.m such that dspline(m(k),m(k+1),p(k), q(k),x0(k), x0(k+1) , x ) computes derivative ofSk(x) for x in the interval[x0(k) , x0(k+1)].22. Modify yourspline_nat.mtospline_clam.msuch thatspline_clam(x0,y0,fp0,fp1,func,funcprime)(a) accepts x0, y0 and the first derivatives of S(x), S’(x0(0)) = fp0,and S’(x0(n)) = fp1. func is the function to be interpolated,funcprime is the derivative of func.(b) defines a ,b,c and f for clamped spline and use gesolve to solve form(i), i=1,2,...,n-1. Compute m(0) and m(n).(c) Computes p and q(d) calls spline.m to plot the clamped spline and the exact functionon the same figure.(e) calls dspline.m to plot the derivative S’(x) and the derivative onthe function.(f) plots the true error, e(x) = S(x) - f(x), as a function of x. On aseparate figure plot the error interval by interval to include thepointsx0(i), e(x0(i)) in your plot and mark them by a *.3. Test your program using a cubic functionf(x) =x3with x0 =[ XXXXXXXXXX]. You should find S(x) = f(x) and the error should be zero toround-off error.4. Run you program to approximatef(x) =x∗exp(x) withx0i=i∗0.2,i =0,1,2,..10.a-Hand in a printed copy of you programs, your plots and results withdiscussions.b-Upload your programs to CanvasLet the system Ax = f have n equations and n unknowns with nonzero entriesA(i,i) = a(i) , i=1,2,...,nA(i+1,i) = b(i), i=1,2,...n-1A(i,i+1) = c(i), i=1,2,..,n-1All other entries of the matrix A are zero.The following algorithm solves the system Ax = f using Gaussian eliminationfor tridiagonal systems.

Math 4446 Project 2 Print your results using format long e. Submit a hard copy of a typed report including matlab scripts, results and discussions. Submit a zipped folder containing all you codes in Canvas. Problem I Let the system Ax = f have n equations and n unknowns with nonzero entries A(i,i) = a(i) , i=1,2,...,n A(i+1,i) = b(i), i=1,2,...n-1 A(i,i+1) = c(i), i=1,2,..,n-1 All other entries of the matrix A are zero. The following algorithm solves the system Ax = f using Gaussian elimination for tridiagonal systems. Algorithm: Enter a(1:n), b(1:n-1), c(1:n-1), f(1:n) % Gaussian elimination and forward substitution steps d(1) = a(1) z(1) = f(1) for i=2:n d(i) = a(i) - b(i-1)*c(i-1)/d(i-1) z(i) = f(i) - b(i-1)*z(i-1)/d(i-1) end % %solve Ux =z or Backward substitution step % x(n) = z(n)/d(n) for i=n-1:-1:1 x(i) = (z(i) - c(i)*x(i+1))/d(i) end 1. Write a matlab M file for the previous algorithm in gesolve.m so that gesolve(a,b,c,f) solves Ax = f and returns the solution x. 1 2. Test your program on A x = f, with A = [ 5 2 0 0; 4 13 -4 0; 0 7 15 6; 0 0 12 -16], f = [ 1 2 3 -4]. compare your results with the matlab command A \ f. Note: In case you are not able to get Problem I right, use the matlab command A\f in Problems II and III for partial credit. Problem II 1. Write an M file spline.m such that spline(m(k),m(k+1),p(k), q(k), x0(k), x0(k+1) , x ) computes Sk(x) for x in the interval [x0(k) , x0(k+1)] 2. Write a matlab program in an M file spline_nat.m that computes the natural spline approximation of the set of data points (x0(k) , y0(k) ), k=0,1,..n. as spline_nat(x0,y0) that (a) Accepts x0 and y0 data points. (b) defines a(i), i=1,2,..,n, b(i), i=1,2,..n-1, c(i),i=1,2,..n-1 and the right hand side u(k), k=0,1,2,..n-2. (c) calls gesolve from question I to solve the resulting system. Compute p(i), i=0,...,n-1, and q(i), i=0,..,n-1. (d) calls spline.m to evaluate Sk(x) and plot the spline approximation. On the same plot show the data points x0 and y0 as plot(x0,y0,’*’). 3. Test your program on an example treated in class to make sure that it is working properly. x = [ -2,-1,0,1,2], y = [ 2,1,0,1,2], m =[ 0 , -6/7 , 24/7, -6/7, 0], p = [ 2,8/7, -4/7, 8/7], q =[ 8/7, -4/7 , 8/7, 2 ]. 4. Run the matlab smile.m program that reads each of the attached data files and calls spline_nat several times to plot its natural spline approximation. Plot the spline approximation of all data files on the same figure to have a smiling face. Data files and the m-file ’smile.m’ will be posted in Canvas under Files. Problem III 1. Write an M file dspline.m such that dspline(m(k),m(k+1),p(k), q(k), x0(k), x0(k+1) , x ) computes derivative of Sk(x) for x in the interval [x0(k) , x0(k+1)]. 2 2. Modify your spline_nat.m to spline_clam.m such that spline_clam(x0,y0,fp0,fp1,func,funcprime) (a) accepts x0, y0 and the first derivatives of S(x), S’(x0(0)) = fp0, and S’(x0(n)) = fp1. func is the function to be interpolated, funcprime is the derivative of func. (b) defines a ,b,c and f for clamped spline and use gesolve to solve for m(i), i=1,2,...,n-1. Compute m(0) and m(n). (c) Computes p and q (d) calls spline.m to plot the clamped spline and the exact function on the same figure. (e) calls dspline.m to plot the derivative S’(x) and the derivative on the function. (f) plots the true error, e(x) = S(x) - f(x), as a function of x. On a separate figure plot the error interval by interval to include the points x0(i), e(x0(i)) in your plot and mark them by a *. 3. Test your program using a cubic function f(x) = x 3 with x0 =[ -1 2 5 8]. You should find S(x) = f(x) and the error should be zero to round-off error. 4. Run you program to approximate f(x) = x ∗ exp(x) with x0i = i ∗ 0.2, i =0,1,2,..10. a-Hand in a printed copy of you programs, your plots and results with discussions. b-Upload your programs to Canvas. You must use the names and format described in the project. 3