473,546 Members | 2,644 Online
Bytes | Software Development & Data Engineering Community
+ 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 4510
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
1899
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: virtual void setAddress(addrClass* node);
10
9077
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
8605
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 myfile.h template<typename T,typename R> class some_class;
7
6849
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 possibles situations. Prefer to get something on c++ or java, but other languages are welcome also. Tnanks
1
3305
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 euler circuit it will output an euler circuit. can any1 give me some starting point or hints ? thanks for viewing...
25
3764
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 let me down, but it has proved surprisingly slow on one puzzle. The puzzle is: p is the perimeter of a right angle triangle with integral length...
8
2563
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 together making a form. But somthing strange goes on: the form will only pass it's fields forward under method GET, but not on POST. Here's a snippet...
4
2292
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 However I can't get the recursive function right. I always have to use return right? Unless I am printing? So I canät stack to diffferent...
8
3848
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 familiar with its syntax and the possibilities that lie therein... Im in problem no 1 Problem no 1: Find the sum of numbers between 0 and 1000...
0
7504
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7435
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7694
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7461
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7792
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5080
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1046
muto222
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.