Make a function

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.

The equation is:
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 soltion is:

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

can someone help me understand why?

Recommended Answers

All 2 Replies

It's very simple, when you write a 2nd order equation as a first order system, the technique is to introduce the vector (u, v) = (u, u') and to compute its derivative. You have (u, v)' = (u', v') = (v, u"). Now you replace u" by (1/m)(F(t) - f(u') - s(u)). Now, f(u') = f(v) because u'=v, and finally you get (u, v)' = (v, (1/m)(F(t) - f(v) - s(u))).
When you program this, you replace the vector (u,v) by a list u = [u[0], u[1]], and the right hand side is the list [u[1], (1/m)*(F(t) - f(u[1]) - s(u[0])]. Note that this expression of the second order equation as a first order system comes before any numerical scheme is applied. It's pure theory. The hard part is the Runge Kutta method which you have to implement on the system !

Thanks, you saved my life.
2 days to the exam.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.