New Address

Since I’m now a graduate student at Purdue, I’ve moved my blog over there. These posts will stick around, but all my new posts will be over there, here’s the link: http://web.ics.purdue.edu/~smit1447/blog/

Posted in Uncategorized | Leave a comment

Setting Up Eclipse for Python

I’ve started using Eclipse for a lot of my programming needs these days, at home and at work, but it requires a bit of additional setup to make it a nice alternative to MATLAB. Most of these steps are unnecessary if you use Python(x,y), but on Linux and Mac, you’ll have to set it up manually, so here’s how I did it on ubuntu:

The first thing you need to do is make sure you install python and all its proper packages, as well as sun java and Eclipse. On a Mac, I’m not sure what all this would entail, but on ubuntu, it’s pretty simple. First, you need to enable the “partners” repository, if you haven’t already (software center -> edit -> software sources then Other Software -> check Canonical Partners). Now open up a terminal to install the software:

sudo apt-get update
sudo apt-get install sun-java6-jdk eclipse ipython python-numpy python-scipy python-matplotlib python-qt4-dev pyqt4-dev-tools
sudo update-java-alternatives -s java-6-sun

Next, you’ll need to edit a file and add some code to the top:

sudo -b gedit /etc/jvm

Add this to the top:

/usr/lib/jvm/java-6-sun

Now, to get the nice Qt Designer integrated into Eclipse, you’ll need to download that and extract it separately (Note: If you don’t care about having it integrated in Eclipse, you can just install Qt Designer separately, you can still use it just fine. Both in Eclipse and in Qt Designer, it will create an xml file that we will load into Python, you can install it with sudo apt-get install qt4-designer).

Download the file here. Then in a terminal, find where your eclipse/plugins directory is. In ubuntu, this is /usr/lib. Change to that directory and unpack the file you downloaded:

cd /usr/lib
sudo tar xzf /path/to/download/qt***

NOTE: you’ll probably want to use tab-completion to get the whole name of the file, I don’t want to type that whole thing out!

Now, to get Eclipse set up for Python, you’ll need to install the pydev package inside of Eclipse, so go ahead and start up Eclipse. Once inside, go to Help -> Install New Software

From there, enter the address http://pydev.org/updates, then wait for the package to load, select Pydev and continue with the installation.

Accept the license agreement and such and finish the installation, when you’re done, re-start Eclipse and we’ll add a few more customisations.

Once Eclipse has re-started, create a new PyDev Project. (If you don’t see pydev as an option, choose Other and look for PyDev -> PyDev Project).

A new window will open. Name your project something like “test” and then click the link to configure an interpreter (the link might be hard to see, so I highlighted it in Yellow).

Next you’ll want to click AutoConfig. If your python installed properly, this should load everything you need.

Press OK and then Finish and you should have a blank project all ready to go.

Now let’s make sure everything’s working the way it should. right click on the src folder and add a new blank file.

Name it something simple (test.py) and add some simple things to make sure it can do what we want:

import numpy as np
from matplotlib import pyplot as plt
x = np.linspace(0,100,500)
y = np.sin(x)
plt.plot(x,y)
plt.show()

Instead of copy and paste, try typing this into Eclipse as you go and you should experience some of Eclipse’s nicer code-completion features.

If you don’t see anything come up after you type “np.” you can hit ctl+space and the autocomplete list should appear. Also, if you hover your mouse over the documentation you can scroll to see the rest of it. You could also, if you wanted to, hit tab and scroll through the documentation with your keyboard. To make sure it runs, hit the green “Run as” button.

Choose “Python Run” and click ok. (In the future, this project will always default to that option every time you click run). You should see a nice sine graph pop up, and you’re good to go and ready to make some more advanced configurations to your setup.

First, let’s make sure the integrated Qt Designer is working properly. Add a new Qt Form to your project by either right-clicking on the src Folder, choosing New then Other, or you can hit Ctl + N. Then choose Qt Designer -> Qt Designer Form.

You can now choose from one of several base widget configurations, note that you must give your form a name ending in ‘.ui’.

Now to be able to properly use the Qt Designer, you need to add several boxes to your layout. This can get cluttered if you leave them in all the time, so I saved an alternate configuration called PyQt that I can switch to every time I’m editing .ui files. First of all, go to Window -> Show View -> Other.

Next we need to add all the Qt options, and place them wherever we see fit. Unfortunately you need to do these one by one, but once you have added them all you can move them around.

After I like to have a good long view for the Qt C++ Widgets and for the Qt C++ Properties (these are the most important ones anyway), the others I like to have down by the console. You can’t use the Python Outline in Qt Designer mode, so I like to close it. Here’s what my setting looks like:

Now, to save this as its own view so we can switch between Qt Designer mode and PyDev mode, click Window -> Save Perspective As and name it something like Qt Designer or PyQt or whatever. Now every time you want to design a gui, add a new .ui file, then switch to that perspective (you can have perspective buttons on the top right by clicking the icon that looks like a window with a star on it)

After you click other, you’ll be brought to a list and you can add PyDev and Qt Designer (or whatever you named your Qt Designer view as).

To play around in Qt Designer, drag some widgets onto your gui. You can edit their properties in the Qt Object Properties box. I’ll go into how to use these in more detail another time, you can also Google Qt Designer tutorials, as they’re pretty much the same.

Next, to be able to use our ui files we need to convert them from .ui files to .py files. (This isn’t always necessary, you can also load them straight in as .ui files, but I haven’t learned how to do that yet, and it’s always good to have things set up so you can do it both ways. For those interested, you can load dynamically using the function PyQt4.ui.loadUi or PyQt4.ui.loadUiType).

First of all, to do this nicely we’re going to have to create a custom script. Paste the following code into a file and name it something like pyuicEclipse:

#!/bin/bash
pyUICCommand="/usr/bin/pyuic4"
x=$1
f=`basename $x`
d=`dirname $x`
fNoUI="`echo $f | sed 's/\.ui$//'`"
$pyUICCommand -o ${d}/${fNoUI}.py $x

There is a command called pyuic4 which will convert your gui code into Python, using the variables Eclipse can give us, we need to rename it from “file.ui” to “file.py”, which is what this script does, as well as run pyuic4. After you’ve saved this script, make it executable (chmod +x /path/to/pyuicEclipse) and we can set up Eclipse to run it.

Back in Eclipse, configure an External Tool by clicking the green run button with a little red toolbox underneath it, then click External Tools Configurations.

Create a “New Launch Configuration” by clicking the button that looks like a new page icon:

Now you’ll want to do the following:

  • Enter PyUIC or something similar for the name
  • Under Main -> Location enter the path to your script
  • Under Main -> Arguments enter ${resource_loc} (this is an Eclipse variable that gives the full path to your .ui file to your script)
  • Under Refresh check the box to Refresh Resources Upon Completion (this means that Eclipse will look for new files after your script has run, which will let you see the new file it created)
  • Under Build uncheck Build Before Launch, we don’t need to compile anything, so save some time and uncheck this
  • Last but not least, under the Common tab check the Display in Favorites menu box, this will let us see this custom command easily in Eclipse

Last but not least, it’s nice to have a few custom commands for Python developing. In MATLAB and Spyder, we have a nice interactive terminal. If you want a really fancy one, run a separate terminal (outside of Eclipse) and type ipython -pylab. This is about as fancy as Python terminals come, it has tab-completion and everything. Unfortunately, it doesn’t work in Eclipse’s limited console, but we can get the next best thing. If you just want a simple terminal that does pretty much everything MATLAB’s does, you can write a simple python script with this code in it:

from pylab import *

Then make a new External Tool the same as PyUIC with the following options:
Location: /usr/bin/python (or the path to your python executable)
Working Directory: ${container_loc}
Arguments: -i /path/to/your/script.py

Also: under the “Build” tab check Build before launch

This is really nice for some quick MATLAB-like editing, but if you do like I do and use

import numpy as np

style imports for your libraries, then you will be confused when in the terminal you can write

a = linspace(0,20,100)

but in the editor you have to write:

a = np.linspace(0,20,100)

For this reason, it could be nice to instead configure your External Tool without an additional script and simply use this setup:
Location: /usr/bin/python (or the path to your python executable)
Working Directory: ${container_loc}
Arguments: -i

This way you can use the same imports as whatever is in your script. Yet another helpful tool is to set things up exactly like the previous example, but include your current script. This will run your script just as if you hit the normal green button, but will let you interact with it afterwards to see what the variables are like, etc. (similar to debug mode, but you don’t need to set break points or anything, it’s a little easier to understand than the debug mode). Here’s how to set it up:
Location: /usr/bin/python (or the path to your python executable)
Working Directory: ${container_loc}
Arguments: -i ${resource_name}

Finally, a quick note about debug mode. I haven’t used it much, but it’s a bit more powerful than the simple interactive Python mode I showed you how to do, and it can show you all of your current variables, but it’s also a bit verbose, which has kindof scared me away from it. If you want to try it out, set a break point and click on the bug instead of the green run button. (You set break points by double clicking in the gray area in the editor)

And here’s what the debug for that script looks like:

Some useful things:

  • The top right area shows your current variables (along with some other namespace stuff that is kindof scary, just scroll down and click ‘x’)
  • The command prompt below has a nice, live python terminal, complete with Eclipse Auto-completion and documentation
  • If my code-complete box wasn’t in the way, you could see what part of your script you were at
  • I’m sure you could delete some menus and change the organization to make it a little less scary and more helpful, then save it so that you wouldn’t have all the unnecessary stuff

There you go, we’re all set up to use Eclipse. In the near future I hope to give a tutorial or two on how to use PyQt to make nice guis and how to set up a Node.js server with Mongodb, as well as how to set up a server-side javascript environment in Eclipse to develop for this Node.js server.

Posted in General Nerdings | 1 Comment

IDE Update

Fooling around in the Python(x,y) documentation a little bit, I found some cool GUI tools I thought would be worth looking into, and I discovered that I my initial judgements of the Eclipse IDE were a bit un-founded and unnecessarily harsh. From what I’ve heard from Mac users trying to use PTK and Spyder, they haven’t had much luck, and it’s a bit of a challenge, so I would highly recommend Eclipse for Mac users, and it’s got some nice features that may even convince non-Mac users like myself to adopt it.

Qt Designer: One awesome thing about Eclipse is that it has the Qt Designer built in! This makes it extremely easy to make GUIs. Here’s a screenshot of a simple project I’m working on:

Eclipse with the built-in Qt Designer

For those who don’t know, Qt is a really nice cross-platform GUI library, so this will create a nice GUI that will work on Mac, Linux, and Windows. It’s really pretty sweet, especially with the Designer so you can organize things visually. I’m beginning to like programming in Eclipse, but for those who aren’t sold, Qt Designer comes in a separate application, so you can always install that and write your code in whatever you want.

I also found that my initial complaint about Eclipse being slow was false. I timed Spyder and Eclipse in a cold start load, Eclipse actually loaded 10 seconds faster than Spyder (15 seconds for Eclipse, 25 for Spyder), both loaded in about 2 seconds in a “warm start” comparison. Neither is as light as PTK, but they both have a lot more features.

Some other advantages for Eclipse: It can also be used for a lot of other languages (C/C++ and Java to name a few), it has excellent code-completion, and it’s more stable than Spyder or PTK, since it is much more widely used. These advantages all work as a slight disadvantage as well, at least when comparing to Spyder, which was designed specifically for scientific computing. This means Spyder is an exceptional MATLAB replacement, while Eclipse is really more of a general programming IDE. It’s not perfect as a replacement for MATLAB, but it’s not hard to make it work either. I hope to get better at GUI programming and post some simple tutorials in the near future.

In case you want to fool around a little bit yourself before I get around to a tutorial, here are some things I’ve found:

  • If you have Python(x,y) installed, all you need to do to start playing around is:
    • Open up Eclipse and start a new PyDev project
    • Right click on the src folder to add a new file (choose other)
    • Choose Qt Designer Form for the file type
    • Now choose a window type (Main Window, Widget, etc.), and give it a name
    • One last thing before you can start playing around, you need to add the Qt Designer bars to your layout. You do this on the menu-bar under Window -> Show View -> Other
    • Next choose all of the sub-options under Qt (don’t mind the C++) and move them to wherever you want on the screen (the most important one is the Widget Box, that will let you drag and drop buttons and such)
    • There are a few more steps to translating this Qt app into Python code, but I’m still figuring out the best way to do it for my tutorial, so more on that when I finish it
  • If you didn’t install Python(x,y) you can install Eclipse and everything else you need following the instructions here.
Posted in General Nerdings | Leave a comment

Changing the Spark Plugs and Ignition Rotor

As promised, I got the additional things I needed and finished the job I started last weekend. It would have taken a total of about 20 minutes if it wasn’t for one problem: My rotor is attached with a small screw, which I broke the head of while trying to screw in the new rotor. I was left with half a screw stuck in my distributor and no way of getting it out.

To fix things, I had to go buy an easy out kit from Sears, I also had to guess the bolt size from the head, since the part I had broken off didn’t have many threads. Luckily, the easy out kit worked great.

The threads from my bolt removed by the easy-out power extractor.


And I happened to guess the right bolt size:
My brand new ignition rotor, ready to rock!

The spark plugs were painfully easy to replace, so I won’t go into much detail, just make sure you have a long enough extension and some kind of spark plug removal adapter. This is important so that it can “grab” the spark plug you’re removing and pull it out. Some tools do this with a magnet, mine used a piece of rubber that the spark plug barely fit into. Either way, you’ll have a hard time getting your spark plugs in and out without it. Here’s what everything looked like when I finished:

After driving it around for about a week since finishing up, the care doesn’t stall anymore at all, it still diesels about as much as it did before, but the main problem has been solved. My next task will be to get the air conditioner working properly again.

Posted in Other Projects | Leave a comment

Changing the Distributor Cap

For some time, my 1994 Nissan Sentra has had some trouble right after I first start it. It idle’s roughly and will often die soon after starting it. A simple Google search yielded this handy site: Fix a Car That Stalls. Number 4 seemed to be just the ticket, so after consulting with my Dad, I decided to change the distributor cap and the spark plugs.

I couldn’t possibly conceive of a more simple thing to do than change the distributor cap on this Sentra. Here are the only tools you’ll need:

  • A short screwdriver
    • It can be either Phillips or Flat-head, but you do need it to be pretty short. I ended up using my Swiss Army Knife, since I didn’t have a real screw driver short enough
  • A pencil and paper
    • Surprisingly, the most difficult part of changing the distributor cap is keeping track of where everything should be plugged in. Plan ahead and make note of it before you start
  • A new distributor cap

And that’s it! Pretty nice, eh? I changed my distributor cap in about 30 minutes, most of which was spent fetching tools, not actually changing the cap, so if you start with all the proper tools, I could see it being done in 5 minutes. I noticed some corrosion inside my distributor cap, so it was probably a good thing I changed it, but it didn’t completely solve my problem, so there remain a few things I can do:

  • Change the spark plugs
    • I fully intended to do that today, but they sit much deeper than I had thought, and I couldn’t jury-rig anything long enough to remove them, so if you’re going to change yours, keep that in mind and check to make sure you can reach them with your tools
  • Change the rotor
    • This is something I probably should have done when I changed the distributor cap. Looking at mine, it looks like it will be significantly more difficult to remove than the distributor cap, but it looked like it could definitely use replacing as well
  • Change the wires and plugs
    • Just to be sure we get everything to do with the distributor, changing the wires and plugs might be necessary too. As I pulled the plugs off of my spark plugs it looked like there was some corrosion at the ends, so changing those might help clean things up a little bit

Hopefully next week I’ll have some more to report about how to successfully replace the rotor, spark plugs, and wires. (Hopefully I can also report that I’ve successfully solved the problem, but we’ll see!)

Posted in Other Projects | Leave a comment

Changing the oil

I have an old 1994 Nissan Sentra and with my dad’s help, I’ve changed the oil on it more than once. Today, however, was the first time I changed it completely on my own, with all my own tools. It was, on the whole, a successful endeavor, but there are a few things I was lacking and I thought I would post a few quick reminders for those out there like me who may be changing the oil in their cars for the first time without a full set of tools.

Things you need (some of these may be obvious, but for completeness, I thought I’d include everything):

  • Oil (You need a different type for different cars. I just bought mine at Wal-mart, they have a book to tell you which type your car needs)
  • A new oil filter (Once again, Wal-mart has a book to tell you which one to get)
  • Clothes you can get dirty
    • As prepared as you may think you are, oil will get on your clothes
  • Some kind of oil draining pan
    • I used this one. It worked pretty well, it was just a little too tall for my car without jacking it up, so keep that in mind if you have a small car. It does have a really handy end, I didn’t even need a funnel to get the old oil into an empty milk jug. One thing that’s a little annoying, there is residual oil and there’s no lid to trap the smell or protect your wife from accidentally dropping something in it, so you’ll want a safe place to put it or else a garbage bag or something to keep it in.
  • A tool for removing the old oil filter
    • I used this one and it was fantastic. Make sure you get one that’s the right size for your oil filter, they make two, one for smaller filters (the one I got) and one for larger ones.
    • This may be obvious to some of you, but make sure you only use the tool to loosen the oil filter, filters should be hand-tightened only
  • A funnel. For whatever reason, I totally forgot a funnel, and I was able to pour the oil in, but I spilled a little, so that’s obviously less than ideal, just get a funnel.
  • A wrench (to remove the oil drain plug)
    • I can never remember what size I need for my oil drain plug and it’s a pain to switch while you’re under your car. You can save yourself some time and effort by making a note somewhere of what size you need
  • A rag
    • This is another thing I forgot. I’m not sure exactly why, but changing oil is about a million times easier when you have a rag. Maybe I just feel better about myself when I wipe my hands off, I don’t know.
  • Some cardboard or newspaper to catch any drips
    • I changed my oil at my grandpa’s house and I think it pained him not to be under the car with me, so he had to stand at the ready to hand me any tool he could think of. I didn’t think I’d really need any of that with the large drain pan catching all the oil drips, but you’d be surprised how nice it is to have a little newspaper to keep drips off the floor or off of your face.

If you need a refresher on how to change your oil, that will be a bit different for every car, but there’s a great link here to get you started. Don’t be afraid of it, it’s not rocket science and it feels very rewarding when you’re all done!

Posted in Other Projects | Leave a comment

Low-Speed Airfoil Data

For a class project, we each need to individually design and build small plane. It can be hard to find a lot of good data for airfoils at low Reynolds numbers, but thanks to the good folks at UIUC, there is a treasure trove of freely available (GPL) data. For more details, feel free to checkout their site here. I would also definitely recommend making a donation if you find their data useful.

Unfortunately, the raw data in the format they posted was a little tricky for me to use. Fortunately, Python made it easy parse that data and make some nice plots. I’ve included one of the airfoils as an image here, but you can download the plots I made for 3 volumes (138 airfoils!) in the attached .zip files. The zip files also include all of UIUC’s original data exactly as downloaded from their site, so you can look at everything raw if you want. At the end of the file MANIFEST.txt there are instructions for how to donate to UIUC. It’s also worth noting that the moment data in volumes 1 and 2 is useless, but it has been included in volume 3. I didn’t include that data in my Python script, however, so you’ll have to wait for me to update volume three with moment data if you want it (or you can import it yourself, it’s not too hard).

EDIT: I added moment curve data for all the airfoils in volume3 (the only ones that have measured moment data)

EDIT AGAIN: A classmate asked me about finding the coordinates of an airfoil once you’ve chosen one. A giant list of coordinates is found in the file COORD01.txt (or 02,03, depending on your volume). You can find this text file inside the .zip file, inside the volume01-02-03 folder. Just do a simple ctl+f to find your file and past the list of coordinates into excel, matlab, python, whatever suits your fancy, it’s pretty easy to plot them, they’re just x y coordinates.

Lift data for the A18 airfoil


Drag data, as well as a L/D curve for the A18 airfoil


volume01
volume02
volume03 – updated

Posted in General Nerdings | Leave a comment

First Tutorial – Modeling Dynamic Systems

To teach myself how to use Python instead of MATLAB, I could think of no better way than to dig up one of my old MATLAB homework assignments and do the whole thing in Python. My way of coding things may not be the best, this is my first attempt at using Python, but I was able to do the entire assignment and this tutorial will share how I did it. Let’s start by going over the assignment.

The Assignment

For a lab in class, we had a simple, physical second order system set up and LabView recorded the time and position of our simple mass. We checked both the free and step responses of this system and LabView recorded the data.

The goal for this assignment is to find which parameters for a model of a second order system will match the ones physically measured in this lab. There are four main parts to the assignment:

  1. Use a least-squares fit to identify parameters
  2. Estimate the parameters using logarithmic decrement
  3. Add dry friction to the model
  4. See if the model matches when a step input is applied

Getting Started in Python

In this tutorial, I’m going to assume you’re using Spyder as installed by the pythonxy package, although I’m sure everything works just peachy using any other appropriate Python set up. (Spyder is my favorite IDE, my next favorite that I’m forced to use when I boot into Linux is the PythonTookit (PTK), Eclipse with PyDev also works well, but I prefer the others).

When using Spyder, it’s very helpful to test the code you’re going to write in the live console before you write it into your script. There are a couple of advantages to doing things this way:

  • You always know that you’ve got the syntax correct
  • You have live access to your variables in the variables explorer. For some reason the variables don’t stay in the explorer when you execute your script, they get cleared out as soon as your script finishes running (if you run in debug mode, you might be able to pause your code and look at the variables, but I think it’s just easier this way…)

Spyder with some important areas highlighted in red. The bottom right is the live console, the top right can be changed between the Object Inspector and the Variable Explorer. The Object Inspector is really helpful for on-the-fly documentation and help. The Variable Explorer is always handy to see what variables you've used and what kind of values they have.

Now, when you create a file in Spyder for the first time it will automatically add a few lines of code at the beginning, this is mainly for documentation. In Python the pound sign (#) is used for comments (as opposed to % in MATLAB). This isn’t really too big of a change, except that it’s very useful in Unix/Linux systems (possibly OS X as well, I’m not sure) because you can add the line: #!/usr/bin/python to the top of your script and then it can be run as its own executable! The other thing to note is that the the triple quotes are just a special kind of comment that can be used as documentation for your script.

The first thing we have to do to make our Python script work right is to import the proper libraries. There are a few ways you can do this, I’ll go over a few of the options, since it makes a slight difference as to how you write your code. First let’s import the numpy library two different ways (numpy is a library that contains a lot of the functions we’ll be needing, others we’ll be using will be scipy and pylab).
from numpy import *

This will blindly import everything in the numpy library. This is nice because you will have direct access to all the numpy commands. It’s not so nice because if you import another library the same way, you may have duplicate commands. An alternative is to import numpy like this:
import numpy as np

This will require a little more typing, because every time you use a command from the numpy library you will have to type np.command() instead of just command(). While this may be a pain, it’s also very nice because now you can specify which library your commands come from. I find it particularly useful when I’m in linux using PTK because PTK’s Namespace Browser has a live variable display, but it also includes all of your available commands in that display. If you import as np, then instead of listing every command available in numpy, it only lists np, and once you click on np it shows you all available commands from within that library.

There are a few other tricks to importing libraries. Often, commands are nested within a library, and how you import them can affect how you may call the commands later. For example:
from scipy.integrate import odeint
now allows me to use the command odeint whenever I want, without having to call integrate.odeint. Finally, you can use an import command on its own. If I load pylab like this:
import pylab
then every call to a command from within pylab must be accessed like this: pylab.command()

I’m sure there’s a lot more that could be said about importing libraries in Python, but I’ve already said too much about them for the purpose of this tutorial, so let’s go on.

Processing External Data

The first thing we need to do in this program is to load our external data. Both numpy and scipy have a few commands available for this, depending on what format your data is in. Before we can load any data, though, we need to set the proper working directory. Your working directory is shown in the top right, you can browse to a new folder by clicking the folder button, then set the working directory by clicking the button directly to the right of it.

The boxes highlighted in red will help you set your working directory in Spyder

My data is a simple tab-delimited text file, so the numpy function loadtxt works wonderfully.
free_response = loadtxt("free_response.lvm")
Note: type “loadtxt” into the Spyder Object Inspector to look at the documentation for this function. If you scroll down a ways there is a nice “See Also” box that provides some helpful tips for a few other functions that might be useful, particularly scipy.io.loadmat

It’s helpful to have a quick look at this data to see how we’ll want to process it. We can plot the data from the interactive terminal just to see what it looks like.
free_response = loadtxt("free_response.lvm")
plot(free_response[:,0],free_response[:,1],free_response[:,0],free_response[:,2])

Looking at my code to plot this data, you can see a few simple differences between Python and MATLAB.First of all, we access arrays slightly differently than in MATLAB. In MATLAB, parentheses are used (), but in Python we use brackets [].

It’s also noteworthy that in Python our arrays start at 0, while in MATLAB they start at 1.
The plot command, taken from Pylab, was modeled after MATLAB’s, so there shouldn’t be much difference there.

Now, when you look at this graph, you can see that the first few points are just stagnant, and that we don’t really want them when we’re dealing with how to fit the model. Let’s delete the first 20 points. Before we can delete a series of points, it’s helpful to go over a couple of useful commands, arange and linspace.
arange will create an array based on a start value, an end value, and a given step size, this can be really useful for handling indeces in arrays. linspace is very similar, except that instead of defining a step size you define the desired number of points, which can be really useful for graphing.

Let’s use arange to help us delete the first 20 points:
a = arange(0,20,1)

Now we can call the delete command to delete those points from the array:
free_response = delete(free_response, a, 0)

As you type the delete command in the terminal, you can see the documentation above that it takes an array, an object, and an axis. We give it the name of our array, and as an “object” we give it the array of indeces we want to delete, since we want to delete along the first axis, we pass 0 as the axis.

The next step is to re-zero our time vector, which is easy enough to do by subtracting the minimum time from the time vector:
free_response[:,0]=free_response[:,0]-min(free_response[:,0])

Step 1 – Approximating the model with least-squares

The theory of this method is not really what we’re trying to explain in this tutorial, but the following equations give you an idea of what we’re trying to do with a least-squares fit:

Equations for the least squares method we will use.

To set up our least-squares approximation, we’ll need vectors for position, velocity, and acceleration. We can find these using the diff command:
time = free_response[:,0]
pos = free_response[:,1]
vel = diff(pos)/diff(time)
time = delete(time,-1)
accel = diff(vel)/diff(time)

The diff command is easy enough to understand, you can learn more about it in the Object Inspector documentation. One interesting thing in the above commands, however, deals with indexing arrays in Python. Passing an index of -1 is simlar to passing end in MATLAB.

Now we need to adjust the vectors so that they’re all the same size:
time = delete(time,-1)
vel = delete(vel,-1)
pos = delete(pos, [pos.size-1, pos.size-2], None)

Now we get to have a little bit of fun with Python’s way to smash arrays into matrices. We want to stack our velocity and position vectors on top of each other to make a matrix. We do this with the vstack command. This vertically stacks two arrays on top of each other (yes, there is a very similar hstack command to stack arrays or matrices next to each other horizontally). We then transpose this just so it will be in the correct orientation for the least squares method we’ll use. You can transpose any array just by adding .T to the end of it.
A = vstack((vel,pos))
A = A.T

And now we’re ready to run our least squares fit to get some parameters out to model. The least squares function lives inside the numpy.linalg namespace, so we could call it like this:
phi = linalg.lstsq(A,accel)

This won’t give you quite everything in the way you would expect, however. If we look at the lstsq documentation, we see that this command actually returns quite a bit more than just the least squares parameters we’re interested in.

The values returned by the least squares function

To put things in a little bit nicer form for us, let’s use this instead:
phi,residues,rank,s = linalg.lstsq(A,accel)

Now we can extract the second-order parameters:
omegan = sqrt(-phi[1])
zeta = phi[0]/(-2*omegan)

And now for the fun stuff, we get to define a function to run through an ode solver. Python is a full-fledged programming language, so defining functions is really quite simple, we can define a function anywhere we like, no need for a separate file or anything silly like that. The key things to defining a function are first, we use the def command to let Python know we’re defining a function, then we name it, include the values it can take in parenthesis, then we indent all the following lines that are part of our function (indentation is important in Python). We should also return some value before we finish our function. Here’s what we’d do to have a function that acts like our second order system:
def stage1(x,t):
return[ x[1], (-2*omegan*zeta*x[1] - omegan**2*x[0])]

And that’s how easy functions are in Python! Now on to solving ODE’s. The first thing our ODE will need is the initial conditions, which are easy enough:
x0 = [pos[0],vel[0]]

Now, technically to get the same algorithm that MATLAB’s ode45 uses (or in other words, to use the fourth-order Runge-Kutta method), we need to use the function scipy.integrate.ode. This is a very advanced ode solving function that will allow us to choose one of several methods to solve an ode, unfortunately I still haven’t figured out how to use it, so in the mean time we can use the much simpler scipy.integrate.ode. To make typing easier, we can import the library like this:
from scipy import eye
from scipy import integrate.odeint

This allows us to call the command odeint without needing to type the scipy.integrate at the beginning. Now, using the command is as simple as:
pos_calc = odeint(stage1, x0, time)

If you are family with plotting in MATLAB, using pylab plotting functions in Python will look very familiar to you. I imported pylab without any *, so I need to prefix all my pylab commands with pylab.:


pylab.figure(1)
pylab.plot(time,pos, label='recorded data')
pylab.plot(time,pos_calc[:,0], label='simulated ode')
pylab.legend()
pylab.xlabel('Time (sec)')
pylab.ylabel('Amplitude')

And voila, we’ve completed stage one and we have a nice graph to prove it! Just a few notes on the side, if you typed all this into a script and see no graph after you hit “run”, don’t be afraid, there is one more pylab command you need before the graph will appear:
pylab.show()

The graphical result of our efforts thus far.

This let’s you control when in your script your graphs will appear. One thing to note, if you have a long program but you only want to execute part of it (or if you’ve written your code in “block code”) you can hit F9 and only the selected code or block will run. At least on my system, the default iPython console does this very slowly, I found it much more speedy to open up a normal Python console (Run -> Open Interpreter) and then hit F9.

Files used in this tutorial: files_used

Posted in General Nerdings | Leave a comment

Migrating from MATLAB to Python

Like most mechanical engineering students I know, I have a love-hate relationship with MATLAB. Most of the time, it’s great, but it is not free and running it through a network (like BYU does on all of its computers) can be a huge pain. As an alternative, I’ve decided to switch to Python, which is free and completely cross-platform. I haven’t done a whole lot in it yet, but I’ve heard you have a little more control over how your graphs look in Python as well.

Installation – Windows

For Windows users, there is a nice package called Python(x,y) that pretty much installs everything for you, including both the Spyder IDE and Eclipse. I haven’t used Eclipse yet, but I’ve found Spyder to be a very nice alternative to MATLAB. To install, just go to http://www.pythonxy.com/ and download the Windows Installer package.

A Windows screenshot of my Spyder installation.

Installation – Ubuntu Linux

Part of the beauty of switching to Python is how cross-platform it is. While pythonxy hasn’t finished a linux build yet, installing spyder is pretty easy in ubuntu:
sudo apt-get install python-sphinx python-scipy python-matplotlib spyder

Note: All you really need is spyder, but adding python-sphinx allows you to have rich text in your documentation which looks nice and in my opinion is easier to read. The packages python-scipy and python-matplotlib may be optional, but I use them in my tutorials, so I would recommend including those as well.

Installation – Gentoo Linux

Unfortunately, python(x,y) doesn’t have a convenient ebuild for Gentoo Linux, but here’s how to get a similar setup up and running in Gentoo:
install the base libraries and such that you will need:
emerge -av numpy scipy matplotlib ipython

For my Gentoo installation, the Spyder IDE unfortunately does not work (for one thing, it’s Qt, not GTK, so it looks ugly on my desktop. For another, it occasionally crashes my entire desktop, which is a little less forgiveable, you can install it at your own risk if you unmask it, but I recommend an alternative IDE)
Option 1: PythonToolkit (http://pythontoolkit.sourceforge.net/)
emerge -av wxpython
download and unzip the source .zip
python setup.py install
It didn’t automatically create a start menu entry, but you can now run PTK to fire up the IDE.

The main PythonToolkit window, note the nice documentation window on the right for quick help.


Screenshot of the PythonToolkit Editor


Option 2: Eclipse
emerge -av eclipse-sdk
Once Eclipse has installed, follow the instructions on the PyDev page to enable the Python IDE in Eclipse
http://pydev.org/manual_101_install.html
I haven’t fooled around with it too much, but it looks pretty nice:

A quick and simple Python project I set up in Eclipse

Mac OS X Installation

I don’t own a Mac, and never will, but for completeness, I thought I’d include some links for how to get set up in OS X. Python stuff is generally pretty cross-platform, so in OS X you still have the choice between Spyder, PTK, and Eclipse. I’ve found Eclipse to be bloated, slow, and incomplete for what I want to do, so I’ll only include instructions for Spyder and PTK, especially since they have similar dependencies.

If I’m not mistaken, Mac OS X already comes with Python installed, but the Numpy site claims their binaries only work with the official version, Apple must have corrupted their default. Before proceeding, you may want to look at this link for some alternatives. I would recommend to go ahead and grab the official version at python.org . Choose the appropriate binary for Python 2.x (last I checked, it was 2.7.1).

For PTK, the only other dependencies you need are:

  • WxPython (Necessary for the GUI)
  • Numpy (Array handling in Python)
  • Scipy (More useful scientific functions for Python)
  • Matplotlib (A MATLAB-like plotting interface)

After that, download the latest stable version of PTK here.

For Spyder, you will additionally want these dependencies:

  • pyflakes (real-time error checking, gives you fuzzy spell-check-esque lines when you have bad syntax)
  • rope (code completion, etc.)
  • sphinx (make code documentation pretty with rich text format)
  • iPython (a more advanced interactive terminal)

My Two Cents

After about a month of Python programming, I really like both Spyder and PTK. Spyder is pretty big, with a lot of fancy features that also make it take a while to load, I would definitely not recommend it for slower machines or if you don’t have much RAM. PTK, on the other hand, is super-light and super-fast. It lacks a lot of Spyder’s fancier features, like auto-completion, pretty documentation formatting, and live syntax checking, but it has all the essentials and I really like it.

Posted in General Nerdings | 4 Comments

How many engineers does it take to change a lightbulb?

We all joke about stereotypical abilities (and inabilities) to change lightbulbs, but there are a bunch of great reasons to replace your standard lightbulbs with more energy-efficient Compact Fluorescent Lightbulbs. There are a lot of important things to consider when you do buy CFLs, most importantly you need to get the right size, shape, color, and wattage to replace your current bulb (Energy Star has a great guide here). Also, be careful when disposing of your CFLs, they last 10 times longer than standard lightbulbs, but they do contain trace amounts of Mercury, so be sure to recycle them to keep that mercury from filling up landfills. Recycling them is pretty easy, Lowes and Home Depot both have programs to recycle CFLs, or you can look up alternative recycling locations at earth911.com.

Now, on to the analysis, how much of a difference does it make to switch?

Assumptions:

  • 440 light bulb hours per week
  • all are standard 60W lightbulbs
  • all can be replaced with 15W CFLs

  • 60W x 440 hours = 26.4 kW-hr
  • 15W x 440 hours = 6.6 kW-hr

It was easy to see from the beginning that replacing all of your 60W bulbs with 15W ones would cut the energy used by 3/4, but what do those 19.8 kW-hr of energy per week mean? That depends on a lot of factors, but mostly just on how your city generates its electricity:

  • Coal is the most common, and therefore has a wide spread, depending on which process is used between 850 and 1200 grams of CO2 are produced per kW-hr of energy
    • Averaging those, we would save 20.3 kg of CO2 emissions per week
  • Oil produces about 900 grams of CO2 per kW-hr
    • This would mean a savings of 17.8 kg per week
  • Natural gas is about 600 grams CO2 per kW-hr
    • 11.9 kg per week
  • Nuclear power produces 60 grams CO2 per kW-hr, while Hydroelectric power emits only 15 grams, and windpower only 21 grams
    • Nuclear: 1.2 kg/week
    • Hydro: 0.3 kg/week
    • Wind: 0.8 kg/week

To compare with last week, we can convert back to english units (pounds) to see how much CO2 we can save compared to riding a bike instead of driving:

  • bike: 56.4 lbs CO2 saved
  • lightbulb (coal): 44.6 lbs CO2 saved
  • lightbulb (oil): 39.2 lbs CO2 saved
  • lightbulb (nat’l gas): 26.1 lbs CO2 saved
  • lightbulb (nuclear): 2.6 lbs CO2 saved
  • lightbulb (hydro): 0.7 lbs CO2 saved
  • lightbulb (wind): 1.8 lbs CO2 saved

So for us in our apartment, we would probably save at least 40 lbs of CO2 per week by switching all of our lightbulbs to CFLs. People with larger houses, more light bulbs, or who are just at home with the lights on longer that us would save a much larger amount by making the switch.

Posted in Green Engineering | Leave a comment