2018年3月15日 星期四

Taylor-series method for solving an initial value problem


Taylor-series method for solving an initial value problem

Ching-Tang Tseng
Hamilton, New Zealand
16 March 2018
http://forthfornight.blogspot.com

This topic belongs to the level of the university student. Maybe it is the simplest method could be applied to solve an initial value problem of ODE (Ordinary Differential Equation). The detailed explanation of this method could be found from every numerical analysis textbooks. I cited it from the following book and used it to test my ABC FORTH system features.

David Kincaid and Ward Cheney, “Numerical analysis: mathematics of scientific computing”, Brooks/Cole Publishing Company, 1990, p.491

There was an example of initial-value problem supplied in section 8.2, on page 496. I followed this example to design out my program in ABC FORTH system. For the convenient purpose, some related information in this textbook must be re-summarized here first.

Example question:
Ordinary differential equation and its initial condition expressed as equation (1) and (2).

X’ = cos t – sin x + t ^ 2  ---------- (1)
X(-1) = 3 ---------------------------- (2)

Try to use Taylor series method order 4 to solve this initial value problem.
Assume 200 steps are progressing over the range from t = -1 to t = +1, i.e. let h =( ( 1 - (-1) ) / 200 ) = 2 / 200 = 0.01. Then x =?

Fig. 1 is a diagram to describe this question.


Taylor-series for x can be expressed as (3)

x(t+h) = x(t) + h* x’(t) + (h^2/2!)*x’’(t) + (h^3/3!)*x’’’(t) + (h^4/4!)*x’’’’(t) + … (3)

The derivatives can be obtained from the differential equation in (1).
They are

x’’ = - sin t – x’ cos x + 2*t
x’’’ = -cos t – x’’ cos x + ( x’ ) ^2 * sin x + 2
x’’’’ = sint – x’’’ cos x +3*x’*x’’*sin x + ( x’)^3*cos x

Then, this is an algorithm to solve this initial-value problem in (1), starting at t = -1 and progressing in steps of h = 0.01. We desire a solution in the t-interval [-1,1] and thus must take 200 steps.
A pseudo programming code can be expressed as following:

Input: M < = = 200 ; h < = = 0.01 ; t < = = -1.0 ; x < = = 3.0
Output: t, x
For k = 1, 2, … M do
x’ < = = cos t – sin x + t^2
x’’ < = = - sin t – x’ cos x + 2t
x’’’ < = = - cos t – x’’ cos x + (x’)^2 * sin x = 2
x’’’’ < = = sin t + ((x’)^3 – x’’’) cos x + 3x’x’’ sin x
x < = = x + h ( x’ + h/2(x’’+ h/3(x’’’ + h/4(x’’’’)))
x < = = t+h
Output t, x
End

My real program in ABC FORTH is

\ Taylor series method to solve ODE (order 4) in Lina64
2 integers k m
7 reals h t x x1 x2 x3 x4

: taylor
basic

10 let m = 200
20 let { h = 0.01 e 0 }
30 let { t = -1.0 e 0 }
40 let { x = 3.0 e 0 }
50 print " " ; { t , x }

100 for k = 1 to m
110 let { x1 = cos ( t ) - sin ( x ) + t ** 2.0 e 0 }
120 let { x2 = negate ( sin ( t ) ) - x1 * cos ( x ) + 2.0 e 0 * t }
130 let { x3 = negate ( cos ( t ) ) - x2 * cos ( x ) + ( x1 * x1 ) * sin ( x ) + 2.0 e 0 }
140 let { x4 = sin ( t ) + ( x3 ** 3.0 e 0 ) * cos ( x ) + 3.0 e 0 * x1 * x2 * sin ( x ) }
150 let { x = x + h * ( x1 + ( h / 2.0 e 0 ) * ( x2 + ( h / 6.0 e 0 ) * ( x3 + ( h / 24.0 e 0 ) * x4 ) ) ) }
160 let { t = t + h }
170 print " " ; { t , x }
200 next k

210 end ;

Instruction “taylor” will resulted in a huge amount of printing output for total 200 sets data. I reserved it in the real video demonstration at the end of this article. Besides, to express this result as an x versus t diagram could let me easier to understand the tendency of curve variation with respect to the ordinary differential equation (1). Another graphic generating program executed in Win32Forth system under Ubuntu V16.04 OS was showing in the video as well. But total needed data for plotting was transferred via a pure text file. In this demonstration, I am still using highlight, copy then post operations to create such a file. It could be implemented automatically by program. For the purpose of simplifying my demo program as above, I didn’t do it.

Fig. 2 shows the tendency of solution.


Since 1990, I knew how to extend FORTH to ABC FORTH, other mathematics programming languages were no longer to be used by me. This article shows out all the reasons. ABC FORTH is my researching tool. This system has been used by me for many years already. It had helped me done a lot of things that other people was hard to do.

My pure high level defined floating point system in Lina64 was built based on 64 bits integer operation. All functions inside were gotten based on calculation of Taylor series expansion equation. 18 digits +, -, *, / operations could supplied 15 digits precision out only. As to the hardware co-processor, all functions were built based on 80 bits operation. Its precision, of course, should be better than my pure software results.

Taylor series method for solving initial value problem is simple and easy to understand. And it can be thought as a first step for doing the research of initial value problem. I was able to create other kinds of ordinary differential equation, to use them as other examples, but I didn’t. All executing results in this demonstration let us have the opportunity to do a detailed comparison with the data to be listed in the book. As regards the other topics, such as error analysis, advantages and disadvantages, using restrictions…etc, please refer to the book. I am not going to discuss it here. ABC FORTH could be applied to all numerical analysis textbooks and to solve all mathematical problems.

Lina64 can do the music sound playing control as well. A public domain breezy music “Little-waltz” downloads from website:

http://www.orangefreesounds.com/

I am using Lina64 to play it as background music in this video.

My personal testing remarks: intrinsic functions sin, cos, ** approved.




沒有留言: