# 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.

## Getting Started¶

1. Compute the mean and standard deviation for weighted data

Input: data and weights (matrices)

Output: mean and standard deviation for each column

My solution

2. Compute the cdf for weighted data.

Input: data and weights

Output: cumulative percentiles and their values

My solution

3. Compute points at the percentile distribution for weighted data (e.g. median)

My solution

## 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:

1. Set up a grid for $$k$$.
2. Start from an arbitrary guess $$V_{0}(k)$$.
3. 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.

1. Draw discrete random numbers

Given: probability of each state

My solution

2. Simulate a Markov Chain

See QuantEcon for the setup and Julia code.

Also compute the stationary distribution

My solution

Also try their Exercise 1 (illustrate the central limit theorem).

3. 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.

My solution

4. Simulate random variables to illustrate the Law of Large Numbers and the Central Limit Theorem.

See QuantEcon for the setup and Julia code.

My solution