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.




2018年3月1日 星期四

Random Number Application in System Testing


Random Number Application in System Testing


Ching-Tang Tseng
Hamilton, New Zealand
2 March 2018
ilikeforth@gmail.com

The best and fast method to generate mass data for testing is using random number. At the end of mathematic system development must to do a lot of testing, especially for all data structures. Recently, I added my ABC FORTH to a Lina64 bare Forth system already. Testing is in progress. This article shows you how to use random number to test my system?

First, I have to explain a few special data formats and some special usages of words for this system. I must emphasize also: Many aspects in my system violate current so called FORTH Standard totally. There are a pure software floating point system and an extended complex number system to be included in my system. Besides, both of them are able to be used in BASIC style programming condition as well.

Only two kinds of number format can be accepted in this system. One is the general integer number without decimal point. Another one is the general double integer number with a decimal point. Then, a floating point number must be expressed as 1.2345 E -3 such a format, E is separated from the front double number and E is separated from the following integer also. This insistence has its advantage. It simplified a lot converting treatment for the input of number. Therefore, the output format of a floating point number must be the same as an input format. A complex number is composed by two floating point number. Its expressive format follows this regulation as well.

In Forth, 'i' is a primitive word. In BASIC style program, 'i' is always to be used as an index. In complex number expression, 'i' is a necessary mark. They are disturbing each other when you are using them. For the sake of simplification, all 'i' in the complex number are replaced by 'ii'. “ ii “ is not only to be used as a complex number output mark, but also is a high level defined word while data input.

All random numbers are coming out from the unique integer random number generator. I am not going to discuss its generating mechanism here. Let us just suppose it works well.

Integer random number generator kernel word named RANDOM. It supports its following application word named RAND. Their stack description is expressed as following:
RANDOM ( -- n )
RAND ( n1 – n2 )
The same, floating point random number and complex random number should be:
FRANDOM ( -- r )
FRAND ( r1 – r2 )
ZRANDOM ( -- z )
ZRAND ( z1 – z2 )

n1, r1, z1 are upper bound of generating range in each kind of random number. In INTEGER mode condition, only positive random number will be generated. To get a negative integer random number need to do a NEGATE operation.

In BASIC environment, three random number functions are using the same name: RND. It is impossible to be confused by each other when you are using RND in each environment.
In general, to do RANDOMIZE once before the random number to be used can improved the random effect. Based on the fast changing content in a hardware counter beside CPU, this instruction was designed.

Many testing had been done on all BASIC style data structures. They were:

In Integer: INTEGER, INTEGERS, (ARRAY), (MATRIX), (TENSOR)
In Floating point: REAL, REALS, ARRAY, MATRIX, TENSOR
In complex: COMPLEX, COMPLEXS (gave such a name on purpose), [ARRAY], [MATRIX], [TENSOR]

I am unable to list out all testing results with one article. I select the most complicated data structure of a complex tensor: [TENSOR] then to do data accessing. Please pay attention to all index 0 parts. Such a data structures let all index operation can be started from 0. Memory saving requirement are not a worth concerned problem.

There were no topics about execution speed or precision to be discussed in this article. All sub-systems were designed for personal using only. Let the last achievement apt to reuse on a traditional Forth system even after 50 years is my main goal. And system ability should be useful in solving all mathematical calculation problems. I am certainly sure, base on Lina64 Forth system, hundreds or even thousands digits precision floating point sub-system could be implemented out in the near future. I am not in a hurry to do it.

When you are looking at BASIC style code, please pay more attention on the mathematical expressed format, and try to feel:

How to let the system can do solving on it?

Many people don’t like BASIC style code. It doesn’t matter in my ABC FORTH. You are able to write your code in FORTH style still. I didn’t deleted anything belongs to original FORTH base system. So there are some mixing code of FORTH and BASIC to be arranged in the definition of MAIN on purpose.

These codes and its execution result record video are showing as following:


\ random number test

\ beware! In integer mode, need a NEGATE operation to get negative random number
: NTEST 10 0 DO CR 10 0 DO 100 RAND NEGATE 4 .R LOOP LOOP ;
: FTEST 10 0 DO CR -1.0 E 2 FRAND F. LOOP ;
\ beware! In FORTH style code, upper bound input format ignore + and ii
: ZTEST 10 0 DO CR -1.0 E 2 1.0 E 1 ZRAND Z. LOOP ;

3 integers i j k
complex z0
3 3 3 [tensor] zzz

: init
basic
10 let z{ z0 = 0.0 E 0 + 0.0 E 0 ii }z
20 for i = 1 to 3
30 for j = 1 to 3
40 for k = 1 to 3
\ attention! this is the usage to create a complex random number
50 let z{ zzz ( i j k ) = RND ( 1.0 E 2 - 1.0 E 1 ii ) }z
60 next k
70 next j
80 next i
90 end ;

: MAIN
randomize
ntest cr ftest cr ztest cr
init
basic
10 for i = 0 to 3
20 for j = 0 to 3
30 for k = 0 to 3
40 print " zzz( " ; i ; j ; k ; " )= " ; z{ zzz ( i j k ) }z
50 let z{ z0 = z0 + zzz ( i j k ) }z
60 next k
70 next j
80 next i
90 print cr cr " z0 = " ; z{ z0 }z
100 end
cr ." --- The end of testing --- "
;



My personal testing remarks: all random number generator functions approved