Question: Do not answer problem 7, it is only here for reference Continuation of problem already given code, just modify it %%%%%%%%%%%%%%%%%%%%% compute spline coefficients here

 Do not answer problem 7, it is only here for reference

Do not answer problem 7, it is only here for reference

Continuation of problem already given code, just modify it %%%%%%%%%%%%%%%%%%%%% compute spline

coefficients here %%%%%%%%%%%%%%% format long e %compute a vector of 100 evenly-spaced

Continuation of problem

points between 0 and 2pi, including the endpoints x = %evaluate vector

already given code, just modify it

%%%%%%%%%%%%%%%%%%%%% compute spline coefficients here %%%%%%%%%%%%%%%

format long e

%compute a vector of 100 evenly-spaced points between 0 and 2pi, including the endpoints

x =

%evaluate vector of "exact" values of sin at above x values; use MATLAB's built-in sin function

y =

%compute the values in vector b here

%compute vector of values in vector a here

%compute vector of values in c here

%%%%%%%%%%%%%%%%%%call sine function here - don't modify%%%%%%%%%%%%%%%%%

X1 = 250;

Y1 = sine(X1,a,b,c)

X2 = -100;

Y2 = sine(X2,a,b,c)

X3 = 10*rand(1)-100;

Y3 = sine(X3,a,b,c)

X4 = 10*rand(1)+100;

Y4 = sine(X4,a,b,c)

%%%%%%%%%%%%%%%%%%%%%plot spline - don't modify%%%%%%%%%%%%%%%%%%%%%%%%%

%number of points on which to plot. n = number of nodes

nplot = (n-1)*19+1;

xplot = zeros(nplot,1);

yplot = zeros(nplot,1);

%spacing between plot points

nspace = (x(n)-x(1))/(nplot-1);

k = 0;

for i = 1:n-1

for j = 1:19

k = k+1;

xplot(k) = x(i) + (j-1)*nspace;

yplot(k) = a(i) + b(i)*(xplot(k) - x(i)) + c(i)*(xplot(k) - x(i))^2;

end

end

xplot(nplot) = x(n);

yplot(nplot) = a(i) + b(i)*(x(n) - x(n-1)) + c(i)*(x(n) - x(n-1))^2;

plot(xplot,yplot)

figure

abserr = abs(yplot - sin(xplot));

plot(xplot,abserr)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%% Compute your sine function here

function y = sine( X, a, b, c )

%input

%x - where to evaluate sin function

%a, b, and c are vectors containing quadratic spline coefficients

end

%%%%%%%%%%%% forward substitution function

function [y] = forsub(L,b)

%Forward-substitution

%accepts an nX1 vector b, an nXn lower triangular matrix L

%generates an nX1 solution vector y

n = size(b,1);

y = zeros(n,1);

y(1) = b(1);

for i = 2:n

y(i) = b(i);

for j = 1:i-1

y(i) = y(i) - L(i,j)*y(j);

end

end

end

2) In this exercise, you will be writing your own function, sine, which will use a quadratic spline to compute approximations for the sine function at any real number. You are to compute the quadratic spline S(x) defined in Theoretical Problem #7. Make sure that any computed vectors are row vectors. 7) A quadratic spline S(x) can be defined in the following way, using evenly-spaced nodes X, X., X., where Xx4 xe = 8 for k = 1,2,...,1-1, and whose derivative is specified at Xi. The numbers y...), are the corresponding function values to be interpolated. On each interval [xx] S(x) = S(x) = a + (x x)+c,(x - x)"; k = 1,2,...,1-1, whose coefficients are computed by the following scheme. After applying the interpolation conditions, one obtains ayik1.2.....-1 (1) ( and Ye: - y CA k = 1.2.....11 = , (2) 8 After applying the smoothness conditions, one obtains be+bul-20...-); * = 1,2,.,12 (3) = - If S'(x)=v, (derivative is specified to be vj), then one obtains b = (4). Use Eqs, (3) and (4) to set up a linear system to solve for vector that contains the coefficients b.by ....... Write down an example system for n=6 in matrix-vector form. (The right-hand side will be in terms of v, 8, and yY... ys) Also, use this to find det(A) for the coefficient matrix, 4, for any number of nodes, n, to determine if there is a unique solution, i.e, a unique quadratic spline, for any n. a) In the first part of the MATLAB script below, where indicated, add code that uses your result from Theoretical Problem #7, along with Eqs. (1) and (2) in the same problem, to compute all coefficients , bp, and C, for k = 1,2,..,n -1, using the data y, Y2 ..., y, and von evenly-space nodes x, X.2 ...,Xn, specifically for n = 100 from 0 to 21 , including the endpoints. To solve the linear system, you may call the function, forsub(), given at the bottom of the script, or you could use a loop. The y-values will be defined by y, = sin(x)); i =1,2,...,100. (In other words, your spline will approximate one period of the cosine function.) b) You will note that the second part of the script below, calls the function y = sine (X, a,b,c) Where indicated, you are to write this function at the bottom of the script below. X is the value where the quadratic spline is to be evaluated to approximate sin(x), and a, b, and care the vectors that define the spline from part (a). i) This function should accept any value of X - not just those in (0,21]. ii) In your function, computing the remainder of X/(27) may be useful (the built-in MATLAB functions rem and mod compute remainders). This will help the code locate the corresponding x-value in (0,21]. Also, you might use the fact that sin(-x) = -sin x to handle negative values of X. iii) Your function, sine, will then need to locate which function in the spline to evaluate. You could use a loop or you could utilize the mod() function. In the former case, you may need to re-introduce the vector of 100 evenly-spaced X-values on [0,21]. 2) In this exercise, you will be writing your own function, sine, which will use a quadratic spline to compute approximations for the sine function at any real number. You are to compute the quadratic spline S(x) defined in Theoretical Problem #7. Make sure that any computed vectors are row vectors. 7) A quadratic spline S(x) can be defined in the following way, using evenly-spaced nodes X, X., X., where Xx4 xe = 8 for k = 1,2,...,1-1, and whose derivative is specified at Xi. The numbers y...), are the corresponding function values to be interpolated. On each interval [xx] S(x) = S(x) = a + (x x)+c,(x - x)"; k = 1,2,...,1-1, whose coefficients are computed by the following scheme. After applying the interpolation conditions, one obtains ayik1.2.....-1 (1) ( and Ye: - y CA k = 1.2.....11 = , (2) 8 After applying the smoothness conditions, one obtains be+bul-20...-); * = 1,2,.,12 (3) = - If S'(x)=v, (derivative is specified to be vj), then one obtains b = (4). Use Eqs, (3) and (4) to set up a linear system to solve for vector that contains the coefficients b.by ....... Write down an example system for n=6 in matrix-vector form. (The right-hand side will be in terms of v, 8, and yY... ys) Also, use this to find det(A) for the coefficient matrix, 4, for any number of nodes, n, to determine if there is a unique solution, i.e, a unique quadratic spline, for any n. a) In the first part of the MATLAB script below, where indicated, add code that uses your result from Theoretical Problem #7, along with Eqs. (1) and (2) in the same problem, to compute all coefficients , bp, and C, for k = 1,2,..,n -1, using the data y, Y2 ..., y, and von evenly-space nodes x, X.2 ...,Xn, specifically for n = 100 from 0 to 21 , including the endpoints. To solve the linear system, you may call the function, forsub(), given at the bottom of the script, or you could use a loop. The y-values will be defined by y, = sin(x)); i =1,2,...,100. (In other words, your spline will approximate one period of the cosine function.) b) You will note that the second part of the script below, calls the function y = sine (X, a,b,c) Where indicated, you are to write this function at the bottom of the script below. X is the value where the quadratic spline is to be evaluated to approximate sin(x), and a, b, and care the vectors that define the spline from part (a). i) This function should accept any value of X - not just those in (0,21]. ii) In your function, computing the remainder of X/(27) may be useful (the built-in MATLAB functions rem and mod compute remainders). This will help the code locate the corresponding x-value in (0,21]. Also, you might use the fact that sin(-x) = -sin x to handle negative values of X. iii) Your function, sine, will then need to locate which function in the spline to evaluate. You could use a loop or you could utilize the mod() function. In the former case, you may need to re-introduce the vector of 100 evenly-spaced X-values on [0,21]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!