1. Write a function array_product to compute the product of the elements of an array v a for loop. The first line of the func3. Write a function plot_function_and_taylor to plot a function f(x) along with its first n+1 Taylor polynomials (i.e., PA(a)

1. Write a function array_product to compute the product of the elements of an array v a for loop. The first line of the function, and explanatory comments have been using provided below array_product(v) Product of elements in an array function P = %array_product v (n) % array_product (v) returns v(1) * v(2) where n is length(v). % Create a script file Quiz3.m. Test your array_product function for three different arrays that you create, and confirm it recovers the same answer as the MATLAB built- in function prod. Display to the Command Window only the results of your tests. 2. (a) Write a function exp_taylor that approximates e2 using the Taylor polynomial e2 1 + 2! + n! … 3! a for loop. The first line of the function, and explanatory comments have using been provided below. function e_to_x exp_taylor(x, n) = %exp_taylor Approximate e^x by Taylor polynomial x^n/n! exp_taylor(x, n) returns 1 + x + x^2/2! + e_to_x = For this task, do not use the taylor sheet 3 or derivative functions supplied with Work- (b) In Quiz3.m, use your exp_taylor function to approximate e using n = 3, n = 10 and n 100. For this task, only display to the Command Window the approxi- mations for each value of n (c) For what value of n does n! first overflow in MATLAB (use trial and error)? Insert your response as a comment in your Quiz3.m script file and confirm your answer by displaying to the Command Window the value of (n – 1)! and n! for the value of n you identified. What effect does this overflow appear to have on the result returned by exp_taylor(1, n) when n is large? Explain your findings and insert your response as a comment in your Quiz3.m script file. 3. Write a function plot_function_and_taylor to plot a function f(x) along with its first n+1 Taylor polynomials (i.e., PA(a), i = 0,1, …,n, where P(r) is the Taylor polynomial of degree i) use different colours for each curve, but does not have to include a feel free to include one if you wish). The first line of the function, and explanatory comments have been provided below on the same figure. Your plot should include appropriate axis labels and legend (however, 1 function plot_function_and_taylor(f, a, b, x0, n) %plot_function_and_taylor Plot a function and its Taylor polynomials % plot_function_and_taylor (f, a, b, x0, n) plots the function f on the % interval [a, b], along with its Taylor polynomial approximations % centred on x0 up to degree n Hint: Use the taylor (and derivative) functions supplied with Worksheet 3 and note that this function is just automating what you did “by hand” in Section 3.4.1 of Work- sheet 3 4. In your Quiz3.m script file, test your function from Task 3 using f(x) = cos(x), a = 0, b T/2, ToT/4 and n 2.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *