Python, RungeKutta and first-order differential equation system.

Click For Summary
SUMMARY

The discussion focuses on implementing a function in Python to solve a first-order differential equation system using the Runge-Kutta method and Forward Euler method. The equation is defined as mv' + f(v) + s(u) = F(t), with initial conditions u(0) = U0 and v(0) = V0. The provided solution for the right-hand side function, rhs(u, t), utilizes global functions for friction, spring, and external forces. The numerical methods are applied with a time step of dt = π/20 to solve the equation u'' + u = 0, yielding the analytical solution u(t) = cos(t).

PREREQUISITES
  • Understanding of first-order differential equations
  • Familiarity with Python programming and function definitions
  • Knowledge of numerical methods, specifically Runge-Kutta and Forward Euler
  • Experience with Python libraries for scientific computing, such as SciTools
NEXT STEPS
  • Implement and test the 4th-order Runge-Kutta method in Python
  • Explore the Forward Euler method for solving ordinary differential equations
  • Learn about the SciTools library for array manipulation and plotting
  • Review the provided ODESolver module for additional numerical methods
USEFUL FOR

Students and professionals in applied mathematics, physicists, and software developers working on simulations involving differential equations and numerical analysis.

MaxManus
Messages
268
Reaction score
1
Make a function:
Code:
def rhs(u,t):
for returning the right-hand side of the first-order differential equation system
from Exercise 11.36. As usual, the u argument is an array or list with the two
solution components u[0] and u[1] at some time t. Inside rhs, assume that
you have access to three global Python functions friction(dudt), spring(u),
and external(t) for evaluating f(u˙ ), s(u), and F(t), respectively.


Homework Equations



The equation is, given to me by lurflurf,:
mv' + f(v ) + s(u) = F(t), t > 0, u(0) = U0, v (0) = V0
where v=u'
For the record the equation was:
mu'' + f(u' ) + s(u) = F(t), t > 0, u(0) = U0, u' (0) = V0 .

Test the rhs function in combination with the functions f(u' ) = 0, F(t) = 0, s(u) = u, and the choice m = 1. The differential equation then reads
u'' + u = 0. With initial conditions u(0) = 1 and u'(0) = 0, one can show
that the solution is given by u(t) = cos(t). Apply two numerical methods:
the 4th-order RungeKutta method and the Forward Euler method from the
ODESolver module developed in Chapter 11.4, using a time step dt = pi /20.


The Attempt at a Solution



Code:
 def rhs(u, t):
    return [u[1],
            (1./m)*(external(t) - friction(u[1]) - spring(u[0]))]

This is the solution, but I don't get how yuu go from the equation to rhs.
Can someone help me?
 
Technology news on Phys.org


Here is the page with the exercises: http://www.ifi.uio.no/~inf1100/ODE_project.pdf"

The equation is given in 11.36 and it is 11.37 I need help with.
The solution is http://www.ifi.uio.no/~inf1100/live-programming/oscillator_v1.py"

ODESolver http://www.ifi.uio.no/~inf1100/src/oo/ODESolver.py"

Scitools contains array, plot etc
 
Last edited by a moderator:

Similar threads

  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 50 ·
2
Replies
50
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
1
Views
2K