Sunteți pe pagina 1din 2

2172–Numerical Methods and Matlab Andre Silva, Nova SBE

Problem Set 4

Due date: November 29


Turn in the results of questions 5 and 6. Upload your answers in files .m through Moodle. Please,
add comments to your files so that you explain what you are doing in each step. You can turn in
your answers individually or in groups of two. Only one member of the group need to upload the
files.

1. Create a function fn.m that calculates y = xa . Your function should be able to receive a vector
x and a parameter a. Using the function, plot the values of the function for 0 ≤ x ≤ 2 for
a = 0.5, 1, 2. Add a legend for each value of a, a title, and vertical and horizontal labels to
your plot.
2. Create a script that prompts the user for a principal invested P , the interest rate r and the
number of years of investment n and returns F = P (1 + r)n . Your script must call a function
that calculates F given the values of P , r and n.
3. Consider the function (√
x−a+b if x ≥ a,
f (x) = √ (1)
− a − x + b if x < a.
Let a = 7/4 and b = 1/32. Obtain the zeros of this function with Newton’s method, the bisection
method, and the secant method. Use the initial limits x1 = 1 and x2 = 2 for all methods. For
Newton’s method, start with x1 = 2. Compare the number of iterations for each method. What
makes the number of iterations high for Newton’s method? Hints: (1) You will have to define
a function that follows (1). (2) The root of f can be obtained analytically, use the analytical
value to guide your solution.
4. Consider the file FamaFrench25Portfolios.txt. It contains the monthly returns of the 25
Fama-French portfolios sorted by size (market capitalization) and book-to-market ratio. The
returns are in percent per month. The file contains a column for the year and month and 25
other columns for the return of each portfolio. Column 1 is for the smallest size and smallest
book-to-market (portfolio 11). Column 2 has the smallest size and the second group of book-to-
market (portolio 12). Column 25 has the largest size and the largest book-to-market (portfolio
55). Create a script that does the following:
1. Loads the file FamaFrench25Portfolios.txt.
2. Calculates the mean return and the standard deviation of returns for each portfolio.
3. Plots a graph with the 25 mean returns and standard deviations (standard deviaionts in
the horizontal axis). Use a dot for each combination or return and standard deviation (do
not connect the points with a line).
4. Prints to the screen the minimum mean return among the 25 portfolios and its standard
deviation. And similarly for the maximum mean return.
Your results should be shown in percent per year. To convert monthly
√ mean returns to annual,
do µannual = 12µmonthly . For the standard deviation, σannual = 12σmonthly .

1
5. Practice working with polynomials, estimation, and graphs. Prepare a script that does the
following:
1. Create a vector x with 500 points, from x = 0 to x = 6. Use linspace.
2. Create a vector y that follows the polynomial x2 − 6x + 8. Use polyval(p,x). Define p in
accordance with the polynominal.
3. Add a random error to y. Use y = y + randn(1,length(x)).
4. Estimate the polynomial using the values generated for x and y. Use polyfit.
5. Create another vector for x, with the same limits, now with 1000 points. Create values for
y using the new x and the estimated polynomial. Use polyval.
6. Plot a graph with the points used to estimate the polynomial and a line with the estimated
polynomial.
Upload to Moodle a script polypractice.m with your solution.
6. An important problem in numerical methods is to sort a vector in ascending or descending
order. Let us use a a popular method of sorting, bubble sort, to sort a vector in ascending order.
Bubble sort sorts a vector making a comparison of numbers one by one. The algorithm compares
adjacent numbers in the vector. If the first number is greater than the second number, then
the algorithm switches the numbers. The algorithm repeats this procedure until it compares
all numbers in the vector and doesn’t make a switch. If it happens, then the vector is in the
correct order.
Let use say that you start with a vector (4, 1, 3, 1). From the first to the last comparison, the
algorithm transforms the vector into (1, 4, 3, 1), (1, 3, 4, 1), and (1, 3, 1, 4). As there were switches,
the algorithm starts again from the first number. It then does (1, 1, 3, 4). The algorithm starts
again from the first number and stops as no switch is necessary. In this example, there were
four switchs and three loops.
Write a Matlab function that receives a vector A as input and returns the vector sorted in
ascending order.

S-ar putea să vă placă și