Exercise #2

advertisement
Weizmann 2012-2013
Introduction to Matlab & Data Analysis
Exercise #5
E-mail for questions: maya.geva@weizmann.ac.il
HW instructions – please read carefully before doing the task:

If you want to learn about a new matlab function – type help function or doc function.
Alternatively – search in mathwork website.

Please name your script "hw5_XXXXXXXXXX_YYYYYYY.m". When XXX and
YYY are the ID's of the students. There is no need to submit twice the same file.

Don't forget to write both names in the header of the ex.

Please submit your solution to the following e-mail address:
maya.geva@weizmann.ac.il.

The mail should contain a single m file only, and the words 'matlab intro' and
exercise number in its subject.

Print to the screen only when asked to. All the questions should be answered in the
proper location in the ‘m’ file as documentation.

Displaying to the screen be done only through disp(), if the string contains both text
and numbers use disp(sprintf)

All figures (or subfigures) must contain title, x and y labels, and if there is more than
one data sets in the same figure, add a legend.

Visualization images must contain a colorbar

Before each question X.Y write the line: “%%Question X.Y”. Please look on the
example below how the m file should look.
Weizmann 2012-2013
Introduction to Matlab & Data Analysis
function [] = hw5_XXXXXXX_YYYYYY()
%HW5
% Avi Bitter XXXXXXXXXX %
% Eliko YYYYYYYYYY%
%% question 1.1
%The answer is 42
matlab code….
%% question 1.2
matlab code….
…..
The first row in the script should be (replace XX.. and YY.. of course ):
function [] = hw5_XXXXXXX_YYYYYY()
This function will be our primary function, it has the same name as the script, and executing
the script is executing this function.
Question 1
1. Create a nested function named countBackwards() which gets as input a
number n, and prints:
n
n-1
n-2
…
2
1
Without any loops or matrices or other matlab functions, just simple recursion.
The last number to be printed is 1.
The nested function should be inside the primary function.
2. Call the function with n=10;
Weizmann 2012-2013
Introduction to Matlab & Data Analysis
Question 2
1. Create a nested function named printTriangle() which gets as input a
number n, and prints a triangle with the following structure:
1
12
123
…………
1 2 3 ………..n
………….
123
12
1
For example if n=3 we will get
1
12
123
12
1
Again we will use only simple recursion and not any loops matrices or other
matlab functions. (hint: the nested function may contain its own nested
functions)
2. Call the function with n=5;
Question 3 – Fibonacci series
1. Create a nested function recoursiveFibonacci() 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 recoursiveFibonacci(15) 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(15) and print the result , measure the running
time .
5. Which function is faster? Why is that?
Weizmann 2012-2013
Introduction to Matlab & Data Analysis
Question 4 – Anonymous Function
1. 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 following questions will be in the subfunction.
2. Build the subfunction 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". when T is matlab numerically 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, now you master matlab's different function types, and
recursion. Numerical integration might be also very useful later on.
Download