Here is
the general «three-sweep» technology of construction of a
graph in Cartesian coordinates in Mathcad (see the upper part of the
fig. 29):
• first step: the appearance of
analyzable function is given;
• second step: the range variable is
introduced (Range Variable – in our case it is x), which
assigns the graph scattering by the axis of ordinates (x start
and x end) and number of points on it (not number of
points but the interval between them is assigned);
• third step: we give a command to
construct the graph, which we can format if it is necessary; for it we call
corresponding dialogue windows; the size of the graph is changed with the help
of transport of its contour (general «window» technology in Windows).
Starting with 7-th version and upper in Mathcad
it is allowable quick (Quick Graph – see the second graph in the
fig. 29) technology of construction of «flat» graphs (in Cartesian and in polar
coordinates):
• first step: see above; by the way
it is possible not to give the analyzable function as the user’s function – it
is enough to write it near axis y (it is done on the second graph in
the fig. 29)
• second step: we give a command to
construct the graph; here some more defaults appear: the graph is constructed
in the following range from -10 till 10[1] by the variable number of points,
which depends on the size of the graph; as rule, the second default (number of
points) meets user’s wishes (here the compromise between quality of the graph
and speed of its construction is kept up). User has a right to break the second
default (graph scattering) and to give the other range of argument scattering
if he changes the numbers at the ends of the axis x.
The
second technology has one more advantage (the main point of the Tip): the
variable arg are free from some numerical value, which
later on can confuse, for example, analytical (symbolic) solution of the
problem. (Continuation of the theme in Tips 65 and 73.)
Apart
construction of a graph with prior marking of the argument (three-sweep
technology) has the following advantage: the graph, which is construct with the
help of two-sweep technology, is badly drawn in Mathcad Explorer[2]
(the range is divided on low number of points and the graph is broken line).
This and
some next Tips (see Tip 35, Tip 36,
Tip 37, Tip 69 and
others) will be illustrated by simple program[3], which forms user’s function with
the name V that returns volume of regular (right) cone
and that has two arguments: diameter of its base (d) and length of
its generatrix (l[4]).
Programming
language Mathcad does not have special tools (operators) for commentation of programs. It is
considered that one or another operators of a program can be commented by
placed right (or around) the program textual
insertions (see point 1 in the fig. 30). Sometimes it is difficult to
place the commentary in necessary place, i.e. exactly near operators because of
possible difference in prints of formulas and textual insertions.
We
have already marked the inconvenience of such technology Tip 5: when you carry over the group of operators it is
possible to lose something.
It
is possible to insert the commentaries in programs as separate operators – textual constants (first line of the
program in point 2 in the fig. 30). But it is possible to do the other way
(see 2-3 lines): in the program to write row vector with two elements. The
first element is commentary but the second one is the operator of the program[5].
The
commentation by the matrix, which first element is a textual constant has one
essential defect: the function stops working with dimensional arguments because
of an array of Mathcad can not keep values of «different» dimensions. One more
defect is the impossibility of spell checking of the commentaries. In this case
we can advise to do the following: to write the commentaries in Word, for
example, and then to carry them in Mathcad.
Remark. For it would be possible to write
the commentaries (textual constants) using Russian print (Cyrillic) it is
necessary to attach the print, which works with Russian letters to the
constants (Constants – see the upper part of the fig. 30).
The
commentation methods of Mathcad-programs that we have illustrated in Tip 30 do
not describe in the documentation and in Íelp of Mathcad. That’s why they can not be
considered as illegal ones – it is
not known how the hollow textual constants will behave in future versions of
Mathcad. As since developers far from always provide complete compatibility of
versions with respect to even documentary methods. Reasoning from this, as well
taking into account the fact that there are no textual constants in Mathcad
PLUS 6.0 (but programming in Mathcad[6] began starting exactly with this
version) it is possible to recommend the legal method of program commentation
(see fig. 31): we assign something the variable Rem (in our case
it is zero) out of the program, then we assign «something» the
variable inside the program, which name is commentary. If you wish it is
possible to insert gaps in the name (see Tip 14) – in our case «down» dash is
used instead of them.
Fig. 31
with variable-remarks prompts more natural, legal and
documentary method for program commentation – use of long speaker names of the
variables and of the user’s functions. It should be removed «primitive»
commentaries in the fig. 31, but using the command Replace[7] from menu Edit
it is necessary to change V to Cone_volume, d to Cone_diameter
and etc.
In Tip 31 we recommend to
comment the operators of Mathcad-program by setting remarks in form, which is
accepted in the programming language BASIC. Indeed in the programming language
the key word Rem is placed at the beginning but not at the end
of the commentary:
Rem is commentary
Followers
of the other programming languages can vest Mathcad-commentaries with the other
forms that are showed in the fig. 32. At that if you make use in addition Tip34,
then you can «mask» Mathcad-program one of traditional programming language.
We have
recommended placing operators in column when you write Mathcad-documents in Tip
5. Such order you’ll obtain if you press the key Enter after input of every
operator. When you have input the finished notional fragment it is expedient to
place some operators on one line for the Mathcad-document will be more compact.
Such organization of the operators is recommended by the documentation of
Mathcad and almost all figures of the book have been designed corresponding
method.
If
you really want you can give the «second change»
Mathcad-program, which operators will be executed not only top-down but also
from left to right. There is the key Add Line in the programming toolbar of
Mathcad but there is not the key for addition empty space for new operator on
the line. However it is possible to do using the other tools.
To
add new operator on the right to the operator on program line in Mathcad 7 Pro
(see the first function with the name Min_3_Ñ_mc7 in the fig. 33) it is
necessary to cover the first operator by cursor (not part of the operator but
the complete one!) and to press the key «,». After this comma and placeholder
appear on the right of registered operator where new operator is inserted.
Everything would be perfect if there was possibility to write enough
extensional program, which occupied on one screen of a display, one standard
paper À4[8] (see Tip 79). But
Mathcad 8 Pro appeared and it turned out that «two-dimensional»
program, which keeps two and more operators on one line divided by commas, does
not work in new version of Mathcad. It stresses once more that two and more
operators are the undocumented method, which can give the bug at any moment.
Capacity for work rebuilds if we turn into one dimension: to leave one operator
on every line (to insert new lines in the program and to carry there «odd» string
operators[9]).
But
writing down a few operators on one program line is still very alluring method.
Such programs can be tried to save and to make them working in Mathcad 8 Pro
and 2000 Pro. If we delete commas between the string operators (to place cursor
near comma and press the key Del or BackSpace) then comma will disappear but
the adjacent operators will be enclosed in brackets and the program will work.
It is possible to delete originated brackets during resuscitation of the
program (see the second function with the name min_3_C_mc8 in the fig. 33) but it is
possible to keep[10] them. At that it is necessary to
remember that a few operators are the undocumented method. Two or three
operators on one line are delayed-action mine, which is ready «to burst» at any
moment. (Indeed, in Mathcad there are « documentary» mines.
Very often even quite simple Mathcad-document, which is written following all
the rules, stops working and requires the update if you carry over it to new
version. The author has a number of programs, which kernel is solution of the
systems of nonlinear algebraic equations. The block Given-Find works
excellently in the fifth version of Mathcad but gives error massage instead of
the answer in the sixth version. Unfortunately such troubles exist quite often
on changeover new version of Mathcad: «Better thing is enemy of good »: see Tip 62.)
It
is possible to turn two lines with single operators to one line with two
operators the following way: upper operator is covered by cursor
a ¬ 1½ b ¬ 2 c ¬ a + b |
and the key
Del is pressed. It leads the following result:
A ¬ 1 b ¬ 2 C ¬ a + b |
Then
we can cover by cursor the operator b ¬ 2 and press the key Del. This way we get the line with three operators. There
is nothing special in these operations: two lines (two indentions) are turned
to one line (to one indention) by the set of cursor at the end of a line and
press the key Del in the environment of any textual editor.
But
we repeat that if reader sees Mathcad-program, where there are a few operators
on one line, in this or the other book or magazine, he has to take it as «
shrink-wrapped» program. It should be «unarchived» before
input it for the avoidance of possible mistakes – to place one operator on one
line.
Existence
of a few operators on one line is the cause of one more trouble. We look
through the following «horizontal» program:
A := a ¬ 2 b ¬ 3 c ¬ 4
It
is asked what value the variable A will be equaled after execution of
the program. In the theory it is four: Mathcad-program has to return the
last value, which it operates (ñ ¬ 4). But it is not so. The variable A will equal twenty-four
– it is production of the numbers, which appear on the given program line. In
Mathcad 7 Pro the value of the variable A, which is
returned by our program equals four that corresponds to logic of our program.
Here is the compatibility of the versions! The compiler of Mathcad 8 Pro and
2000 Pro suggests that the gaps between operators are missing mulTiplication signs
(see Tip 9). The program returns right answer
if it is «given back the vertical line» or even
the last operator ñ ¬ 4 is written on separate line.
Hence
we have the following summary: if reader takes the risk of writing a few
operators on one program line in Mathcad then he has to remember, in the first
place, that it is undocumented method and, in the second place, that the last
string of the program (or operand of the operator return) has to have only one operator.
One
more method for placing a few operators on one program line is described in Tip
30 – the embedding of a matrix with one line, which elements are the operators
of the program. Both the first and the second methods have the limitations not
to mention that they are undocumented methods of programming.
The
programming language Mathcad is «young» enough. Many programming languages
(BASIC, fortran and etc.) in their earlier versions allowed only on operator on
a line. Then («at a mature age») this limitation was removed for a
programmer can decide himself if to write a few operators on one line or only
one. We’ll hope that the key Add Line will be supplement with the key Add
Operator on the programming toolbar and above mentioned method, which makes
more compact programs will become legal one in new versions of Mathcad.
One
more remark (warning). As we have said above Mathcad 2000 Pro understands a few
operators on one line as product[11]. That’s why it is impossible to
work with a few operators on one line if one of them has the other type (all
operators are numbers but one of them is text, for example). Execution of such
program will be interrupted by error message.
Many
Mathcad users, with experience in conventional programming languages, find the
«multi-stored» technique of formula recording quite natural outside the program
body, and rather «wild» within the program. Such users have gotten used to the
text («one-stored», single-level) formula recording technique of other
languages. To placate old habits (which are, as is known — second nature), and
to make programs more compact, one may remember the technique of redefining and
renaming built-in function described in Tip 20. It is also possible to redefine
Mathcad’s basic built-in operators. For greater program compactness and for
further indulgence to the users habits the «multi-stored» Mathcad operators can
be recorded like «single-stored» ones: integration, differentiation, summation,
product[12] etc. In the figure, the function V returns the value of the cone volume using «basic-like» operators and
functions (sqr, «^» and so on). The Mathcad way to formulate the function V is presented on the figure for Tip
30. For this Tip, functions with non-standard names (+, -, *, /, ^, è **[13]) are created by means of the
technology of the non-standard symbol insertion, described in Tips 14 and 16. (Editing
by Thomas Porritt tporritt@tesoropetroleum.com)
Before to
write a program it is desirable «to reconnoiter» the
operators included in it outside of the program. In the point 1 in the
fig. 35 the operators are written with the help of which the volume of our
cone is calculated using measured parameters – diameter of its base and length
of its generatrix. In the point 2 the given operators are gathered in the
program, where the variables r, S and h are already local ones and they are inaccessible for control (they will
be inaccessible if you do not become familiar with Tip 36).
If
a loop is put in a program (for the sake of loops we use the programming
Mathcad very often) then we can try to realize it by step-by-step approach
outside of the program. The features of realization of it are described in Tip 53.
By the
time of debugging it is worth to add a program so that it will return values of
all variables, which are used in
calculation. In the fig. 36 the matrix is written on the last line of the
program, which first column is commentaries, but the second one is local
variables of the program devoid of their dimension (corresponding unit is
written in commentaries). We have to do it as since the elements of the matrix
can not have different dimensions (see also Tip 30 and Tip 45). It is possible to remove the
matrix after debugging of the program and instead of it to write the local
variable V for the sake of which we wrote the program.
Also it is possible to come back to the program, which we have considered in Tip 35 (to the program without the local variable V). But it is possible to set one more function – Volume (see the
end of fig. 36), which will «exclude» necessary line or necessary element
of the matrix from the function V.
One of
the effective methods of debugging is BreakPoint. It is prescheduled breaking of
the execution of a program by break
points. Such debugging tool equips many «modern» tools of programming.
The
construction return…if… is inserted in the function V[14]
three times in the
fig. 37. It returns (return) value of a local variable if (if) user set break point (the third, additional argument of the debugging
function V; it is
possible not to introduce the argument, but simply to write ÂÐ:=1, ÂÐ:=2 and etc. before call the function). When we change the values of the
variable ÂÐ we can make the function V to return values of its local variables by turns not all at once as in
the fig. 36. When we «move» break point along[15] the program then it will be
possible to localize an error if it has crept into the program. After debugging
of the program you can remove debugging operators and debugging argument. But
if you set the values of the argument unequal 1, 2 or 3 when you call the
function BP then you will
be able to keep everything as is.
It is
possible to dispute about if the alternative is the main or the subsidiary
structural directive construction (see Tip 39).
But the loop with precheck is more «mainstream» than
the loop with postcheck. It is the fact in – there is the loop while (at first it checks and only then it executes) but there is not the
loop repeat (terminology of Pascal – at first it executes
and only then it checks). The operators of the loop body while can execute not once whereas the operators of the loop body repeat have to execute at the list once. But loop exit is able to be situated
not only in its «head» (the loop while) not only
in its «feet» (the loop repeat) but also
in the middle of it. Apart such exits can be a few ones, but Boolean
expressions, which direct the loops, can be different. The user’s functions are
written in the fig. 38. They return the root of the algebraic equation
with the help of Newton’s method (tangent method)[16]. The loop with precheck requires
that Boolean expression, which directs the loop, has been defined before entry
in the loop. But this is missed in Newton’s method. The variants of solution of
the problem are showed in the fig. 38. The loop with precheck and the
method of artificial assign of necessary first value of Boolean expression,
which direct the loop x1←x + 2.TOL[17], are used in the first function. The
loop with exit from the midpoint is used in the second function. This does the
program more natural: break (break) the loop of approximation to the
root, if (if) new approximation (x1) almost
equals previous one (x). For the title of the loop while 1 does not confuse users (basically any number nonzero can be written in
this case), it is possible to write one in white and white (see Tip 19)
and to hide it. It will be good idea if we write the symbol infinity «∞» in the title of the loop while: as if we comment this way the method of construction of the endless
loop, which body has the operators break (return or ñontinue).
If
we raise the construction break if¼ (return¼ if¼) to the word while, then we’ll get the loop with precheck (while), if we
lower it to «feet» of the loop body, we’ll get the
loop with postcheck (repeat). The constructions, which break the loop, can be a few ones. At that
different Boolean expressions can be written after the key words if.
The
function in the fig. 38 has one more distinguishing feature. The second
function works with use of auxiliary user’s function «approximately equal»,
which for its turn relies on the built-in variable TOL (accuracy of
calculations – by default the variable TOL equals
0.001).
[1] This is not always. If, for
example, when x < 0 a function either isn’t defined or returns
complex values or has some other problems, then the graph will be constructed
only on right part of the axis of ordinates.
[2] Short-cut version of Mathcad 8 Pro,
which is meant for reading of Mathcad-documents (mainly in Internet).
[3] Advisedly the author illustrates Tips
of the book by simple problems as he considers that it is impossible to give
reader unknown algorithm and new program method at the same time.
[4] The variable with the name l is not quite convenient as since it (depending on selected print) can
be confused with one or with the Latin capital letter I. But the letter l has already fixed for marking some
length in geometry: the word «length» begins with the letter «l» both in Latin
(longetudo) and in main European languages. When you create Mathcad-document it
is very important to give the variables that names, which were fixed after them
during forming one or another scientific discipline long before computers.
We’ll be able to work without commentaries at all in Mathcad-document if we
mark right way the variables.
[5] We forestall this Tip the Tip 33, where it will be said how it is
possible to place a few operators on one line of the program.
[6] Mathcad PLUS 6.0 is popular in out
country because of russified version as well as because it works under Windows
3.x.
[7] In Mathcad 2000 the command became
to work either in text or only in formulas or in both places.
[8] It is very comfortable when you
work with the program – when you look into it, search errors, optimize.
[9] It is possible to consider the
program with a few operators on one line as «shrink-wrapped» one. To use the program
it is necessary to «unarchive» it– to bring every operator on separate line.
[10] These brackets will appear once
more if the program, which was saved in Mathcad 8 Pro, we open in Mathcad 2000
Pro.
[11] It is possible to lead cursor to
the gap, to click right key of a mouse and to change the gap to some mulTiplication
sign using the first command of contextual menu.
[12] It became possible to use the
symbol of «horizontal» division in addition to the symbol of «vertical»: à ¸ â. This operator has completely «legalized» in
Mathcad 2001 – separate key has appeared for it in the toolbar of arithmetical
operators.
[13] To please followers of BASIC (^)
and fortran (**) the exponentiation operator is duplicated. The
given user’s functions are called in the form of infix operators in the fig. 34.
[14] The vertical line in the fig. 37, which marks the beginning and the
end of program block, is curved. It is made in process of rework of the figure
for its compactness (see also Tip 52).
[15] Or «across»; «along» is when one
moves something across. But we move the break point «down».
[16] In our programs in the fig. 38 the condition of the loop exit is
minimization of the distance between two contiguous
(scheduled) approximations to the root x1 ≈ x2. In it possible to
choose the other condition – y(x) < TOL, for example. These
conditions can be checked by turns – here is the loop with two exits from the
middle.
[17] In this case we have to assign the
variable X1 such value so
our loop will execute at least once without fail (imitation of the loop via t).