Tutorial 030 Simultaneous Equations for All!
The following program has evolved to be an educational "toy" aiming
to give practice to budding mathematicians in graph work and/or algebraic
mental manipulation. You can choose whether or not to have the aid of a graph
in solving two simple simultaneous equations. A surprising amount of program
lines is necessary to format equations into the expected "textbook notation",
e.g. to avoid printing an equation as y = -1x + 7, but as y = -x + 7
The following output from the program has been obtained from the screen
by saving the text onto the clipboad using Ctrl-TAB and pasting it into
this web-page (compressed by deleting some screen space) :
y=y=y=y=y=y=y=y=y=y=y=y=y= SIMULTANEOUS
EQUATIONS! x=x=x=x=x=x=x=x=x=x=x=x=x=
Click to choose whether to
have the help of a graph (easier)
Hide Graph
Show Graph
Clicking the "hide graph" (hair shirt!) option gave the following
random question:
y=y=y=y=y=y=y=y=y=y=y=y=y= SIMULTANEOUS EQUATIONS! x=x=x=x=x=x=x=x=x=x=x=x=x=
Input values of x until the
y values are equal :
y = -x - 5 (1)
y = -2x + 4 (2)
(The following comes up in a scrolling text box,
some of which has scrolled away) and shows me in pure guessing mode until
I get the right answer; if you keep looking at the equations it is quite
instructive!) When you have guessed the correct x value the y values will
be equal and their difference will be zero!
x = 5 y(1)=
-10.000 y(2)= -6.000 difference= 4.000
x = 7 y(1)= -12.000
y(2)= -10.000 difference= 2.000
x = 9 y(1)= -14.000
y(2)= -14.000 difference= 0.000
You have solved
the simultaneous equations...Well done!
Press <
SPACE > for another go or < g > to change graph option
If you can subtact the two equations in your head
its possible to enter the answe as an expression (since we are using EVAL
to process the answer):
y=y=y=y=y=y=y=y=y=y=y=y=y= SIMULTANEOUS
EQUATIONS! x=x=x=x=x=x=x=x=x=x=x=x=x=
Input values of x until the
y values are equal :
y = 7x + 9 (1)
y = -5x - 8 (2)
x = -17/12 y(1)= -0.917 y(2)= -0.917
difference= 0.000
>> small enough!
You have solved
the simultaneous equations...Well done!
Press <
SPACE > for another go or < g > to change graph option
(Here I (mentally) subtracted eqn (2) from eqn(1)
to give
0 = 12x + 17
and then rearranged it to:
-17 = 12x
and then entered the answer as -17/12
Good practice!
If the graph option is chosen then you can see the
two graphs and read or estimate their crossing point, which gives you the
solution. I've got the program to plot your entered "guess" as coloured
points to see how close you are and aid approach to the solution with
acceptable accuracy. Its instructive to see the slopes of the lines and the
interecepts produced by the randomly chosen slope and intercept parameters,
m and c.
New techniques introduced here are :
- Use of VDU28 to define a BBC Basic Text Window
- Use of VDU 24 to define a BBC Basic Graphics Window
Listing :
REM: Simultaneous
Equations Game v0.013
REM: Richard Weston 04 June 2003
:
MODE8:OFF
DIM by(3),m$(2),m(2),c(2)
COLOUR4,50,50,128
:
m$(1)=" Show Graph"
m$(2)=" Hide Graph"
choice=0
sf=30
bx=500
bw=210
bh=35
:
COLOUR6
PRINT'STRING$(13,"y=");
COLOUR1
PRINT" SIMULTANEOUS EQUATIONS!
";
COLOUR5
PRINT STRING$(13,"x=")
COLOUR1
PRINT'''TAB(8)"Click to choose whether
to have the help of a graph (easier)"
VDU5: REM print at graphics cursor
FOR i=1 TO 2
by(i)=500+(100*i)
PROCbox(bx,by(i),bw,bh)
PLOT0,8,32
PRINT m$(i)
NEXT i
:
VDU4
PRINT
:
clicked=FALSE
boxclicked=FALSE
:
REPEAT
PROCmouse
UNTIL boxclicked
REPEAT
PROCgame(choice)
UNTIL FALSE
:
END
:
DEF PROCmouse
MOUSE x,y,btn
FOR j=1 TO 3
IF btn=4 THEN
clicked=TRUE
IF x>bx THEN
IF
x<bx+bw THEN
IF y>by(j) THEN
IF y<by(j)+bh THEN
boxclicked=TRUE
choice=j
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
NEXT j
IF boxclicked=TRUE THEN PROCgame(choice)
ENDPROC
:
:
DEF PROCbox(x,y,a,b)
RECTANGLE x,y,a,b
ENDPROC
:
DEF PROCgame(choice)
MODE8:ON
guess=0
COLOUR6
PRINT'STRING$(13,"y=");
COLOUR1
PRINT" SIMULTANEOUS EQUATIONS!
";
COLOUR5
PRINT STRING$(13,"x=")
COLOUR3
PRINT'TAB(8)"Input values of x until
the y values are equal :"'
COLOUR7
PRINTTAB(0,15);
FOR i= 1 TO 2
REPEAT:m(i)=RND(19)-10:UNTIL
m(i)<>0 AND INT(m(i)+0.5)<>1 AND m(2)<>m(1)
REPEAT:c(i)=RND(19)-10:UNTIL
c(i)<>0
PRINTTAB(5)"y = ";
IF m(i)=-1 THEN PRINT" ";
IF m(i)<0 THEN PRINT"-";
ELSE PRINT" ";
IF m(i)<>-1 THEN PRINT;
ABS(m(i));
PRINT;"x";
IF c(i)>0 THEN PRINT"
+ ";
IF c(i)<0 THEN PRINT"
- ";
IF c(i) <> 0 THEN PRINT
;ABS(c(i));SPC(4);"(";i;")"'
NEXT
REM: Draw equation lines
PROCgraph
:
VDU28,0,31,79,26
REM: ^ Defines a Text Window
REPEAT
REM: Input "Guesses"
guess+=1
INPUT" x = "x$
x=EVAL(x$)
v=VPOS
ya=m(1)*x + c(1)
yb=m(2)*x + c(2)
REM: Plot points (x,ya) and
(x,yb)
IF choice=1 THEN
GCOL0,(guess+1)
PLOT69,x*sf,ya*sf
PLOT69,x*sf,yb*sf
ENDIF
@%=&20309
REM: &=hexadecimal; 2=fixed
no decimal places;
REM: 03 decimal places; 09
field width of 9 characters
diff=ABS(ya-yb)
COLOUR1
PRINTTAB(12,v-1) "y(1)= ";
COLOUR7
PRINT ;ya,;
COLOUR1
PRINT"y(2)= ";
COLOUR7
PRINT ;yb;
pos=POS
COLOUR1
PRINT" difference=
";
COLOUR7
PRINT;diff;" ";
IF choice=1 THEN
COLOUR(guess+1)
PRINT".";
ENDIF
PRINT
COLOUR7
IF diff<0.01 AND diff<>0
THEN
PRINTTAB(pos+13)">>
small enough!"
ENDIF
UNTIL diff < .01
REM: Acclamation!! :
COLOUR3:OFF
PRINTTAB(10)"You have solved the simultaneous
equations...Well done!"
PRINTTAB(10)" Press < SPACE > for
another go or < g > to change graph option"
G$=GET$
@%=10
REM: ^ restores default (Format 0 and
field width 10)
IF G$="g" OR G$="G" THEN RUN
ENDPROC
:
DEF PROCgraph
IF choice=1 THEN
ORIGIN 800,512
VDU 24,-300;-300;300;300;
REM: ^Defines a Graphics
Window
VDU5
GCOL0,4
FOR x=-10 TO 10
LINE x*sf,-300,x*sf,300
NEXT x
FOR y=-10 TO 10
LINE -300,y*sf,300,y*sf
NEXT y
GCOL0,7
LINE -300,0,300,0
LINE 0,-300,0,300
FOR x=-9 TO 9:LINE x*sf,-2,x*sf,2:NEXT
x
FOR y=-9 TO 9:LINE -2,y*sf,2,y*sf:NEXT
y
MOVE 280,-10:PRINT"x"
MOVE 10,290 :PRINT"y"
GCOL0,1
FOR i= 1 TO 2
ylow=-10*m(i)
+ c(i)
yhigh=10*m(i)
+ c(i)
LINE -10*sf,ylow*sf,10*sf,yhigh*sf
NEXT i
VDU4
ENDIF
ENDPROC
Annotated Listing
REM: Simultaneous Equations
Game v0.013
REM: Richard Weston 04 June 2003
:
MODE8:OFF
DIM by(3),m$(2),m(2),c(2) *** by = clickable
box position; m = line slope; c = line intercept on y-axis
COLOUR4,50,50,128 *** make blue more
subtle for graph grid
:
m$(1)=" Show Graph"
m$(2)=" Hide Graph"
choice=0
sf=30 *** scale factor for graph
bx=500 *** clickable box parameter
: x position
bw=210 ***
clickable box parameter : width
bh=35 *** clickable box parameter : height
COLOUR6
PRINT'STRING$(13,"y="); *** decoration
COLOUR1
PRINT" SIMULTANEOUS EQUATIONS!
"; *** title
COLOUR5
PRINT STRING$(13,"x=") *** decoration
COLOUR1
PRINT'''TAB(8)"Click to choose whether
to have the help of a graph (easier)"
VDU5: REM *** print at graphics cursor
FOR i=1 TO 2 *** two clickable choice
boxes : "to graph or not to graph, that is the question"
by(i)=500+(100*i) *** positions
clickable boxes
PROCbox(bx,by(i),bw,bh) ***
draw the clickable choice boxes
PLOT0,8,32 *** move relative
to last point
PRINT m$(i) *** graph or
not messages
NEXT i
:
VDU4 *** separate the graphics and text
cursors once more (default)
PRINT
:
clicked=FALSE *************** mouse handling
stuff ******************************
boxclicked=FALSE
:
REPEAT
PROCmouse
UNTIL boxclicked ************** end of
mouse stuff ******************************
REPEAT
PROCgame(choice) ***********
keep playing for ever if you want *********************
***********
choice specifies graph or not ************************
UNTIL FALSE
:
END ********************* Put procedure
definitions after the end statement ***********
:
DEF PROCmouse *********** recycled
from the hangman program - see notes there Tutorial 23
MOUSE x,y,btn
FOR j=1 TO 3
IF btn=4 THEN
clicked=TRUE
*** not in fact used in this particular program but it does no harm here
IF x>bx THEN
IF
x<bx+bw THEN
IF y>by(j) THEN
IF y<by(j)+bh THEN
boxclicked=TRUE
choice=j
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
NEXT j
IF boxclicked=TRUE THEN PROCgame(choice)
ENDPROC
:
:
DEF PROCbox(x,y,a,b) *** hardly worth
a procedure, but here for (attempted) readability
RECTANGLE x,y,a,b
ENDPROC
:
DEF PROCgame(choice)
MODE8:ON *** Mode statement clears the
screen and clears graphics and text windows set in a previous loop
guess=0
COLOUR6
PRINT'STRING$(13,"y="); *** decoration
etc repeated ..................
COLOUR1
PRINT" SIMULTANEOUS EQUATIONS!
";
COLOUR5
PRINT STRING$(13,"x=") ................................................................***
COLOUR3
PRINT'TAB(8)"Input values of x until
the y values are equal :"'
COLOUR7
PRINTTAB(0,15); *** position the text
cursor
FOR i= 1 TO 2
REPEAT:m(i)=RND(19)-10:UNTIL
m(i)<>0 AND INT(m(i)+0.5)<>1 AND m(2)<>m(1)
*** ^ I don't want m to be zero, or +1 , or the same for both lines
(or they will be parallel and not meet)
REPEAT:c(i)=RND(19)-10:UNTIL
c(i)<>0
*** ^ intercept non-zero or it gets too easy
PRINTTAB(5)"y = ";
IF m(i)=-1 THEN PRINT" ";
**** Print format fiddling to make things look neat................
IF m(i)<0 THEN PRINT"-";
ELSE PRINT" ";
IF m(i)<>-1 THEN PRINT;
ABS(m(i));
PRINT;"x";
IF c(i)>0 THEN PRINT"
+ ";
IF c(i)<0 THEN PRINT"
- ";
IF c(i) <> 0 THEN PRINT
;ABS(c(i));SPC(4);"(";i;")"' ...................................................****
NEXT
REM: Draw equation lines
PROCgraph
:
VDU28,0,31,79,26
REM: ^ Defines a Text Window
REPEAT
REM: Input "Guesses"
guess+=1
INPUT" x = "x$ *** enter
your "guess"
x=EVAL(x$) *** evaluates
an input such as -9/13
v=VPOS *** vertical screen
position
ya=m(1)*x + c(1) *** equations
to be solved! ***
yb=m(2)*x + c(2)
REM: Plot points (x,ya) and
(x,yb)
IF choice=1 THEN *** Graph
Option chosen
GCOL0,(guess+1)
PLOT69,x*sf,ya*sf
*** Plots the points calculated so you can see "how far off you" are
(if its not too far)
PLOT69,x*sf,yb*sf
ENDIF
@%=&20309 *** print formatting
....
REM: &=hexadecimal; 2=fixed
no decimal places;
REM: 03 decimal places; 09
field width of 9 characters
diff=ABS(ya-yb) *** difference
between the calculated y values so you can "close in" on the solution
COLOUR1 ************ PRINT
RESULTS OF CALCULATIONS IN TEXT WINDOW****
PRINTTAB(12,v-1) "y(1)= ";
COLOUR7
PRINT ;ya,;
COLOUR1
PRINT"y(2)= ";
COLOUR7
PRINT ;yb;
pos=POS *** text "x position"
COLOUR1
PRINT" difference=
";
COLOUR7
PRINT;diff;" ";
IF choice=1 THEN *** only
need if graph option chosen ***
COLOUR(guess+1)
*** colour moves on so diferent guesses stand out from each other
PRINT"."; ***
dot colour same as "graph dots"
ENDIF
PRINT
COLOUR7
IF diff<0.01 AND diff<>0
THEN *** Guess needs to be quite close but not unreasonably so
PRINTTAB(pos+13)">>
small enough!" *** pos used here to keep things neat when this message appears
ENDIF
UNTIL diff < .01 *** keep guessing
until this condition is met
REM: Acclamation!! :
COLOUR3:OFF
PRINTTAB(10)"You have solved the simultaneous
equations...Well done!"
PRINTTAB(10)" Press < SPACE > for
another go or < g > to change graph option"
G$=GET$
@%=10
REM: ^ restores default (Format 0 and
field width 10)
IF G$="g" OR G$="G" THEN RUN *** its
simplest to cut loose and run the whole program again in this case....
*** ... otherwise the procedure will end and the main game loop.....
REPEAT
PROCgame(choice) ***********
keep playing for ever if you want *********************
***********
choice specifies graph or not ************************
UNTIL FALSE
will repeat without
a graph being drawn...................................................***
ENDPROC
:
DEF PROCgraph
IF choice=1 THEN
ORIGIN 800,512 *** defines
centre of the graphics window
VDU 24,-300;-300;300;300;
*** a square, edge 300 graphics units; points outside this area do not show
REM: ^Defines a Graphics
Window
VDU5 *** print at graphics
cursor
GCOL0,4
FOR x=-10 TO 10 *** draws
graph grid vertical lines
LINE x*sf,-300,x*sf,300
NEXT x
FOR y=-10 TO 10 *** draws
graph grid horizontal lines
LINE -300,y*sf,300,y*sf
NEXT y
GCOL0,7
LINE -300,0,300,0 *** draws
x axis (white over the blue grid)
LINE 0,-300,0,300 *** draws
y axis
FOR x=-9 TO 9:LINE x*sf,-2,x*sf,2:NEXT
x *** draws scales on axes
FOR y=-9 TO 9:LINE -2,y*sf,2,y*sf:NEXT
y ***
MOVE 280,-10:PRINT"x" ***
Label axes
MOVE 10,290 :PRINT"y" ***
GCOL0,1
FOR i= 1 TO 2
ylow=-10*m(i)
+ c(i)
yhigh=10*m(i)
+ c(i)
LINE -10*sf,ylow*sf,10*sf,yhigh*sf
*** Draws the red line graphs !!!
NEXT i
VDU4 *** return to "text-cursor
printing"
ENDIF
ENDPROC
Next tutorial
Richard Weston's Homepage