Matlab Exercises¶
Try the exercises at Quantitative Economics.
Some of the code for these exercises is in the examples
folder in my github repo.
Some of my general purpose reusable code is posted in the shared
folder in my github repo.
Read the Matlab documentation on the following subjects:¶
- Data types: matrices, strings, structures.
- Indexing to access matrix elements.
- Operators:
*,.*,/,./
- String handling: sprintf
- Loops: while, for.
- Functions: input/output arguments, global/local variables.
- Optimization: fzero, fsolve, optimset.
- Graphics: plot.
Getting Started¶
Compute the mean and standard deviation for weighted data
Input: data and weights (matrices)
Output: mean and standard deviation for each column
Compute the cdf for weighted data.
Input: data and weights
Output: cumulative percentiles and their values
Compute points at the percentile distribution for weighted data (e.g. median)
Root finding¶
Write a Matlab program that numerically finds the solution of
\( f\left(x,a\right) =x^{2}-a=0 \) for \(x\geq 0 \)
- Write the deviation function \( f(x) \)
- Plot \( f(x) \) for \( x \in [0,4] \) .
- Find the solution for \( a = 4 \) using
fzero
. - Find the solution for \( a = 4 \) using
fsolve
.
Try other algorithms, such as fminsearch
for fun.
Growth Model¶
Consider a growth model given by the Bellman equation
\(V(k) = \max u(c) + \beta V(f(k) - c) \)
with \(f(k) = k^\alpha\).
Solve the growth model by value function iteration.
Steps:
- Set up a grid for \(k \).
- Start from an arbitrary guess \( V_{0}(k) \).
- Iterate over the Bellman operator until \( V \) converges.
Along the way, plot the value functions to show how they converge to the true one. Like this:
GrowthValueIter
See QuantEcon and my Matlab solution.
Useful Exercises From QuantEcon¶
You should also write test routines for each function.
Draw discrete random numbers
Given: probability of each state
Simulate a Markov Chain
See QuantEcon for the setup and Julia code.
Also compute the stationary distribution
Also try their Exercise 1 (illustrate the central limit theorem).
Approximate an AR1 with a Markov chain from QuantEcon.
Do this at home by simply “translating” the Julia code into Matlab.
You will see that the syntax is very similar.
Simulate random variables to illustrate the Law of Large Numbers and the Central Limit Theorem.
See QuantEcon for the setup and Julia code.