Getting started with Matlab
A practical introduction to Matlab
Note: Matlab is designed for numerical computation. What I shall describe below is a relatively advanced feature in Matlab. If you don't know Matlab, visit the links above to familiarize yourself with the basic of Matlab first. Otherwise, you may be confused.
You need to make sure you have the symbolic toolbox before you can try the following. This is available in the Matlab installation in MSU.
Suppose we are given the data density
p(x) \propto exp( -(x-u)^2 / s^2 ) for x in (-\infty, \infty).
What is the analytic expression for this density?
You probably have noticed that p(x) is a Gaussian pdf, and the proportionality constant can be looked up in the textbook. Let's try to compute this in Matlab instead.
First, we need some declarations.
>> syms x u real >> syms s positive
This declares both x and u as symbolic real numbers, and s as a symbolic positive real number. Note that normal variables in Matlab do not require declaration. Only symbolic ones do.
Next, we created the symbolic variable "f" to represent to the above density.
>> f = exp( -(x-u)^2 / s^2 ) f = exp((x-u)^2/s^2)
We now need to integrate the expression with respect to x. Since x ranges from negative infinity to positive infinity, the integration limit should be -inf, +inf.
g = int(f, x, -inf, inf) g = s*pi^(1/2)
So, the normalization for the pdf is 1/(s*pi^(1/2)). In other words, the pdf for x should be
p(x) = 1/(s*pi^(1/2)) * exp( -(x-u)^2 / s^2 ) for x in (-\infty, \infty).
We can check this is indeed a valid pdf because it integrates to one:
>> pdf = 1/(s*sym(pi)^(1/2)) * exp( -(x-u)^2 / s^2 ) ; >> int(pdf, x, -inf, inf) ans = 1