Euler's Method in Python

• Python

Main Question or Discussion Point

Hi
Here's is the differential equation I need to solve using Euler's Method:

v' = 5 - 0.5v^2

I need to plot the position x(t), velocity v(t) and acceleration a(t) as a function of time.

v(0) is 0

I have the data for time:
s = linspace(0, 12, 121) #(delta t is 0.1)
But, that's about it...

What commands should I know to be able to solve equations like this using Python..

Related Programming and Computer Science News on Phys.org
rcgldr
Homework Helper
Is this homework? If so we're supposed to wait for you to propose a solution and ask for help. It's not clear if you understand Eulers method, but that should have been explained in class or you can look it up doing a web search.

There are better methods than Euler for doing numerical integration, but I assume the assignment is to use the Euler method. If you're curious, I can post a link to threads here and at other web sites about the other methods.

Yes, it was for homework. I had to hand it in wednesday (09. February), and I failed to solve the parts where I needed programming.

I could use either Python or Matlab. Since I do not have access to Matlab on my computer, I decided to use Python.

The lecturer will use Matlab when reviewing the project, so I need somebody else to tell me how to solve it using Python.

Do you know how to use python?

To do this in python, you'd use numpy 'cause it's the python numerical computation library.
You can use the numpy to matlab cheat sheet to translate your professor's solutions.

Though maybe, since you're so lost on python, it may be a good idea to try out octave.

jhae2.718
Gold Member
Last edited by a moderator:
I can never make matplotlib work properly on Windows 7, so I am using Gnuplot.

I have the vector with all the values for time:
s = linspace(0, 12, 121)

I need this to work:
plot (s, vt)
hold('on')
plot (s, xt)

But, I cannot make the vectors for vt and xt.
I am new to Python, so I need help.

Preferably, you have a link to a website with tutorials/lectures or a good book. I need to learn how to solve mathematical problems with Python. Googling has not helped me much.

Dude, you really really should just use matplotlib as it integrates well with numpy. What version of python are you using? You may have used the wrong installer.
Matblotlib has loads of examples.

I need to learn how to solve mathematical problems with Python.
You'd solve this the exact same way you would on paper of using a calculator-python is just the syntax. We can help you out, but what are vt and xt supposed to be? What formulas were you given that mention v and x of t?

Looking at some of your other stuff, probably something like:

Code:
import numpy
from matplotlib import pyplot as plt
ET = 100 # or whatever you want your end time to be
t = numpy.arange(0,ET,0.1)
v = x/t
plt.figure()
plt.plot(s, vt)
plt.plot(s, xt)
plt.show()
Preferably, you have a link to a website with tutorials/lectures or a good book.
The numpy/scipy/matplotlib documentation is actually pretty decent and chock full of examples, and you can try the cookbook. Most of the intro-python stuff is geared towards programmers and will probably fly straight over your head.

You'd solve this the exact same way you would on paper of using a calculator-python is just the syntax. We can help you out, but what are vt and xt supposed to be? What formulas were you given that mention v and x of t?
a(t) is acceleration
v(t) is velocity
x(t) is position
t is time
And, I presume everybody knows:
a(t) = v'(t) = x''(t)

And, the equation is:
a(t) = 5 - 0.5v(t)^2

a(t) = 5 - 0.5v(t)^2
in python, it's just:
Code:
a = 5-0.5v**2
provided you've already solved for v. So how do you think you're supposed to do that?

in python, it's just:
Code:
a = 5-0.5v**2
provided you've already solved for v. So how do you think you're supposed to do that?
a(t) = 5-0.5v(t)^2 is a differential equation:
It is the same as:
v'(t) = 5-0.5v(t)**2
I am supposed to solve it using Euler's method:
v(t_n)= v(t_n-1) + (5-0.5v(t_n-1)**2)*Δt
Δt = 0.1
Also v(0) = 0

I need to solve v for every t_n between 0 and 12. Also, I need to plot it as a function of t.
Then, I need to use Euler's method again to find x for every t_n between 0 and 12. I must plot x as a function of t.
Moreover, both curves needs to be on the same graph.

Last edited:
jambaugh
Gold Member
For "quick and dirty" plots if you are too short on time, try saving your output values to a .csv file and you can then plot using a spreadsheet such as Excel.

Secondly, since you are new to python and the method, I'd start with the built in floating point arithmetic of Python, get it done fast and then if you have time try to improve precision with the numpy package. It's homework, not a critical shuttle mission calculation so demonstrate you can implement the algorithm first.

Okay...
Here's what I have (for v)... What am I doing wrong?
Code:
from scitools.easyviz.gnuplot_ import *
from numpy import *

a = 5
b = 0.5
h = 0.1 # delta t

s = linspace(0, 12, 121)

t = zeros(121,float)
v = zeros(121,float)
t[0] = 0
i = 0

while t[i] <= 100:
v[i+1] = v[i] + (a - b*v[i]**2)*h
plot(s,v)

Well, I finally figured it out (we had a substitute teacher who knew python):

Code:
from scitools.easyviz.gnuplot_ import *
from numpy import *

a = 5
b = 0.5
h = 0.1 # delta t

s = linspace(0, 12, 121)

t =  zeros(121,float)
v = zeros(121,float)
ac = zeros(121,float)
x = zeros(121,float)

for i in range(121-1):
ac[i] = 5 - b*((v[i])**2)
v[i+1] = v[i] + ac[i]*h
x[i+1] = x[i] + v[i+1]*h

plot(s,v)
hold('on')
plot(s,x)
Thanks to everybody who tried to help. I probably will not have much trouble with similar tasks in the future.

Last edited: