Weizmann 2016 - Introduction to Matlab & Data Analysis Exercise #5 Tutor in charge of this HW: Ayelet Sarel, e-mail for Questions: ayelet.sarel@weizmann.ac.il The mail subject should be: 'Matlab intro - exercise 5'. Attach only the script file to your mail. HW instructions: Don't forget to write both names in the header. Print to the screen only when required. All the questions should be answered in the proper location in the ‘m’ file (as comments). Printing should be done only with the disp() function. When the print-out contains both text and numbers use disp(sprintf) All figures (or subfigures) must contain a title, x and y labels. If there is more than one data set in the same figure - add a legend. Images display must contain a colorbar Before each question X.Y write the line: “%% Question X.Y”. Below there's an example of how the *.m file should look like. 1 Weizmann 2016 - Introduction to Matlab & Data Analysis function [] = hw5_XXX_YYY () % HW5 % Avi Bitter XXX % Eliko YYY %% question 1.1 matlab code…. %% question 1.2 matlab code…. ….. The first row in the script should be (replace XX.. and YY.. of course ): function [] = hw5_XXX_YYY () This function will be our primary function, it has the same name as the script, and executing the script is done by calling this function (from the Matlab command for instance). Question 1 1. Create a nested function named factorial() which calculates the factorial of the number n and prints its value. The factorial is defined as the product 𝑛! = ∏𝑛𝑘=1 𝑘 You should program this function using recursion only - without any loops, matrices or other Matlab functions. The nested function should be inside the primary function. 2. Call the function with n=7, and print the result. Question 2 1. Create a nested function named calcTailorExp() which gets as input a number x and the upper limit N (where n=0,…,N), and calculates the following series: 𝑁 𝑥1 𝑥 2 𝑥 3 𝑥𝑛 1+ + + +⋯= ∑ 1! 2! 3! 𝑛! 𝑛=0 You should program this function using recursion without any loops, matrices or other Matlab functions. You may want to use the factorial function from the previous section. 2. Assume x=1,what is the minimal value of N that gives you a good approximation (with an error of less than 0.0005) for an exponent 𝑒 1 ? Question 3 – Fibonacci series 1. Create a nested function called recursiveFibonacci() that gets a number n and returns the n'th Fibonacci number ( Fn Fn 1 Fn 2 ), using nothing but recursion. 2. Create a nested function iterativeFibonacci() that gets a number n and returns the n'th Fibonacci number, with a loop and no more than 3 simple variables (no matrices are allowed). 3. Execute the recursiveFibonacci(12) and print the result , measure the running time (add the line tic; before , and the line toc; after) the running time will be automatically displayed. 4. Execute the iterativeFibonacci(12) and print the result , measure the running time . 5. Which function is faster? Why is that? 2 Weizmann 2016 - Introduction to Matlab & Data Analysis Question 4 – Anonymous Function Ask the user for an equation to integrate. Construct function handle from the input string. You can assume that the input is legal, for example: 2*x-3. Ask the user to enter the function limits, separated by a comma, and create a numbers vector of size 2 from the input. You can assume that the input is legal, for example: -2,2. You’ll need to parse the text, you can use the function regexp(), and also to convert the text to numbers with str2double() 1. Call the function myIntegrator (which you will build later) with the function handle and the limits vector. This line will be the last line in the primary function. All the following questions will be in the sub-function. 2. Build the sub-function myIntegrator() which gets as input a function handle and limits. The subfunction is in the same file as the primary function, but its not inside the function itself, like nested functions. 3. Plot the function between the specified limits (there is a special Matlab function that does that). Expand the plot x-limits by 1 from each direction, and Add red circles with size 10 in the edges of the function. 4. Add a title "The integral is T". Where T is the Matlab numeric integration of the function in the specified limits, with a precision of exactly 2 digits after the dot – use quad() to perform the numerical integration. Congratulations, you are now familiar with Matlab's different function types, and recursion. Numerical integration might be also very useful later on. 3