Skip to content

Instantly share code, notes, and snippets.

@olivierverdier
Created December 7, 2015 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olivierverdier/41a62bf82e7089d7296c to your computer and use it in GitHub Desktop.
Save olivierverdier/41a62bf82e7089d7296c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise for the course [Python for MATLAB users](http://sese.nu/python-for-matlab-users/), by Olivier Verdier"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Using matplotlib backend: MacOSX\n",
"Populating the interactive namespace from numpy and matplotlib\n"
]
}
],
"source": [
"%pylab\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Check out the formula for a [companion matrix on wikipedia](http://en.wikipedia.org/wiki/Companion_matrix).\n",
"Define a function `companion` which accepts a vector in argument, and returns the corresponding companion matrix.\n",
"You can use the command `diag` for that.\n",
"The resulting matrix should be of complex type."
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"diag?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"ones?"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def companion(coefficients):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"C = companion(ones(3))\n",
"assert(C.dtype == complex)\n",
"assert(len(C) == 3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fix a given size, say `size = 20`, and create a vector of length 20 with random, normally distributed, complex numbers. Use the `randn` function for that, and combine two random real vectors to get a random complex vector."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"randn?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, fix a standard deviation, say `sigma = 1./10`, and use the random complex coefficients obtained above, multiplied by sigma, in the `companion` function. Use `eigvals` to compute the eigenvalues, and plot them on the complex plane. You can use the command `axis('equal')` to make sure the plot has the same dimensions in x and y."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, repeat that, say 200 times. Plot all the eigenvalues on the same figure. What do you observe? What happens when you change the standard deviation `sigma`?"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"size = 20\n",
"for i in range(200):\n",
" pass\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
}
],
"metadata": {
"gist_id": "afafc406fdd481686a86",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment