How can I use Euler's Method in Python to solve a differential equation?

In summary, the student needs to solve a differential equation using Euler's Method, plot positions and velocities as a function of time, and use Euler's Method to find x for every t_n between 0 and 12.
  • #1
Avatrin
245
6
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..
 
Technology news on Phys.org
  • #2
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.
 
  • #3
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.
 
  • #4
Do you know how to use python?
 
  • #5
To do this in python, you'd use numpy 'cause it's the python numerical computation library.
You can use the http://www.scipy.org/NumPy_for_Matlab_Users 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.
 
  • #6
For plotting, use http://matplotlib.sourceforge.net/" if you go the Python route.

Here's a link to the NumPy tutorial: http://www.scipy.org/Tentative_NumPy_Tutorial
 
Last edited by a moderator:
  • #7
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.
 
  • #8
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 http://www.scipy.org/Cookbook. Most of the intro-python stuff is geared towards programmers and will probably fly straight over your head.
 
  • #9
story645 said:
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
 
  • #10
Avatrin said:
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?
 
  • #11
story645 said:
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:
  • #12
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.
 
  • #13
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)
 
  • #14
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:

Related to How can I use Euler's Method in Python to solve a differential equation?

1. What is Euler's Method in Python?

Euler's Method is a numerical method used for solving ordinary differential equations (ODEs) in Python. It is named after the Swiss mathematician Leonhard Euler and is often used in scientific and engineering applications.

2. How does Euler's Method work in Python?

Euler's Method approximates the solution of an ODE by using small time steps and calculating the next point on the curve using the slope at the current point. This process is repeated until the desired accuracy is achieved.

3. What are the advantages of using Euler's Method in Python?

Euler's Method is relatively simple to implement in Python and does not require advanced mathematical knowledge. It is also a versatile method that can be applied to a wide range of ODEs.

4. Are there any limitations to using Euler's Method in Python?

Yes, Euler's Method can introduce significant errors when used to solve complex ODEs with rapidly changing slopes. It also requires a small time step to achieve accurate results, which can make it computationally expensive.

5. Can Euler's Method be used for solving systems of ODEs in Python?

Yes, Euler's Method can be extended to solve systems of ODEs in Python by using a vector approach. This allows for the simultaneous solution of multiple ODEs and is commonly used in scientific and engineering simulations.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
801
Back
Top