473,480 Members | 1,847 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Forward Euler method for N-dimensional ODE

5 New Member
Hi,

I'm trying to come up with a FWE method for an N-dimansional ODE. So far, I was able to turn the one dimensional solution into a two dimensional one:

Expand|Select|Wrap|Line Numbers
  1. from scitools.std import *
  2. import numpy as numpy
  3. v0=0
  4. K=3 
  5. n=10
  6. dt=0.1
  7. t0=0
  8.  
  9. def f1(u1, u2, u3, t):
  10.     return u1
  11.  
  12. def f2(u1, u2, u3, t):
  13.     return u2
  14.  
  15. def f3(u1, u2, u3, t):
  16.     return u3
  17.  
  18. listf=[f1, f2, f3]
  19. listu0=[1, 1, 1]
  20. listu0 = map(float,listu0)
  21.  
  22. class ForwardEuler:
  23.     def __init__(self, listf, dt):
  24.         self.listf, self.dt = listf, dt
  25.  
  26.     def set_initial_condition(self, listu0, t0=0):
  27.         self.listu = [] 
  28.         self.t = [] 
  29.         self.listu.append(listu0)
  30.         self.t.append(float(t0))
  31.         self.i =0
  32.     def solve(self, K):
  33.         """Advance solution in time until t <= T."""
  34.         tnew = 0
  35.         while tnew <= K:
  36.             listunew = self.advance() 
  37.             self.listu.append(listunew)
  38.             tnew = self.t[-1] + self.dt
  39.             self.t.append(tnew)
  40.             self.i += 1
  41.         return numpy.array(self.listu), numpy.array(self.t)
  42.  
  43.     def advance(self):
  44.         """Advance the solution one time step."""
  45.         # avoid "self." to get more readable formula:
  46.         listu, dt, listf, i, t = \
  47.             self.listu, self.dt, self.listf, self.i, self.t[-1]
  48.  
  49.         lustunew = listu[i] + dt*listf(listu[i], listu[i], t) # Forward Euler scheme
  50.         return listunew
  51.  
  52. method = ForwardEuler(listf, dt)
  53. method.set_initial_condition(listu0, t0)
  54. listf, t = method.solve(K)
  55. print listf
  56.  
I'm now working on the N-dim solution by using a matrix. It looks like this:

Expand|Select|Wrap|Line Numbers
  1. class Matrix(object): 
  2.     def __init__(self, kolommen, rijen): 
  3.         self.kolommen = kolommen 
  4.         self.rijen = rijen 
  5.         # Creeer matrix met nullen
  6.         self.matrix = [] 
  7.         for i in range(rijen): 
  8.             ea_rijen = [] 
  9.             for j in range(kolommen): 
  10.                 ea_rijen.append(0) 
  11.             self.matrix.append(ea_rijen) 
  12.  
  13.     def plaatselement(self, kolom, rij, v):   #Zet element v op plaats [kolom, rij]
  14.         self.matrix[kolom-1][rij-1] = v 
  15.  
  16.     def neemelement(self, kolom, rij):  #Neemt element op plaats [kolom, rij]
  17.         return self.matrix[kolom-1][rij-1] 
  18.  
  19.     def __repr__(self): #Dient om matrix te printen
  20.         outStr = "" 
  21.         for i in range(self.rijen): 
  22.             outStr += 'Rij %s = %s\n' % (i+1, self.matrix[i]) 
  23.         return outStr 
  24.  
Sorry for the weird names, but I'm from Belgium so I speak dutch and therefor some of the names I use are dutch. Anyway, the beginning of my N-dim solution looks like this:

Expand|Select|Wrap|Line Numbers
  1. from scitools.std import *
  2. from numpy import *
  3. from Matrix import Matrix
  4.  
  5. #moeten gedefinieerd worden
  6. F
  7. x0 = []
  8. #N, F, u0 worden gedefinieerd in oproepprogramma
  9.  
  10. class ForwardEuler:
  11.     def __init__(self, F, dt):  
  12.         self.F, self.dt = F, dt
  13.  
  14.     def set_initial_condition(self, Matrix ,lijstU0, N, t0=0):
  15.         M = T/float(self.dt)  #We bepalen de lengte van de kolommen door interval te delen door de lengte van de stapjes
  16.         M = int(M)  #We gebruiken hier int om er een geheel getal van te maken en het rond naar beneden af.
  17.         self.u = Matrix(M,N)  #We maken onze matrix een MxN matrix
  18.  
  19.         n = 1 #Tellen over het aantal functies
  20.         while n <= N:   #Loop plaats cte u0 in eerste kolom
  21.             self.u.plaatselement(1, n, u0[n-1])
  22.             n += 1
  23.  
  24.         self.t = []    
  25.         self.t.append(float(t0))     
  26.         self.i = 0
  27.  
So now I've got to define the functions solve and advance, but I'm a bit stuck here... If anyone could help, that would be great.
May 10 '10 #1
5 4509
Glenton
391 Recognized Expert Contributor
Hi

So numpy has a matrix which you could surely use? It doesn't look like your Matrix class does anything other than assignment and recall?

Could you explain what the n-dimensional step is going to be? In plain english would be fine? What's the calculation that you're struggling with?

I'm relatively confident that numpy matrix or array operations will be suitable for you.
May 10 '10 #2
Dunnomuch
5 New Member
An N-dimensional ODE has N equasions:
df1/dt = f1(x1, x2, x3, ..., xN, t)
df2/dt = f2(x1, x2, x3, ..., xN, t)
...
dfn/dt = fn(x1, x2, x3, ..., xN, t)
The solution should be N different functions and those should come in the matrix. Afterwards, it should be possible to plot those N functions. But don't get me wrong: We won't really find functions, but functionvalues in a certain interval [0, T]. And it's those values that we plot afterwards.
I hope I solved your question.
May 10 '10 #3
Glenton
391 Recognized Expert Contributor
Hi

So this example might get you started (apologies for the scrapiness - I am in haste):

Expand|Select|Wrap|Line Numbers
  1. def f0(u,t):
  2.     #u is an array of n elements
  3.     return 2+u[0]+u[1]+t
  4.  
  5. def f1(u,t):
  6.     #u is an array of n elements
  7.     return 1-u[0]-u[2]-t
  8.  
  9. def f2(u,t):
  10.     #u is an array of n elements
  11.     return 3+u[2]+2*u[1]+t
  12.  
  13.  
  14. F=[f0,f1,f2]
  15. U=[0.0,0.0,0.0]
  16.  
  17. results=[]
  18. results.append(U)
  19.  
  20. dt=0.1
  21. t=0
  22.  
  23. T=1
  24.  
  25. while t<=T:
  26.     Utemp=[]
  27.     for u,f in zip(U,F):
  28.         Utemp.append(u+dt*f(U,t))
  29.     t+=dt
  30.     U=Utemp[:]
  31.     results.append(U)
  32.  
  33. print results
  34.  
Of course, if the functions were always linear, then they could be represented by a matrix of coefficients, which might make it all a bit easier, but less general.

It might also be worth checking out the scipy ability to do this kind of thing (which is probably far superior to the above!). See for example here
May 11 '10 #4
Dunnomuch
5 New Member
I'm a little confused... You don't seem to use the Forward Euler methode...?
May 11 '10 #5
Glenton
391 Recognized Expert Contributor
It's U_{i+1} = U_i + dt.f(U,t), right?

Anyway, I was just trying to show you how to code an n-dimensional step forward with n functions and n variables, and a t variable. I was trying to get the FEM based on your first post, but if I haven't, perhaps you'll be able to do so and post back...

Let us know how it goes, and if you have any further questions.
May 11 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1889
by: qazmlp | last post by:
Is it a good style to re-forward declare the types, which were already forward declared in base header file, in derived class header file? // base.h class addrClass; class base { public:...
10
9076
by: Protoman | last post by:
Hi! I'm trying to compute Euler's totient function for an extremely simple RSA program I'm writing. How, exactly, is it calculated? Thanks!
6
8596
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
7
6845
by: dkultasev | last post by:
Hello, could anyone help with euler circuit ? I tried to find it, found some, but they are not working. Tried to write my own one, but it supposed to be slow with big graphs because it trying all...
1
3299
by: luna18 | last post by:
i like to do programming but i am not a computer student.. =) i m trying to write a program to determine a euler circuit.but end up all stuck... i tyr to take the input as graph and if it is a...
25
3754
by: jwrweatherley | last post by:
I'm pretty new to python, but am very happy with it. As well as using it at work I've been using it to solve various puzzles on the Project Euler site - http://projecteuler.net. So far it has not...
8
2558
by: bruno_guedesav | last post by:
This has ocurred before, but if the person had find a way to solve it or not, I've got no clue. So, here I am to ask for help. I've created a form via pure PHP, basically a bunch of prints...
4
2286
by: process | last post by:
I am trying to solve project euler problem 18 with brute force(I will move on to a better solution after I have done that for problem 67). http://projecteuler.net/index.php?section=problems&id=18 ...
8
3846
by: Mathismylove | last post by:
Hi. Im new to Java and attempted to increase my knowledge by working on Project Euler..I figured that If Im able to solve as many problems from the project as possible with Java I will get more...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6758
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7010
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4499
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3011
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.