any help please! Write a MATLAB program to compute the derivative of f (x) = sin
ID: 2985126 • Letter: A
Question
any help please!
Write a MATLAB program to compute the derivative of f (x) = sin x at x = pi/3 using the forward, backward, and central difference formula. Use the step size h = 10-n for n = 1, ... ,5. Make a table for each method which includes the values of derivatives, errors, and ratios of consecutive errors. (Refer to Table 5.12 on page 238.) Discuss the behavior of errors as compared with the theoretical results. Next, compute the derivative using smaller step size h = 10-n for n = 6, ... , 14. Recall that numerical instabilities occur for hExplanation / Answer
(a) y = sin(x) n = 1 : 100; c = 2*n-1; s = (-1).^(n-1); f = factorial(c); for i = 1 : length(x) y(i) = sum(s .* x(i).^c ./ f); end We are just coding the formula... We calculate the three parts of the vectors: the alternating sign (s), the part that contains xi (within the loop), and the factorial part (f). Finally we multiply, divide and sum to produce every response corresponding to each element in the input vector. We have to iterate along all of the elements of the input vector to obtain all of the elements of the output vector. Again, we’re using n = 100, which seems to be a good enough number for our experiments... We can fastly test the code, like this x = 0 : .1 : 10*pi; plot(x,sin(x),'b', x,sin(x),'ro') We use horizontal values from 0 up to 10π, and we’re comparing the regular sine function (blue-solid line) against our experiment (sine series in red circles). The result is as follows The approximation seems to work fine. Of course, we know that we need only values (responses) from 0 to π/2 and that we can modify our algorithm to repeat those responses or change the sign as necessary, but we want also to simplify the code.
(b)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.