In this exorcise we will experiment with numerical different
In this exorcise, we will experiment with numerical differentiation of data Evaluate the derivative of y at each of the given t values with the following second-order finite difference schemes y(t + h) - y(t - h)/2h = y\' (t) + O (h^2) -y(t + 2h) + 4y (t + h) - 3y (t)/2h = y\' (t) + O (h^2) y(t - 2h) - 4y (t - h) + 3y(t)/2h = y\' (t) + O (h^2) TASK: Save the resulting values [y\'(0), y\' (1), ..., y\' (5)]^T, as a column vector in the file A01.dat Fit a polynomial of degree 3 by least-squares to the data, using poly fit. TASK: Save the values for the cubic polynomial, p, at each of the given t values [p(0), p(1), ..., p(5)]^T, as a column vector, in the file A02.dat Read the help on the MATLAB command polyder. Use this command to obtain p\' be the derivative of p. TASK: Save the values for the polynomial, p\', at each of the given t values [p\'(0), p\'(1), ..., p\' (5)]^T, as a column vector, in the file A03.dat Let q be the interpolating polynomial of degree 5, interpolating the given data. TASK: Save the values for the derivative polynomial, q\', at each of the given t values [q\' (0), q\' (1), ..., q\' (5)]^T, as a column vector, in the file A04.dat
Solution
Matlab Code:
1) Fitting the polynomial of degree 3
t= (0:1:5);
y=[1,2.7,5.8,6.6,7.5,9.9];
p = polyfit(t,y,3); %%p contains fitted polynomial
polyp=polyval(p,t); %%polyp consists of p evaluated at each point of t
2) Now that p is obtained in step 1 let us do the differentiation using polyder.
Hers is code for it. (Note : Continue the code on same workspace without clearing it, as it requires value of p)
derivative_p= polyder(p); %%derivative of polynmial p
derivative_pval= polyval(derivative_p , t); %% value of derivative of p at t;
Now ,
q= polyfit(t,y,5);
derivative_q= polyder(q);
derivative_qval= polyval(derivative_q,t); %% value of derivative of q at t;
