# Programs

## Mathematics Programs

### First Degree Ordinary Differential Equation

#### Summary

Programs:

SLOPFIEL

Version:

4.0, 4 September 2001

Description:

This program draws a slope field for a function of two variables, and optionally graphs the solution y(x) to y'(x)=f(x,y(x)) for specific initial values (x,y).

Compatibility:

 CFX-9850G Compatible with CFX-9850G FX-7400G Compatible with FX-7400G

#### Detailed Description

The solution to the differential equation dy(x)/dx = f(x,y(x)) depends on both the initial value (x0,y0) and the behavior of the function f. One way of visualizing how solutions y(x) will behave for a variety of initial values (x,y) is to display the slope field of the function f — that is, to represent the value of the function f at a point (x,y) by a line segment with slope f(x,y). Should the solution y(x) pass through the point (x,y), y(x) will have the same slope at (x,y) as the line segment. One can visualize the overall behavior of the solution y(x) by tracing from a point to a nearby point through the slope line.

Of course, another way to visualize how solutions y(x) will behave for a variety of initial values (x,y) is to solve the differential equation and graph the function y(x).

The program SLOPFIEL embodies both of these approaches. It optionally draws the slope field of a function f(x,y), then repeatedly graphs the solution y(x) to the equation y'(x)=f(x,y(x)) for specified initial values (x,y). The initial values need not be included in the view window; however, only those portions of the solution y(x) that are in the view window will be graphed. The solution y(x) is computed numerically using a fourth degree Runge-Kutta method with a step size of one X pixel width.

Program SLOPFIEL draws the slope field for the function f(x,y) for a set of (x,y) values determined by the current graphics view window. The X values of the points for which line segments are drawn are those on the grid determined by the Xmin, Xmax, and Xscale values of the graphics view window; similarly for Y.

#### Usage

Load the programs SLOPFIEL and DRAWLINE. Program the function f(x,y) into program F1. F1 must take its x value from variable X, its y value from variable Y, and leave its result in Ans. F1 must not destroy any of the variables used by program SLOPFIEL, as described in the “Resources Used” section.

Note that there are CFX-9850G and FX-7400G specific versions of the programs SLOPFIEL and DRAWLINE. You can use the 7400 version in the 9850, although with some loss of graphics accuracy. You cannot use the 9850 version in the 7400.

Set the graphics view window to the region of interest. Finally, run the program SLOPFIEL. The slope field will be displayed on the graphics screen.

The program will first inquire whether the slope field is desired. It issues the prompt:

```Field (0->no,1->yes)?
```

Enter 0 if the slope field is not to be plotted, or 1 if the slope field is to be plotted. The program then will ask for an initial value (x,y) with the prompts

```X0?
Y0?
```

In response to these prompts, enter the initial x value and the initial y value. After the program graphs the solution, it will ask for another initial value (x,y). Enter all initial values desired.

#### Program Source

The program is available for the CFX-97850G as a text file with .CAT contents, or may be entered as shown below. The program is also available for the FX-7400G as a text file with .CAT contents, or may be entered as shown below. A semicolon (“;”) marks the beginning of a comment, and is not to be entered into the calculator. Remember that these programs are copyrighted; see the copyright issues page for limitations on redistribution.

Program DRAWLINE (381 bytes) (9850 version):

```; Variables:
;   A is X value of endpoint 1
;   B is Y value of endpoint 1
;   C is X value of endpoint 2
;   D is Y value of endpoint 2
;   E is color code (1=orage, 2=green, other=blue)
;   F is utility variable
; Symbols:
;   < is less than relational
;   > is greater than relational
;   -> is assignment arrow
;   / is divide operator
;   = is equal to relational

; The following is all one line. It is split here
;   simply to make it fit
If (A<Xmin And C<Xmin) Or (A>Xmax And C>Xmax) Or
(B<Ymin And D<Ymin) Or (B>Ymax And D>Ymax)
; Test the entire line is out of range
Then Return        ;   If so, simply return
IfEnd              ; [End of test if line is out of range]
If A>C             ; Test if X values are out of order
Then A->F          ;   If so, swap
C->A               ;     X
F->C               ;       values
B->F               ;   Swap
D->B               ;     Y
F->D               ;       values
IfEnd              ; [End of test if X values are out of order]
If A<Xmin          ; Test if lower X is out of range
Then B+(D-B)(Xmin-A)/(C-A)->B
;   If so, compute intercept Y
Xmin->A            ;   And intercept X
IfEnd              ; [End of test if lower X is out of range]
If C>Xmax          ; Test if upper X is out of range
Then D-(D-B)(C-Xmax)/(C-A)->D
;   If so, compute intercept Y
Xmax->C            ;   And intercept X
IfEnd              ; [End of test if upper X is out of range]
If B>D             ; Test if Y values are out of order
Then A->F          ;   If so, swap
C->A               ;     X
F->C               ;       values
B->F               ;   Swap
D->B               ;     Y
F->D               ;       values
IfEnd              ; [End of test if Y values are out of order]
If B<Ymin          ; Test if lower Y is out of range
Then A+(C-A)(Ymin-B)/(D-B)->A
;   If so, compute intercept X
Ymin->B            ;   And intercept Y
IfEnd              ; [End of test if lower Y is out of range]
If D>Ymax          ; Test if upper Y is out of range
Then C-(C-A)(D-Ymax)/(D-B)->C
;   If so, compute intercept X
Ymax->D            ;   And intercept Y
IfEnd              ; [End of test if upper Y is out of range]
If E=1             ; If color choice is orange
Then Orange Plot A,B;  Then plot first point in orange
Orange Plot C,D    ;   Plot second point in orange
Orange Line        ;   Draw orange line
Else If E=2        ;   Otherwise if color choice is green
Then Green Plot A,B;     Then plot first point in green
Green Plot C,D     ;     Plot second point in green
Green Line         ;     Draw green line
Else Plot A,B      ;   Otherwise plot first point in blue
Plot C,D           ;     Plot second point in blue
Line               ;     Draw blue line
IfEnd              ;   [End of test if color green]
IfEnd              ; [End of test if color orange]
```

Program SLOPFIEL (664 bytes) (9850 version):

```; Variables:
;   A is X coordinate of first endpoint of line to draw
;   B is Y coordinate of first endpoint of line to draw
;   C is X coordinate of second endpoint of line to draw
;   D is Y coordinate of second endpoint of line to draw
;   E is color of line to draw
;   F is used by DRAWLINE
;   G is length factor for lines in slope field
;   H is X index for slope field
;   I is Y index for slope field, delta X for Runge-Kutta
;   J is X delta for slope line, starting X value for Runge-Kutta
;   K is Y delta for slope line, starting Y value for Runge-Kutta
;   L is first value for Runge-Kutta
;   M is second value for Runge-Kutta
;   N is third value for Runge-Kutta
;   O is fourth value for Runge-Kutta
;   X is X value for function
;   Y is Y value for function

;
;   Phase 1: Draw slope field
;
0->E               ; Draw field in blue
Xscl->G            ; Get X scale factor
If Yscl<G          ; If Y scale factor is less than X scale factor
Then Yscl->G       ;   Get Y scale factor
IfEnd              ; [End of test of scale factor]
"Field (0->no,1->yes)"?->A
; Inquire whether to do slope field
If A<>0            ; Test whether to do slope field
Then Int (Xmin/Xscl)->H
;   If so, get lower bound on X index
HXscl<Xmin=>H+1->H ;   If too low, bump up by one
Do                 ;   Repeat for each X grid value
Int (Ymin/Yscl)->I ;     Get lower bound on Y index
IYscl<Ymin=>I+1->I ;     If too low, bump up by one
Do                 ;     Repeat for each Y grid value
HXscl->X           ;       Figure out X value
IYscl->Y           ;       Figure out Y value
Prog "F1"          ;       Get the function value
Ans->K             ;       Store function value in K
sqrt(1+Kx^2)x^-1->J;       Figure out X delta
JK->K              ;       Figure out Y delta
.3GJ->J            ;       Scale X delta
.3GK->K            ;       Scale Y delta
X-J->A             ;       Figure endpoint
Y-K->B             ;         one for slope line
X+J->C             ;       Figure endpoint
Y+K->D             ;         two for slope line
Prog "DRAWLINE"    ;       Draw slope line
I+1->I             ;       Increment Y grid index
Int (Ymax/Yscl)->J ;       Figure limit on Y grid index
JYscl>Ymax=>J-1->J ;       If too big, bump down by 1
LpWhile I<=J       ;     [End of Y grid value loop]
Int (Xmax/Xscl)->J ;     Figure limit on X grid index
JXscl>Xmax=>J-1->J ;     If too big, bump down by 1
H+1->H             ;     Increment X grid index
LpWhile H<=J       ;   [End of X grid value loop]
IfEnd              ; [End of test to do slope field]
;
;   Phase 2: Solve ODE and draw solution
;
1->E               ; Draw solutions in orange
While 1            ; Loop getting starting points
"X0"?->G           ;   Inquire starting X value
"Y0"?->H           ;   Inquire starting Y value
(Xmax-Xmin)/126->I ;   Get delta X for Runge-Kutta
;
;   Phase 2a: Solve forward until Xmax exceeded
;
G->J               ;   Copy starting X value
H->K               ;   Copy starting Y value
While J<Xmax       ;   Loop until Xmax exceeded
J->X               ;     Set X value
K->Y               ;     Set Y value
Prog "F1"          ;     Get function value
IAns->L            ;     Save function value
J+I/2->X           ;     Set next X value
K+L/2->Y           ;     Set next Y value
Prog "F1"          ;     Get function value
IAns->M            ;     Save function value
J+I/2->X           ;     Set the next X value
K+M/2->Y           ;     Set the next Y value
Prog "F1"          ;     Get function value
IAns->N            ;     Save function value
J+I->X             ;     Set the next X value
K+N->Y             ;     Set the next Y value
Prog "F1"          ;     Get function value
IAns->O            ;     Save function value
J->A               ;     Set starting X value
K->B               ;     Set starting Y value
J+I->J             ;     Set ending X value
K+(L+2M+2N+O)/6->K ;     Set ending Y value
J->C               ;     Save ending X for next start
K->D               ;     Save ending Y for next start
Prog "DRAWLINE"    ;     Draw the function graph segment
WhileEnd           ;   [End of loop until Xmax exceeded]
;
;   Phase 2b: Solve backwards until Xmin exceeded
;
G->J               ;   Exactly as above
H->K
While J>Xmin
J->X
K->Y
Prog "F1"
IAns->L
J-I/2->X
K-L/2->Y
Prog "F1"
IAns->M
J-I/2->X
K-M/2->Y
Prog "F1"
IAns->N
J-I->X
K-N->Y
Prog "F1"
IAns->O
J->A
K->B
J-I->J
K-(L+2M+2N+O)/6->K
J->C
K->D
Prog "DRAWLINE"
WhileEnd           ;   [End of loop until Xmin exceeded]
WhileEnd           ; [End of loop getting starting points]
```

Program DRAWLINE (315 bytes) (7400 version):

```; Variables:
;   A is X value of endpoint 1
;   B is Y value of endpoint 1
;   C is X value of endpoint 2
;   D is Y value of endpoint 2
;   E is color code (ignored)
;   F is utility variable
; Symbols:
;   > is greater than relational
;   -> is assignment arrow
;   < is less than relational
;   / is divide operator

If A>C             ; Test if X values are out of order
Then A->F          ;   If so,
C->A               ;     swap X
F->C               ;       values
B->F               ;   Swap
D->B               ;     Y
F->D               ;       values
IfEnd              ; [End of test if X values are out of order]
If A>Xmax          ; If the two points
Then Return        ;   are both completely
IfEnd              ;     out of the graphing
If C<Xmin          ;       range, then
Then Return        ;         return without
IfEnd              ;           drawing anything
If A<Xmin          ; Test if lower X is out of range
Then B+(D-B)(Xmin-A)/(C-A)->B
;   If so, compute intercept Y
Xmin->A            ;   And intercept X
IfEnd              ; [End of test if lower X is out of range]
If C>Xmax          ; Test if upper X is out of range
Then D-(D-B)(C-Xmax)/(C-A)->D
;   If so, compute intercept Y
Xmax->C            ;   And intercept X
IfEnd              ; [End of test if upper X is out of range]
If B>D             ; Test if Y values are out of order
Then A->F          ;   If so,
C->A               ;     swap X
F->C               ;       values
B->F               ;   Swap
D->B               ;     Y
F->D               ;       values
IfEnd              ; [End of test if Y values are out of order]
If B>Ymax          ; If the two points
Then Return        ;   are both completely
IfEnd              ;     out of the graphing
If D<Ymin          ;       range, then
Then Return        ;         return without
IfEnd              ;           drawing anything
If B<Ymin          ; Test if lower Y is out of range
Then A+(C-A)(Ymin-B)/(D-B)->A
;   If so, compute intercept X
Ymin->B            ;   And intercept Y
IfEnd              ; [End of test if lower Y is out of range]
If D>Ymax          ; Test if upper Y is out of range
Then C-(C-A)(D-Ymax)/(D-B)->C
;   If so, compute intercept X
Ymax->D            ;   And intercept Y
IfEnd              ; [End of test if upper Y is out of range]
Plot A,B           ; Plot the
Plot C,D           ;   endpoints
Line               ; Plot the line
```

Program SLOPFIEL (663 bytes) (7400 version):

```; Variables:
;   A is X coordinate of first endpoint of line to draw
;   B is Y coordinate of first endpoint of line to draw
;   C is X coordinate of second endpoint of line to draw
;   D is Y coordinate of second endpoint of line to draw
;   E is color of line to draw
;   F is used by DRAWLINE
;   G is length factor for lines in slope field
;   H is X index for slope field
;   I is Y index for slope field, delta X for Runge-Kutta
;   J is X delta for slope line, starting X value for Runge-Kutta
;   K is Y delta for slope line, starting Y value for Runge-Kutta
;   L is first value for Runge-Kutta
;   M is second value for Runge-Kutta
;   N is third value for Runge-Kutta
;   O is fourth value for Runge-Kutta
;   X is X value for function
;   Y is Y value for function

;
;   Phase 1: Draw slope field
;
0->E               ; Draw field in blue
Xscl->G            ; Get X scale factor
If Yscl<G          ; If Y scale factor is less than X scale factor
Then Yscl->G       ;   Get Y scale factor
IfEnd              ; [End of test of scale factor]
"Field (0->no,1->yes)"?->A
; Inquire whether to do slope field
If A<>0            ; Test whether to do slope field
Then Int (Xmin/Xscl)->H
;   If so, get lower bound on X index
HXscl<Xmin=>H+1->H ;   If too low, bump up by one
Do                 ;   Repeat for each X grid value
Int (Ymin/Yscl)->I ;     Get lower bound on Y index
IYscl<Ymin=>I+1->I ;     If too low, bump up by one
Do                 ;     Repeat for each Y grid value
HXscl->X           ;       Figure out X value
IYscl->Y           ;       Figure out Y value
Prog "F1"          ;       Get the function value
Ans->K             ;       Store function value in K
sqrt(1+Kx^2)x^-1->J;       Figure out X delta
JK->K              ;       Figure out Y delta
.3GJ->J            ;       Scale X delta
.3GK->K            ;       Scale Y delta
X-J->A             ;       Figure endpoint
Y-K->B             ;         one for slope line
X+J->C             ;       Figure endpoint
Y+K->D             ;         two for slope line
Prog "DRAWLINE"    ;       Draw slope line
I+1->I             ;       Increment Y grid index
Int (Ymax/Yscl)->J ;       Figure limit on Y grid index
JYscl>Ymax=>J-1->J ;       If too big, bump down by 1
LpWhile I<=J       ;     [End of Y grid value loop]
Int (Xmax/Xscl)->J ;     Figure limit on X grid index
JXscl>Xmax=>J-1->J ;     If too big, bump down by 1
H+1->H             ;     Increment X grid index
LpWhile H<=J       ;   [End of X grid value loop]
IfEnd              ; [End of test to do slope field]
;
;   Phase 2: Solve ODE and draw solution
;
1->E               ; Draw solutions in orange
While 1            ; Loop getting starting points
"X0"?->G           ;   Inquire starting X value
"Y0"?->H           ;   Inquire starting Y value
(Xmax-Xmin)/78->I ;   Get delta X for Runge-Kutta
;
;   Phase 2a: Solve forward until Xmax exceeded
;
G->J               ;   Copy starting X value
H->K               ;   Copy starting Y value
While J<Xmax       ;   Loop until Xmax exceeded
J->X               ;     Set X value
K->Y               ;     Set Y value
Prog "F1"          ;     Get function value
IAns->L            ;     Save function value
J+I/2->X           ;     Set next X value
K+L/2->Y           ;     Set next Y value
Prog "F1"          ;     Get function value
IAns->M            ;     Save function value
J+I/2->X           ;     Set the next X value
K+M/2->Y           ;     Set the next Y value
Prog "F1"          ;     Get function value
IAns->N            ;     Save function value
J+I->X             ;     Set the next X value
K+N->Y             ;     Set the next Y value
Prog "F1"          ;     Get function value
IAns->O            ;     Save function value
J->A               ;     Set starting X value
K->B               ;     Set starting Y value
J+I->J             ;     Set ending X value
K+(L+2M+2N+O)/6->K ;     Set ending Y value
J->C               ;     Save ending X for next start
K->D               ;     Save ending Y for next start
Prog "DRAWLINE"    ;     Draw the function graph segment
WhileEnd           ;   [End of loop until Xmax exceeded]
;
;   Phase 2b: Solve backwards until Xmin exceeded
;
G->J               ;   Exactly as above
H->K
While J>Xmin
J->X
K->Y
Prog "F1"
IAns->L
J-I/2->X
K-L/2->Y
Prog "F1"
IAns->M
J-I/2->X
K-M/2->Y
Prog "F1"
IAns->N
J-I->X
K-N->Y
Prog "F1"
IAns->O
J->A
K->B
J-I->J
K-(L+2M+2N+O)/6->K
J->C
K->D
Prog "DRAWLINE"
WhileEnd           ;   [End of loop until Xmin exceeded]
WhileEnd           ; [End of loop getting starting points]
```

Page last updated 25 November 2001.

# Brian’s Casio Calculator Corner

Home

Programs

Index

Mathematics

Index

Factorization

Bisection

Newton’s Method

Complex Zeroes

Best Fraction Approximation

Pseudorandom Numbers

Romberg Integration

First Degree ODE

Rounding

Permutations

Finance

Physics

Utility

Tutorial

Puzzles

Site Information