Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Lab-Assignment 2 due 11/30, 2016 Approximation of Functions Recommended Tools: Matlab, Word Staple this page on top of your lab report. Group work done by
Lab-Assignment 2 due 11/30, 2016 Approximation of Functions Recommended Tools: Matlab, Word Staple this page on top of your lab report. Group work done by (list up to 3 names): Lab number Total: Remember that your lab report should be a printed word file, with plots included. Extensive computations can be added on as a handwritten sheet of paper. Make sure you answer questions in full English sentences. Tutorial In this lab we will learn how to use matlab to compute series expansions of functions. To get you started, we will work through the Taylor-Series expansion of ex . Recall that a Taylor's formula says f (x) = f (a) + f 0 (a)(x a) + f 00 (a) (x 2! a)2 + + f (n) (a) (x n! a)n + The Taylor series expansion of f (x) = ex at zero is ex = 1 + x + 1 2 1 x + + xn + 2! n! Note that each term has a coefficient an = n!1 multiplied by a power of x. We can approximate ex by using the first N terms of this series: N X 1 n fN (x) = x n! n=0 Since finding these approximations involves a repetitive calculation, it is convenient to make matlab do that for us. Here's how: Setup First, we create a script. After opening matlab, click \"New\" in the upper left corner. An editor window opens where we will write our code. Definitions The first part of our script should define the variables we are going to work with: x = -2:.01:2; % % f = exp(x); % % FN = zeros(size(x)); % % N = 3; % % Define the range of x to be from -2 to 2, and use increments of .01. Define what we want to approximate, f(x). Note that exp(x) is MATLAB code for e^x. Initialize a variable for the series expansion. To start it will be a list of zeroes of the same size as x. This will be how many terms of the expansion we use. We will start with 3, but we can change it later. Looping Now that we have definitions, the next section of code will calculate first the coefficients for the Taylor expansion, and then the terms of the series. To do this repeatedly, we use a for loop. This command tells us to start with a counter set to 0, perform the steps that follow, and then repeat with the counter set to 1, and then 2, and so on, until the counter reaches the value of N . for n=0:N, a_n = 1/factorial(n); FN = FN + a_n*x.^(n); end % % % % Use n as the counter and have it go from 0 to N. The n-th coefficient in the Taylor series is 1/n!. Build the Taylor polynomial by adding the n-th term. Stop the loop. Observe the dot in front of the exponentiation operator. This is because x is a vector, and hence the exponentiation should apply to each element of x. Plot Finally, make a plot of the function and its approximation. These plotting commands should be familiar from the last lab! plot(x,f,x,FN) % Plots x, f(x), and f_N(x) on the same window. grid on; axis([-2,2,0,10]); xlabel('x from -2 to 2'); ylabel('f and f_N'); title('Taylor Series Expansion of e^x') Now click the run button at the top of the screen, or hit ctrl-alt-r, to run your script! A function f (x) is called periodic with period p if f (x + k p) = f (x) for all k 2 Z. Example 1: The functions sin(x), cos(x), tan(x) are all periodic. The period of sin(x) is 2, as is the period of cos(x). The period of tan(x) is . Here is a picture: tan = tan + 4 4 In signal processing, discontinuous functions arise, like the square wave: Example 2: Consider the piecewise defined function 1 bxc is even f (x) = 1 bxc is odd where bxc is the largest integer which is less than or equal to x. This function is periodic with period 2. Here is a plot: The vertical lines are artifacts and do not belong to the graph of f (x). Example 3: Here is another periodic function (again, the vertical lines are artifacts): The piecewise definition of this function f (x) is f (x) = x 2k if 2k 1 x < 2k + 1, Please indicate the regions on the x-axis where k = for some integer k. 2, 1, 0, 1, 2. Assignment Problem 1 Consider the periodic function f (x) = x 2k, if (2k 1) x < (2k + 1), for some integer k. What is the period of this function? Plot this function in the plot below (your plot should show at least 3 complete periods of the function): Label your axes and indicate scale with tickmarks. Problem 2 You are trying to rewrite a periodic function f (x) as 1 b0 + a1 sin(x) + b1 cos(x) + a2 sin(2x) + b2 cos(2x) + 2 1 1 X X 1 = b0 + an sin(nx) + bn cos(nx) 2 n=1 n=1 f (x) = Your friend Joe just told you that the coefficients in this expansion can be computed using the formulae 1 an = Z f (x) sin(nx)dx, 1 bn = Z f (x) cos(nx)dx. You are asked to try out this method on the periodic function f (x) = x 2k, if (2k 1) x < (2k + 1), for some integer k from Problem 1. Compute the an and the bn according to Joe's formula. Hint 1: Integration by Parts! Hint 2: On the interval [ , ), the function becomes f (x) = x and this is all you need for Joe. Problem 3 Now you are asked to make a plot of the periodic function f (x) and of the approximations fN (x) that you get when you consider N N X X 1 fN (x) = b0 + an sin(nx) + bn cos(nx) 2 n=1 n=1 for N = 1, 2, 3, 4, 5, 6. Make six dierent plots. Each plot should have f (x) overlayed with the approximation fN (x) for a given value of N. Hint: To define f (x) as a periodic function from 2 to 2, we can use the following code (assuming x is defined first and ranges from 2 to 2): f = ppval(mkpp([-2*pi, -pi, pi, 2*pi],[1/pi 0; 1/pi -1; 1/pi -1]) , x); Hint: Refer to the tutorial above! Problem 4 Describe the behavior of the functions plotted in Problem 3 as N increases. Would you be able to achieve the same eect with Taylor series? Why or why not? Matlab Code Template %{ This program approximates the periodic funcion with piecewise definition x/pi-2k according the formula given to us by our friend Joe TODO: Add N find a_n, b_n and enter them in the for loop add a statement to increment the sum FN add axis, x and y labels and a title %} % This will be the number of iterations of our for loop: N = % This is the interval we will calculate, no need to touch this: x = -2*pi:.01:2*pi; FN = zeros(size(x)); for n = 1:N, a_n = %TODO: calculate a_n and enter it here b_n = %TODO: calculate b_n and enter it here FN = %TODO add the next iteration of the approximation. % Watch out for the dot operator! end % Don't touch this: fx = ppval(mkpp([-2*pi, -pi, pi, 2*pi], [1/pi 0; 1/pi -1; 1/pi -1]), x); plot(x, fx, x, FN) grid on; axis(); %TODO make appropriate endpoints for the axes xlabel(''); %TODO make a label for the x-axis ylabel(''); %TODO make a label for the y-axis title(''); %TODO make a title
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started