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

Click For Summary

Discussion Overview

The discussion revolves around using Euler's Method in Python to solve a specific differential equation related to velocity, position, and acceleration. Participants explore the implementation of this numerical method, including plotting results and addressing challenges faced by a user who is new to Python programming.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant presents the differential equation v' = 5 - 0.5v^2 and seeks guidance on using Python to solve it and plot related functions.
  • Another participant questions whether the inquiry is homework-related and suggests that the original poster should propose a solution first.
  • Some participants mention that there are better numerical integration methods than Euler's Method but assume the assignment requires its use.
  • Several participants provide links to resources for using NumPy and Matplotlib in Python, emphasizing their importance for numerical computations and plotting.
  • One participant expresses frustration with using Matplotlib on Windows and mentions using Gnuplot instead, while seeking help with creating vectors for plotting.
  • There is a discussion about the definitions of acceleration, velocity, and position, with some participants clarifying the relationships between these quantities.
  • A participant shares their code and seeks help with errors, leading to further clarification on how to implement Euler's Method correctly in Python.
  • Another participant suggests saving output values to a CSV file for plotting in Excel as a quick solution if time is short.
  • Eventually, a participant shares their corrected code that successfully implements Euler's Method and plots the results, thanking others for their assistance.

Areas of Agreement / Disagreement

Participants generally agree on the use of Euler's Method for the homework task, but there are differing opinions on the best tools and methods for implementation, as well as the effectiveness of Python versus other software like Matlab or Gnuplot. The discussion remains unresolved regarding the optimal approach to learning and applying Python for mathematical problems.

Contextual Notes

Some participants express uncertainty about the original poster's understanding of Euler's Method and Python programming, indicating that foundational knowledge may be lacking. There are also mentions of potential issues with specific software versions and installation problems that could affect the implementation.

Avatrin
Messages
242
Reaction score
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
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 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.
 
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:
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 http://www.scipy.org/Cookbook. Most of the intro-python stuff is geared towards programmers and will probably fly straight over your head.
 
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:

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
Replies
55
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K