Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Friday, November 4, 2016

Energy comparison plot revisited

     Looking back in my post about energy comparison graph,


is easy to see room for improvement. So in this one I'm sharing a code to make the same plot, but in much more pythonic way. Take a look in the code:


     In the first time I refused using "def" to define the function, thinking this would be overkill for a simple script, and would affect the readability. I was wrong, now the code bloat is reduced, and the readability is even better. The way python works, permitting that you define the parameter value inside an array (look at lines 31, 32, 33), keeps the readability, and also makes the order of the parameters in the array unimportant. Sweeeeeet.

Friday, October 28, 2016

Plotting Beam Diagrams with Python

     Today I'm sharing how to plot Beam Diagrams using python. More specifically the code is about a cantilever beam subjected to gravity load, a special case of uniformly distributed load. There's a bunch of cool python features in this post, like the very convenient use of "x>" to evaluate a function, or part of it, only after a specified value in the array.

For example, the code: (my blog font shows this symbol =, as equals, this is a minus -)

import numpy as np
x=np.linspace(1,10,10)
y=2*x*(x>5)
print x
print y

returns these two arrays.

[  1.   2.   3.   4.   5.   6.   7.     8.   9.    10.]
[  0.   0.   0.   0.   0.  12.  14.  16.  18.    20.]

     Values with "false" boolean condition returned 0, and the values with "true" condition were evaluated. This is very useful when computing singularities functions, also known as Macaulay method, due to the British mathematician William Herrick Macaulay.  Oddly this method was first proposed by the German mathematician Alfred Clebsch. 

    Other features that worth take a look is how to make very neat plots using matplotlib subplots method.

The code:



The plot:


See ya!

Friday, July 15, 2016

Car, motorcycle and bicycle energy comparison graph using Matplotlib

     Everytime I met bike enthusiasts there's a lot of fuss about the bicycle being more efficient than cars and motorcycles. So I thought in making a graph comparing cars, motorcycles and bicycles . Of course here Is shown a mechanical stand point about the subject.

     The simplest energy equilibrium equation goes like this:



     F_t is the traction force 
     F_r is the resistance force
     F_i is the inertial force
     F_a is the aerodynamic drag force
     F_r is the rolling resistance force
     F_g is the gravitational force
     V is speed

     The power that makes the vehicle goes forward equals the speed times the its traction power. That power has to overcame the resistance forces in the opposite direction of the traction. 
     The resistance forces can be model as, inertial forces, aerodynamic drag forces, rolling resistance forces, and gravity resistance forces.  Roughly the inertial force deals with speed variations, the aerodynamic drag is the drag imposed by the air, the rolling resistance is deals with the mechanical friction between parts and the gravitational forces is the tangential forces caused by gravity, helping you in downhill, and making your life harder in hillclimbs. Mathematically they can be written as bellow. 



      C_d is a dimensionless parameter called drag coefficient.


     C_r is a dimensionless paramenter called coefficient of rolling resistance.


      I is the steepness of the hill (rise/run)

      To plot the graph I'm using an amazing python library called matplotlib. See the script.

    
     And here is the graph plotted:


     One final word, a very cool site doing an interactive version of this calculations only for bike) can by found in Mr. Steve Gribble website.


Once again, thanks for reading. 

Eddie. 

Saturday, June 11, 2016

Live data plot with Arduino + Python

      This post is about my interpretation about live data plot using  Arduino and python. I say it is my interpretation because it already been post by a few people some scripts about that. I like very much the one posted by Mr. Paul McWhorter, its simple and very well explained.

http://www.toptechboy.com/tutorial/python-with-arduino-lesson-11-plotting-and-graphing-live-data-from-arduino-with-matplotlib/

     But the problem begins when you need high baudrates. Mr. McWhorter application is reading a temperature sensor relatively slowly. My application was a Gyroscope + Accelerometer MEMS sensor (MPU6050 by Invensense), and I needed faster rates. I don't know why, at least with my setup, Python 2.7, Ubuntu, Arduino Uno Clone and MPU6050, when the communication between computer and the microcontroller begins, Arduino sends some gibberish just in the very beggining, but it is enough to ruin the communication. My way around this problems goes in the script below.



     Here is the print screen of the script running, it's very cool to see real time data being plot.


     One final information: in the arduino side I used a Script by Jeff Rowberg, which can be found in https://github.com/jrowberg/i2cdevlib. 

Thanks for reading

Eddie