01 do not use assignment statement
Mathcad program package is frequently called supercalculator: it displays a numerical answer for any expression, containing constants, variables, operators and functions. To obtain this result you should simply type «=» (equality sign) after the expression:
125 + a2 – sin (b) = 2.753
Of course, variables, included in this expression (a and b), should be defined with any numerical value beforehand. An assignment (input) statement in the Mathcad environment is symbolized like «:=». But it is typed only with «:» (colon) button. Mathcad kindly prints a second symbol «=» itself:
The main point of this Tip.
It is worth to push not «:», but «=» button inserting an assignment statement to a Mathcad worksheet: if the entered variable has not been defined yet, operator «=» (output) will automatically turn into «:=» (input). And if this variable has been defined, then its numerical value is displayed. This operation controls whether the given variable is free ¾ neither user nor system had assigned nothing to it. By doing so we secure ourselves against a number of mistakes. Firstly, a system variable can be «spoiled»: å: = 5, m: = 1, A: = 2 (variable å – stores the base of natural logarithm, m and A – store respectively measurement units of length and current strength). Secondly, one may forget, that the variable has been already «loaded» and to assign a new value to it. The event when a variable has one value in the first part of a Mathcad-worksheet and another in the second part ¾ is not quite normal.
But one can assign a new value to the variable not only due to forgetfulness. The variable redefinition technique was in vogue in those times when the computer memory was limiting problem solving and a «vacated» variable should have been immediately «loaded» with another value. Nowadays one have to act this way storing in the computer memory bulky arrays (vectors and matrixes, elementary and aggregate — this is a not numbered Tip).
You can shut off the automatic replacement mode, when operator «=» is replaced by «:=» (such hybrid of two operators is called Smart Operator), by choosing option Preference… from the View menu (see the dialog box, called by this command, at the end of the figure).
The variable numerical value input operator was a kind of stumbling block for those who had tried to master Mathcad about ten years ago, having heard much about its extraordinary ability to plot diagrams, solve equations and systems. People, from force of habit acquired while handling with fortran or BASIC programming languages, had been keying in «a=» instead of «a:» in the Mathcad environment and … had been abandoning further work with Mathcad because of a strange error — the Mathcad system had displayed such information: variable a is not defined yet. There were made attempts to define it with the help of the variable type assignment statement, but the Mathcad environment does not offer such facility so far. The MathSoft company has «given up» only in the seventh version — it has abandoned the attempt to teach users to key in «a:» instead of more customary and natural «a=». And now the pendulum is swinging to another direction: by means of the first Tip the authors of the book are trying to make (to teach anew) users keying in again not «a:», but «a=».
The note. The figure shows a Mathcad-worksheet in a mode when separate operators are displayed on a white background, while free space of the document is gray. This mode will be used to make some Mathcad-worksheets more readable: white frame against a light-gray background portrays separate operators more clearly.
Not numbered Tip. The Mathcad beginner is advisable to operate in the operator background marking mode (see the figure). It is particularly good for the situation when several operators are situated on one line close to each other and their bounds are not always clear.
02 Symbolical output instead of numerical
Sometimes it is worth to use operator «®» (output of the symbolical value of an expression or a variable) instead of «=» operator (numerical value output — see the 1-st Tip). The figure 11 illustrates three reasons for such substitution:
Item 1: operator «=» displays up to 15 decimal points in mantissa (see the number formatting window in the figure of the Tip7, for example); operator «®» ¾ up to 250 points (our figure presents twenty points — they are the key of the command keyword float).).
Item 2: operator «®» allows absolutely precise answers at the expense of output of all significant digits (item 2.2) or by representing a number as a simple fraction (item 2.4).
Item 3: operator «®» in conjunction with operator k:=k (releasing a variable from its numerical value concerning operators of symbolical mathematics) displays a numerical answer, including also symbolical constants (this result can be obtained, truth to tell, simply by printing a variable after the answer like physical units of measurements ¾ see item 2 in the figure of the Tip 4).
Attention! In the figure of the item 3.2 there is written 123/2 rather than 66.5: these numbers are different for the symbolical mathematics of Mathcad. In the case that there was written 66.5 in the item 3.2, the answer would be quite different — 259.5×k. A symbolical answer has to be anticipated by a «symbolical» question.
03 Combination of symbolical and analytical output
Sometimes it is worth to unite symbolical (see the 2-nd Tip) and numerical (see the 1-st Tip) output operators into a single expression. As in the previous Tip, the figure shows three examples of such combination:
Item 1: arithmetic expression enables both a simple and decimal fraction answers at the same time.
Item 2: while solving an equation one can obtain both absolutely precise and approximate, but «speaking» answers («speaking» in the sense that the root position on the numeric axis becomes evident at once).
Item 3: expression can be simplified before calculation. This operation is implemented by Optimize mode, but in our example we once again observe both analytical (a polynomial of degree four) and numerical answers.
04 Constants in the numerical answer
Many users do not know, that the basic operator of Mathcad «=» (the numerical value output operator — see the Tip 1) has two operands rather than a single one:
˜ = ˜[˜]
The Mathcad user fills in the first (left) small square (placeholder) with an algebraic expression (or a variable), which numerical value appears in the second (medium) placeholder (see the Tip 1). The third (right) placeholder may contain (square brackets accentuate that this operand is not necessary) any constant or variable, which has been defined earlier. Usually this placeholder is filled in by the built-in or user’s (see the Tip 26) physical units, that permits various answers:
L = 200 ◦ cm or L = 2 ◦ m
The figure demonstrates another two examples:
Example 1: In the cartoon «Thirty-eight parrots » the length of Boa is measured in Parrots, Marmosets and Elephants. If to assign (to select) animal’s size, than Boa will «fit» in 5 Marmosets, in 2 Elephants or in 38 parrots and one more carrot’s wing. «You see, in parrots I am much longer!».
Example 2: The trigonometric equation solution quite often contains number p. If to input this number as the optional operand of the operator «=», then it will make possible absolutely precise numerical answers (one fourth p, rather than 0.7855…), and secondly, all answers (one fourth p with the period of 1). The answer can be supplemented by constants and even expressions containing p: 2 × p, 3 × p etc.
05 Typical set of three operators
The above figure presents a typical fragment of a Mathcad-worksheet with a heading «Initial version of the calculation»: the user types a text commenting further actions (Time), puts into calculation a variable (t), assigning a numerical value to it (360 seconds), and «prints» it out accompanying with another (minutes) dimensionality just for a change. One can pack the whole Mathcad-worksheet with such sets of three operators and solve a problem put by (the velocity estimation as in our figure). But this technology is inconvenient for carrying fragments over a new place, because it is possible to lose operators included in this fragments «on the way». Furthermore, if the output operator («=») is disposed a little bit higher than the input one, a relationship of cause and effect will be broken and error message will be displayed: «The so-and-so variable is not defined above».
Not numbered Tip: Creating a new Mathcad-worksheet it is worth to place operators in columns with only one on a line. In the ready document you may proceed «from a line to a surface», placing some operators on one line of the worksheet, making it thereby more compact and satisfying the European tradition to write initially from left to right and only afterwards starting a new line.
Another technology of calculation is displayed at the bottom of the figure: a comment (Time) is being written with six spaces on the end. Then, the mathematical areas (Math Region command in the menu Insert) are being inserted in the center and in the end of this chain of spaces. And these mathematical areas were filled in with input and output operators. Thus, an obtained conglomerate (text—three spaces—input operator—three spaces—output operator) can be dragged from place to place without any caution. Besides it is possible to transform the three bottom operators of the figure, constituting a certain semantic fragment of calculations, into a single operator: text of three lines including six mathematical areas (three input operators and three output operators). One may try to «push» the whole calculation into the single commentary text region, but such operator becomes difficult to edit: it is necessary to know where to stop.
Mathematical insertions in comments differ from the text, firstly, by color and font (the Mathcad default). Secondly, the background marking mode gives more broad white surroundings to the mathematical insertions: in the figure this white background of mathematical formulas is moreover thickened in the interactive mode (modification using the graphics editor environment).
06 Check of the data input
As a rule a variable in computation has a limited range of reasonable values. In this case it is expedient to check the input value correctness. The figure «asks» the user to assign a room temperature value to a variable t. Let us assume that this value can not be less than minus 20 degrees Celsius (at this point addition statement is functioning as logic addition — see the Tip 12) and more than 40 degrees Celsius (units of measurement — are accepted by default in this example). And if the user makes an inadvertent mistake, then the variable t takes a string value (“This is impossible!”) and blocks further computations. (The 21-st Tip informs how to control even the input value dimensionality).
The second expression containing function if is enclosed in frameworks (the Mathcad-worksheet area) which collapse in order to protect the user from embarrassment — see the command Collapse in the context-sensitive menu. To «slip out» this menu one should place cursor inside the area and push the right button of the mouse. The area itself (two horizontal lines) is inserted by the command Area from the Insert menu.
07 Input of a bulky matrix with standard elements
To put into calculation a vector or a matrix the Mathcad users usually resort to the command Matrix… in the menu Insert (or press button with the matrix image). After that there will be displayed a dialog box requesting for the number of rows and columns of the matrix to be created. This technique displays a number of disadvantages. First, in this case the size of a matrix is bounded by the value of 100 elements, while any matrix can keep up to 8 millions elements in Mathcad. Many users solve this problem by creating several little matrices and «gluing» them together into a large one using stack and/or augment operators (see the Tip 13). The second disadvantage is in that all elements of a matrix defined through the Matrix command are unfilled. In this case matrix elements have to be inserted manually. And maybe this is a reason for a limitation of 100 elements, which can be inserted manually yet. A large amount of elements requires a different input technology, and its example constitutes the main point of the Tip.
The figure illustrates the Mathcad-worksheet where a bulky matrix containing a standard element (we have set it as 1.0) is automatically created and displayed. This matrix size is set also by the user. Then this matrix is copied to the right operand of the assignment statement and subsequently edited: 1.0 is substituted by something else. We have assumed here, that the matrix elements — are numbers ranged from 1 to 2. We should not type units and points manually — it is sufficient to edit zeroes. Through the use of the function Matrix, or the technique described in the 8-th Tip it becomes possible to record various numbers in such semi-finished matrix. For further copying of the matrix M it is important to format it correctly at output. The fragment of the formatting window is showed at the bottom of the figure, where the user notes, that:
· There is only one number displayed after the decimal point (Displayed precision — there are displayed three decimal points by default, while the maximum allowed number of points is — 15);
· It is necessary to display non-significant zeroes too (1.0, rather than simply 1 — Show trailing zeros); they are not shown by default;
· There should be displayed the whole matrix in a matrix style (Matrix), rather than in the form of a table with scrolling bars (see the figure in the following Tip); by default the Mathcad system selects the matrix display style itself.
Not numbered Tip. It is worth to create such matrix by reading a file from the disk (see the Tip 57) or by introducing the Excel table into a Mathcad-worksheet (see the second part of the book).
08 Output of a bulky matrix
There may appear some difficulties not only at input of a bulky matrix (see the Tip 7), but also at displaying and printing its elements. By default there is displayed only upper left corner of a bulky matrix in the form of a table. Its sidehead keeps row numbers and headline keeps column numbers (we have broken this default in the figure of the Tip 7). A matrix which is not entirely displayed has vertical and horizontal scrolling bars with a runner to the right and at the bottom of a table. To look through the whole matrix one should move the runner: the table scrolling technique. Another possibility — output of a bulky matrix not in the form of a table but in the form of a matrix (see the Tip 7). In this case you can look through it by scrolling the Mathcad-worksheet window itself.
And if a bulky matrix has to be printed out (unfortunately, a sheet of paper has not scrolling bars), then we can recommend to print this matrix-table several times, having preliminarily displayed its separate regions with the help of the runner — see the figure displaying the matrix M three times so that all its elements become visible.
09 Do not pass multiplication sign
The Mathcad environment does not require a multiplication sign to multiply a constant by a variable. This method seemingly brings Mathcad-worksheets more closer to documents written in handwriting: 2 à, not 2 × à, 5 kg, not 5 × kg and so on.
But here are three reasons against this technique that runs counter to the programmer tradition to put a multiplication sign (asterisk) between any factors: 2 * à, 5 * kg and so on.
Firstly; design 2 × à is more or less clear: two is multiplied by the variable a. And expression 2 a carries some ambiguity: this is both the product of 2 by a and a postfix operator named a, which operand is a constant (two).
Secondly; the behavior of expression 2 a is not known in the case when this construction is applied in symbolical mathematics.
The figure illustrates the third «contra» argument. If two is reduced by unit (see the bottom of the figure), then it should remain unit, but by no means two and furthermore with minus? The secret of this «focus» appears to be evident, but this simplicity is, indeed, «worth than larceny». The point is that not unit is subtracted from two, but two is multiplied by minus unit in the given example. This service of Mathcad (programmer does not have to enter a multiplication sign) has turned out to be an «ill turn» (involuntary pun). There is another ambiguity (see the item 2 in the figure): what is k a — is that the variable k, multiplied by the variable a, or is that a variable, with a name of three symbols, where the second one is a space (see the pieces of Tip 14 and 18). Another interpretation: k a — is a postfix operator with the operand k or a prefix operator with the operand a.
10 Global assignment at the system solution
The Mathcad environment offers the built-in function Find for solution of the system of algebraic equations and inequalities. A system is commonly solved this way: the first approximation to the system root is being set with the help of assignment statements «:=». Then the key word Given is being written, a system of equations and/or inequalities is being set (it is not allowed to use only Boolean operator «¹») and, at last, function Find is being called. This function returns values of its arguments in the form of a column-vector, converting a system of equations into a system of identities. In a number of cases in order to solve the system the standard values of built-in variables TOL and CTOL should to be changed. These variables influence on the accuracy of calculations. Very often this technique requires successive approximations to the right answer. In this situation it is advisable to change the above-mentioned sequence of operators (but not sequence of their satisfaction) — to «pull down» the first approximation and other initial settings under the function Find by means of the «º» operator. If the system is bulky and display can not find room for it, then the shown sequence of operators will obviate the necessity for scrolling a Mathcad-worksheet to adjust input data and to browse the answer.
11 Superfluous brackets
Many Mathcad functions have a vector or a matrix (array) as their argument. Vectors and matrices represent a collection of scalar values contoured with brackets. On the other hand, brackets also are an attribute of the function enclosing the function argument. This feature leads to appearance of double brackets at the conventional call of a function with a vector-argument (see the first operator in the figure). Double brackets can embarrass a user who may try to remove a «superfluous» pare of brackets not understanding a reason why it is impossible. The way out — lies in the call of a «matrix» function (which argument — is an array) not in function form, but in the form of a prefix operator, which operand does not need a «brackets’» design (the second operator in the figure).
The figure depicts the dialog boxes creating the three prime objects of Mathcad:
· The built-in function insertion — Insert Function; the order of the mean function input to a Mathcad-worksheet is shown. Function mean returns an arithmetic mean of the array elements.
· Insertion of an array — Insert Matrix; there is displayed the order of the row-vector input — that is a one rowed (Rows) matrix with several columns (Columns).
· The prefix operator insertion through the fx button in the Evaluation window.
Input of the first two objects is also possible without call of the dialog boxes, shown on the figure. The third object is required not so frequently, as the two first ones, that is why it can be inserted only through the Evaluation dialog box.
12. Set of three Boolean functions
The Mathcad environment does not present built-in Boolean (logic) functions And, Or and Not (logic negation), which are an integral part of all programming languages and which support implementation of all sorts of logic constructions, loops and alternatives. And what is to be done here!?
Firstly, it is necessary to recollect, that «logic AND» and «logic OR» have also such names as «logic multiplication» and «logic addition»: the given functions are realized through customary multiplication and addition operators (see the item 1 in the figure): the Mathcad environment recognizes logic «not» — as zero, and logic «and» — as a non-zero number.
Secondly, logic «and» and logic «or» functions can be defined (see the item 2 in the figure). In so doing there can be defined any (variable) quantity of Boolean arguments — that is elements of associated vectors of multiplication and addition operators. (Not numbered piece of Tip. It is worth to define two pairs of functions with various names È-And — ÈËÈ-Or to satisfy both Slavophiles and Westerners.)
Thirdly, the Mathcad environment already has built-in functions that are well suited for logic tasks, — they are min and max (see the item 3 in the figure). And furthermore, they are also convenient for the following reasons:
· It is not necessary to «litter» a Mathcad-worksheet with supplementary built-in functions AND, OR and so on;
· Functions min and max accept both matrix, column-vector and row-vector arguments that allows more compact notations;
· Functions min and max accept not only logic expressions (yes – no, 1 – 0) as their arguments (array elements), but also real expressions, varying smoothly from zero to unit. Thus it is possible to operate not only with precise but also with fuzzy logic.
Not numbered Tip. If it is necessary to hasten the program operation, then it is worth to use the multiplication operator instead of the min (And) function, having placed, where possible, an expression most often assuming the value zero on the first place. The point is that the multiplication operator returns zero if the first factor equals zero in order to hasten computations, while the function min (And) is too pedantic: it sorts out all elements of a matrix or a vector — its argument.
13 The built-in function redefinition
Mathcad offers the user a unique opportunity of renaming and redefinition of built-in functions. Renaming seems more or less intelligible (the old substance in the new frame): a new user function is being defined which is completely or partially coincident with one of the built-in functions. What is the reason for that? The items 1 and 2 of the figure illustrate two examples of the built-in function renaming. First, it is possible to replace a short name of the function (identity) with a more integrated one (Unit square matrix — see the item 1). We obtain a certain hybrid of a function name and a comment. The renamed function in the item 1 is called as a prefix operator (fx), rather than as a function, that makes possible to get rid of brackets and to make the operator, creating a new matrix, absolutely natural (refer also to the Tip 11). This effect is also aided by three spaces in a new function name (see the pieces of Tip 14-16).
In the item 2 the name of the function reproducing the built-in one (augment - «horizontal» confluence of two matrices) is abbreviated to one symbol. It allows drastic reduction of the program size because this program use the renamed function repeatedly. Besides, a word is replaced with the «contraction» symbol, which is more easy to remember.
The item 3 demonstrates redefinition (the old substance in the new frame) of the built-in function stack to visualize its essence. The function augment (see the item 2 in the figure) accomplishes the «horizontal» confluence of two matrices – its arguments are positioned also «horizontally». Function stack is dedicated to the «vertical» confluence of two matrices but its arguments are positioned still horizontally (see the item 3.1), that is not quite relevant. Redefinition of the function stack – replacement of two arguments by one column-vector with two elements (see the item 3.2) – restores «geometrical» logic of the matrix confluence functions.
The note. It is impossible to redefine the built-in functions WRITEPRN and APPENDPRN. Their operation is analyzed in the Tip 57.
14 Special symbols in the name of variables and functions
Programming environments always place certain restrictions on the variable and function names. As compared to conventional languages Mathcad has less restrictions: the variable name in Mathcad can include Greek letters (Cyrillic letters deserve particular investigation – see the Tip ?) and subscripts. All these peculiarities of Mathcad enable names assigned to different variables long ago in mathematics (p, for instance, rather than Pl), physics (tin), chemistry and other branches of science. On the other hand, the Mathcad-variables have their own restrictions associated, in a manner of speaking, with a bad «heredity» preventing the variable name from insertion of some symbols: «$», «&», «?» etc. The point is that these and other symbols (keyboard keys) are loaded with a purpose of input in a Mathcad-worksheet of sum operators («$»), integration operators («&»), differentiation («?»), product («#») etc. If to push these buttons at input of the variable name, there will not appear an indicated symbol, but a blank of the corresponding operator. But, if you are eager for that, it is possible to enter special symbols and spaces into a name of user’s function.
This technique of the «conventional» restriction removal from the variable names has its own history. The 30-th Tip recommends to use string variables — series of symbols framed by inverted commas — for comments of the Mathcad-program operators. Such series may include spaces and other symbols («$», «?», «&» and so on). Sometimes when editing such Mathcad-program there may happen «a glitch», that will result in a loss of inverted commas by string constants. Constants will «turn red» indicating an error situation. But every cloud has a silver lining — it is possible to take advantage of this situation (transformation of a string constant into the variable name), and that is described in the figure. The «ablush» series of symbols should be copied and subsequently utilized as a name of a variable or a function.
The technique of the insertion of special symbols and spaces into the Mathcad names (and many people find this method a pure sedition) has such «historical» ground. The DOS environment and Windows 3.* did not allow spaces, Cyrillic letters and other special symbols in the file names. And the Windows 95/98 environment has lifted such restriction.
15 Enumerated variable names
The previous Tip shows how to lift some restrictions from the Mathcad variable names — how to insert special symbols and spaces into a name. But some restrictions on the variable names still remain. In particular, it is impossible to see a point in the Mathcad-variable name: you can insert a point but afterwards (when cursor is taken away) the point disappears, halving the variable name – to the name itself and a subscript text: there was t.in, it has become t in. (A subscript text is not entirely desirable for the Mathcad-worksheets, since it can be confused with the operator of the vector element indication — numerical subscript). If to continue the analogy started in the previous Tip, it is possible to note, that the point is also slightly ambiguous in the file name of the operational system Windows 95/98: by tradition the point separates the file name and its extension (its «surname»). If the full file name containing more than one point it is not always clear, where the name finishes and the extension begins. The second restriction on the variable name is in that it is impossible to start a name with a number. But… It is meant an Arabic number (by default).And there also exist Roman numbers apart from Arabic ones. The figure illustrates solution of a conventional linear programming task: a furniture factory can produce two models of chairs at the cost of 70 and 108 relative units. On this order human and material resources are allocated. Besides, quantity of boards, cloth and time for producing of each chair is known:
Board consumption, meters
Cloth consumption, square meters
Time consumption, man-hours
The first model
The second model
The question is to project manufacture of chairs so that their cost is highest possible.
There are two unknown variables in this task which are enumerated and have special symbols within their names (minus sign and space): I-st chair and II-nd chair.
16 User's physical units
Mathcad presents quite a lot of system (predefined) variables keeping the built-in physical units of various systems (see appendix 6). But the user has a right to complement this list and put into calculation user’s units of measurement (see the figure), combining them with the built-in ones.
The user can be ignorant of units he really needs for calculation at once. These units may be put into calculation as necessary: for example, one needs to input time in the week duration units — then it is sufficient to return to the «head» of calculation entering week:= 7 × day and so on.
Operator ì: = m is also convenient for the following needs. It is possible to be lost in reverie and to assign a new value to the variable m, which is very popular (see Tip 1). This operation can break the orderly system of physical units stored in Mathcad. Operator ì: = m liberates the system variable m to store something else.
17 Nonphysical units of measurement
We did not use units of measurement in the 15-th Tip considering the linear programming task — the problem on the chair manufacture plan. But these units were mentioned in the problem description (see the table of the Tip 15). Along with physical units (meters, square meters and hours) the given task employs countable (chairs, people) and financial units.
The Mathcad program package has the built-in system variables (refer to appendix 6) keeping physical units of measurement. The figure solves the problem on the chair output plan by application of the all quantity measurement unit.
The American currency unit ($US) is put into calculation on the principle «Time is money»: the given calculations does not use a physical quantity «time» and it can be temporarily «loaded» with another duty. Certainly, it is easier to write down: $US := 1, and pieces := 1 (our pieces are «attached» to gram-molecules, which are also not utilized in the given calculations), but it is fraught with such a mistake:
$US + pieces = 2
There should appear not a numerical answer (two), but an error message «Incompatible dimensions».Physical and other quantities are used in the described calculation not only for making it more «readable» but also for the dimension conformity control in the inserted formulas.
18 Once more about special symbols in the variable names
The authors have published their technique of insertion spaces and special symbols into the variable names, described in the 15-th Tip, on the Collaboratory site (http://webserve.mathsoft.com/mathcad/collab) of Mathsoft (Mathcad designer company). That was quite intriguing:
I have spaces in var-name := 10
Can you do it? := 2
I have spaces in var-name + Can you do it? = 12
There were three kinds of responses to this quiz:
1. And what’s the big idea?!
2. How do you contrive to do that?
3. I have been inserted special symbols into the variable names for a long time, but I’ve never known, that it is possible to enter spaces too.
The third response leans upon the fact, that one may use special chord (Shift+Ctrl+k) for names of functions and variables to block the Mathcad system perception of special symbols (space, $ and so on) as the command of the name envelopment with cursor (space) or the sum operator ($). The figure displays definition of the variable with two spaces and «$» symbol within its name (Cost in $US). And the second variable with a space inside (Russian ruble) is obtained by editing the first one. It is impossible to insert spaces and special symbols at editing. To do that the above mentioned chord is required (the Tip 54 describes the third technique of creating nonstandard variable and function names).
19 The invisible function and function-werewolf
The Tip 15 recommends to write down not Arabic (it is impossible) but Roman numbers starting the variable name to enumerate variables. (But, true, one can write The first chair, The second chair, substituting Arabic number with words).
But many people can count Roman only up to twelve, recollecting the clock-dial. But we face Roman numbers not only checking the time, but also keeping financial payments: amount of money — is an Arabic number, and its cash fragmentation — a Roman one. The program shown on the figure will prove useful to the automatic teller operation, for example.
But it is not the very purpose to cite this program. The object is to give the reader another piece of Tip.
A lot of Mathcad built-in functions and operators have their inverse analogues: sin ¾ arcsin, åx ¾ ln(x), differentiation — integration etc. Producing a pare of user functions (direct — inverse), one may act in a such way: produce the first function, and define the second (inverse) function employing the first one. If the function has a real argument then the task is reduced to the equation root search, and if argument is integer, the task may be solved through selection.
The figure illustrates operation of such «double-sided» function. This function returns a Roman number, if its argument is an Arabic one, and, vice versa, it returns an Arabic number, if its argument is a Roman one. Single function incorporates both direct and inverse algorithms. The given function determines the direction of conversion identifying argument’s type. If (see operator if) it is of the string type (the built-in Boolean function IsString is used here), then Arabic number is searched, otherwise (otherwise) — a Roman one.
Apotheosis of the technique of insertion special symbols and spaces into the variable names (see the pieces of Tip 15 and 18 — is the user function, described on the figure, with a name of a single space. The question is, what can this function prove useful to.
The anonymous function allows not only recalculation without overloading a Mathcad-worksheet with unnecessary words and symbols ("XXV" =25 is clear without any comments), but also simulation of calculations (subtraction, root extraction, sine computation) with Roman numbers (see the end of the figure). The focus is that our function is called not conventionally, but in the form of prefix and postfix operators: there are no brackets framing an argument (or, rather, operand), and the symbol of operator itself is invisible (see the Tip 11). Thus it leads to the absolute illusion of Mathcad’s ability to operate with Roman numbers without any supplementary tools — operators and functions.
There follow some pieces of Tip from the figure:
1. In specific cases it is expedient to combine direct and inverse calculation algorithms into a single function. The reason is that these algorithms may have the same base (our pairs of Roman and Arabic numbers, for example): a single program is shorter than two separate ones.
2. Sometimes it is expedient to give an invisible name to an object of a Mathcad-worksheet. There can be a space or a chain of spaces (another technique — the name is not a space, but have a white colored font — see the Tip 50). There can exist several different styled variables or functions with a name of a single space (see the Tip 43).
3. The function displayed in the figure can appear useful for encryption of messages. For instance, you may send such a message to your trade agent: «Minimum price is MM, and maximum — MMCD». If you apprehend that the competitor, you protect the message by encryption, is acquainted with Roman numbers, then another pair of numbers may fit: the Arabic numbers analogue are, for example, Fibonacci numbers, (1, 2, 3, 5, 8, 13 and so on indefinitely), and their Roman analogues —are their serial numbers («1», «2», 3», «4», «5», «6» and so on indefinitely).
20 Adding superfluous brackets
The reader has likely already noticed that some pieces of Tip in the book interfere each other. Tip 11 has recommendations to exclude superfluous brackets from Mathcad-expressions. And now we will discuss insertion of auxiliary brackets.
The 1-st item in the figure once more time notes that the Mathcad environment, as well as all other programming environments and mathematics on the whole, value involution operator «superior» to multiplication operator, which in its turn, is valued «superior» to addition operator. These brackets once again accentuate the given hierarchy of operators, which can be familiar not to everyone (see examples in the figure of the Tip 34).
The item 2 by means of the function (treelike operator) augment simulates a not-reshuffled pack of cards — a row-vector with 52 elements (cards — «two», «three»… «ace»). Brackets look far from superfluous in the pack simulating «tree» (compare items 2.1 and 2.2).
In the 3-rd item the Boolean function Verdict is formed, which returns results of voting. The participant votes (1 — pro, 0 — contra) are stored in a vector-argument X: verdict is approved when more than two thirds of voters vote «pro» (function mean returns an arithmetic mean of the array elements). Brackets here also stress an algorithm of function operation and the satisfaction sequence of operators «>» and «:=». In the first case the function mean is called in the form of a prefix operator, and in the second — in the form of a function.
Inserting supplementary brackets one should remember that this method improving the Mathcad-worksheet «readability» may reduce the speed of its execution.
21 The variable dimension control
Sometimes the user is necessary to input by means of the «:=» operator not a simple value of a variable in a certain range (see the Tip 6), but a value with a fixed dimension of a physical quantity. Input of a dimensionless quantity or a quantity with another dimension should result in an emergency halt of the Mathcad-worksheet execution. Such scenario is realized by operators of the figure: user should set a length into the variable d (its style is User 1 — see the Tip 43), i.e. he should set a constant multiplied by either a built-in or user unit of length (see the Tip 43). An input dimension is checked as follows: variable d is firstly raised by and immediately reduced by a unit meter (m – the system variable). The value of the new variable d (its style is already not User 1, as the first variable d style, but Variables) does not suffer any variations but, in the case that the length is added by anything else, there occurs an error (see the upper half of the figure). The focus of the displayed Mathcad-worksheet is in that it contains the two same named but differently styled variables d. The right value was entered only after the third try. (To be continued in the Tip 45).
22 Graphical solution of the linear programming task
The Mathcad-worksheets in the figures of the pieces of Tip 15 and 17 solves the linear programming task. The Mathcad environment does not offer special visualization facilities for solution of such tasks. But with a certain skill this problem is solvable by means of a conventional Cartesian graph – see the figure. The core of the problem is described in the Tip 15.
To solve the linear programming task graphically there are defined two functions Constr (constraints) and CF (criterion function) with one argument Chair1 (quantity of chairs of the first model). Application of symbolical mathematics at the function output is dictated rather not by needs of inequality solution (solve) — that is easy manually, it serves for the graphical solution core illustration: straight line of the of criterion function slides upwards (animation of Mathcad is very pertinent here, when the system variable FRAME (the animation frame counter) is associated with the value of the criterion function). Solution will be achieved at the moment when the straight line (criterion function) «takes leave» of a polygon (the region of the constraint satisfaction). The graphical solution in question both suits for an integer task (a region bounded with a polygonal line is substituted by a multitude of points) and for a nonlinear task (line segments are replaced with portions of a curve: convex or concave).
The note — not numbered Tip. The item 2 illustrates two attempts to solve the criterion function relative to the variable Chair2 (chairs of the second model). The first operator solve (with a small font of its expression) defines the Euro to dollar rate as 1.2, and the second — 12/10. This substitution, equivalent on the first sight, changes the form of the answer factors (see also the Tip 2).
23 Color Cartesian graph
The given piece of Tip extends the subject of the Mathcad non-typical graphics, started in the previous piece of Tip.
Mathcad offers the three-dimensional graph coloring facilities: for example, surface vertexes can be colored more «warmly» while a concave remains more «cold». Unfortunately, this principle cannot be applied to a two-dimensional diagram. But if something impossible is very desirable, then it becomes possible. The figure exhibits a single argument function diagram, so that — see comments to the right of the diagram. Unfortunately, this book is not colored, but the reader can trust the author, that this line on the diagram is displayed with all rainbow hues: maximums are toned more warmly, and minimums — more coldly. The trick of this figure lies in that it presents not a Cartesian graph but a colored surface, turned to a spectator in a special way (with one «side»). It is worth to hide the operators and the forming matrix Ì, which elements — are values of the function under analysis (they are recurring over the columns), by locking up an appropriate area (see the top of the figure and also the Tip 62). This operation produces an absolute illusion of a colored Cartesian graph.
Not numbered Tip. It is expedient to «pull» operators, the forming user function y(x) and diversity limits of the diagram (x íà÷-x êîí) up to the diagram. Operator «º» (global assignment) proves useful here, it spreads its influence not only downwards that is executed by operator «:=» but also upwards.
24 Graphical solution of the set of two algebraic equations
To solve the problem of this Tip heading one should graph two Cartesian graphs and verify a point of their intersection. But this task may appear arduous for systems are not consistently represented in a suitable for graphing form: y1(x):=…, y2(x):= …. Equation sets are generally assigned in a common way — f1(x, y)=0, f2(x, y)=0 — variables in this notation are very seldom dividable to reduce this task to a straightforward one:
x + y = 5
y1(x):= 5 – x
y = x2
The figure depicts this problem solution: matrix M is filled up by means of two loops with a parameter (x value and y value). The matrix elements display status of equations on the x-y plane: if selection of x and y values with the augment of dx and dy shows any expression to reverse its sign in the given point, then corresponding element of matrix M takes the value unit, and zero — otherwise. We are scanning the diagram area as though as «popping up» (oscillographing — on the level line diagram, specially formatted) our curves (at the center of the Bernoulli lemniscate («entwined with ribbons»). How long will this «scanning» take — this is another question. Constant 300 (order of the matrix M) allows compromise settlement between speed of graphing and its quality.
The technique shown in the figure is suitable also for the single equation diagram construction in the form of: f(x, y)=0.
25 Radar diagram
To visualize tabular data one may find convenient a special business-graphics — radar diagram: rays are being drawn from the diagram center, then cuts are being made on these rays at a distance proportional to the elements of the visualized vector. Later the given cuts are being connected by straight lines. A polygon is being obtained, representing a certain phenomenon or a certain object by its area and shape.
The figure illustrates results of the matrix M data visualization through the radar diagram. The crux of the problem is: nine experts are engaged to estimate any ten characteristics of any object. Experts put down marks between 2 (unsatisfactory) and 5 (excellent). A regular polygon on the radar diagram (and this is nothing more than a polar plot of Mathcad) displays an average mark put by experts, while an irregular polygon illustrates an average mark put for individual characteristics: «weaknesses» and «strong spots» of the examinee become clear at once.
26 Start with a symbol and finish with a number
In the Tip 24 we have tried to solve graphically a system of two algebraic equations — we have defined the range of the system real root existence and by means of numerical methods we have found values of the unknown variables for the first approximation of the solution search (this is implemented in the Tip 44). But it is also possible to wedge a «symbol» in the «graph» — «number» line. The main point of the Tip. It is worth to try to apply symbolical mathematics of Mathcad to a task before its numerical solution. The figure illustrates analytical solution of our system of two nonlinear algebraic equations with the help of the Mathcad-operator solve. All the 24 system roots are found, of which the four real ones are depicted on the diagram of the Tip 24.
The figure displays only five points in mantissa (see the key float, 5). There are displayed 20 points by default (without the float key), and the maximum amount is — 250 points. (The same named command from the symbolical mathematics menu (the solve command) is capable of returning up to 4000 signs in mantissa. But this command operates only with equations, rather than with sets of equations).
The roots can be displayed in the decimal fraction form with a floating point (0.043778), instead of a «scientific» form (4.377810-2). To do that one should enclose an expression with cursor and push the «=» button. That makes possible a «numerical» answer to be formatted as needed (see also the Tip 3).
27 Commit, but audit
This Tip, actually, concerns all software intended for the mathematical task solution, not only Mathcad. So, having obtained the answer, don’t be too lazy to check it up. In the figure the system roots are substituted into the equations. For this purpose the matrix (from the Mathcad-worksheet of the Tip 26) containing system roots is entered in the variable Roots. Then the pairs of value from the Roots matrix rows are inserted in the x and y variables, which in their turn are substituted into the left-hand members of the algebraic equations of the system under control. Analyzing discrepancy the solution quality is estimated.
28 Three methods to preset the three-dimensional graph existence range
The surface construction in the Mathcad environment is anticipated by filling of a matrix, which elements — are the values of the two variable function under analysis in the orthogonal area. In this case the matrix row numbers are being related with the values of the first argument, and column numbers — of the second one.
The figure in the item 1 demonstrates the standard technique of the given matrix filling, described in the Mathcad program package documentation: the control points are the row and column numbers of the analyzed function matrix of the values.
The items 2 and 3 present alternative and somewhat more convenient techniques for the graph construction: the control points are the corner point coordinates of the orthogonal area (the item 2) or the center of the square area, which side is also set by the user. Moreover, if to «let down» operator of the analyzed function coordinate input (operator «º») close to the graph, then operations with it become very easy. The third technique is handy for animation: by means of the variable FRAME it is possible to «fly» over a surface varying one of the coordinates of the square center; one may move near or away from a surface varying the D value, observing the surface through the square «illuminator».
29 Plane graph in two ticks
Here is the standard, «three-step» technology of the Cartesian graph construction in the Mathcad environment (see the top of the Figure):
• The 1-st step: there is assigned the style of the function being analyzed;
• The 2-nd step: there is entered the Range Variable (we have set it as x), assigning the graph dispersion over the ordinate axis (x init and x fin) and the number of points on the graph;
• The 3-rd step: the command is given to plot the graph, which is subsequently formatted if some parameters do not suit the user, for this purpose corresponding dialog boxes are called; and the graph size is altered prolonging its outline (ordinary «window» technology in the Windows environment).
The Mathcad-environment, beginning with the 7-th version, allows quick (Quick Graph – see the second graph in the figure) technology of the «plane» graph (Cartesian and polar) construction:
• The 1-st step: see above, but the function under analysis is not necessary to be defined as user’s function, it is sufficient to insert it at y axes;
• The 2-nd step: the command is given to plot the graph; there appear another two defaults: the graph is constructed using a variable number of points depending on the graph size in the range between -10 and 10; as a rule the first default satisfies the user (a compromise settlement between the graph quality and speed of its construction takes place here). And the user has a right to break the second default setting another dispersion range of an argument.
The second technique has one more advantage (the main point of the Tip): the variable arg stays free from any numerical value, which afterwards can interfere with, for example, an analytical (symbolical) solution of a task.
30 Comments in a Mathcad-program
This Tip and some of the following ones are illustrated (for instance, the pieces of Tip 31 and 35 — 37) using a simple program, that formulates user’s function named V, returning the volume of a regular (right) cone depending on the diameter of its basis (d) and the length of its element (l — [el]).
The programming language Mathcad has no special facilities (operators) for program commenting. It is thought, that it is possible to comment one or another program operator placing text insets to the right (or around) of the program (see the item 1 on the figure). Furthermore, distinctions in the fonts of formulas and text insets make it difficult to insert a comment where needed.
We have investigated disadvantages of such technology in the Tip 5: it is possible to lose operators little by little «on the way» at dragging.
It is possible to insert comments into a program in the form of separate operators — text constants (the first line of the item 2 program on the figure). And one may act in a different way (see the 2-nd and the 3-rd lines): to record in a program a row-vector with two elements, first of which is a comment, and second one — is the program operator.
Comment by a matrix, which first element — is a text constant, displays a substantial disadvantage: the function ceases «knowing» how to operate with dimensional arguments. This limitation is due to that the Mathcad array can not store quantities of different dimension.
31 Comments in the form of the assignment statement
The Mathcad-program commenting techniques, described in the previous Tip, are not depicted in Mathcad’s help and documentation. Therefore they cannot be considered legal — the behavior of hollow text constants in the future versions of Mathcad is not known. The Mathcad designers not even always provide the complete compatibility of the version regarding to the documented techniques. And what to say of not documented techniques?! Hence, taking into account the fact that the Mathcad PLUS 6.0 environment (and this is the starting version of the Mathcad programming) does not offer text constants, there can be recommended the legal technique of the program commenting – see the figure: outside the program there is assigned something (we have it as zero) to the variable Rem, and then, within the program, this «something» is assigned to the variables which names — are comments themselves. If you will it is possible to insert spaces in these names (see the Tip 15) — we have «dropped» dash instead.
32 «Language» comments in Mathcad
The previous Tip has recommendations to comment the Mathcad-program operators, by shaping notes as it is accepted in the environment of the programming language BASIC. Though, this language places the keyword Rem before, rather than after a comment:
Rem This is the comment
The adherents of other programming languages can shape Mathcad-comments as displayed on the figure.
33 Several operators on one line of the Mathcad-program
The 5-th Tip recommends to position operators in columns when writing a Mathcad-worksheet. This order of operators is obtained of itself, if to push Enter after input of each operator. After input of the complete semantic fragment it is expedient to position some operators on one line to make the Mathcad-worksheet more compact. This is how almost all the examples in the figures of the book are arranged and how the Mathcad-documentation recommends to act.
Having a particular desire it is possible to impart «the second dimension» to a Mathcad-program too, which operators will be satisfied not only from top to bottom but also from left to right. The Mathcad toolbar has the button Add Line, but there is no button to create some free space on a line for a new operator. But this is practicable by other means.
In the Mathcad 7 Pro environment (see the first function on the figure), in order to add a new operator to the right of an operator on the program line, one should enclose this operator with cursor (not a part of the operator, but the whole operator!) and press the key «,». Whereupon to the right of the enclosed operator there will appear comma and a placeholder, where a new operator is inserted in. Everything was perfect – there were allowable rather bulky programs arranged as a whole on one display, on a single standard sheet of paper (À4). But here Mathcad 8 Pro has come into the world and «two-dimensional» programs, containing on one line several separated with commas operators, has appeared inactive in the environment of the new Mathcad version. This phenomenon once more time accentuates the fact that the technique of two and more operators on one line is not documented and can bug at any moment. The program serviceability was restored after proceeding to one dimension – i.e. leaving only one operator on a line (that was achieved by inserting new lines to the worksheet and «superfluous» operators were carried over there).
But the technique of several operators situated on one program line still remain very attractive. One may try to rescue such programs and make them operating in the environment of Mathcad 8 Pro. If to erase commas separating operators of a line (to bring cursor to comma and press Del or Back Space), the adjacent operators will be enclosed in brackets and the program will start working. Brackets which have appeared during the program reanimation can be erased (see the second function in the figure), but it is also possible to keep them. In so doing one should not forget that several operators – still remain the not documented technique. Such two or three operators on one line represent a delayed mine ready to «explode» at any time. (Truth to tell, the Mathcad environment has also «documented» mines. It happens very frequently that even a not sophisticated Mathcad-worksheet, formulated in due form, halts and needs modification after carrying it to a new version. One of the authors has a number of programs, which main destination – is to solve sets of the nonlinear algebraic equations. The block Given-Find perfectly operates in the fifth version of Mathcad, but in the sixth version it has started returning the error message instead of the answer. Unfortunately, such troubles are quite frequent at the transition to a newer version of Mathcad: «The best – is the enemy of the good»).
Two lines with a single operator on each one can be converted into one line with two operators in the following way: the upper operator is enclosed by cursor
a ß 1½
b ß 2
c ß a + b
and then Del is pressed, that results in such transformation:
a ß 1 b ß 2
c ß a + b
Furthermore, it is possible to enclose operator b ß 2 with cursor, press Del and obtain a line with three operators. There is nothing unusual in such operations: every text editor allows transformations like that by placing cursor to the end of a line and pressing Del; two lines (two paragraphs) turn into a single line (a single paragraph).
If the reader, in this or in any other book or in the journal article, will come across a Mathcad-program with several operators on a line, he should perceive it as a «stuffed» program which needs to be «unstuffed» – every line should keep only one operator.
Several operators on a line are fraught with one more trouble:
A := a ¬ 2 b ¬ 3 c ¬ 4
The question is, what value will be assigned to the variable A, after satisfaction of such «horizontal» program. In principle – it should equal four: Mathcad-program has to return the last value it has operated with (ñ ¬ 4). But it is not true. The value of the variable A will equal twenty four – the product of numbers appearing in the given program line. In the environment of Mathcad 7 Pro the value of A, returned by our program, equals four, that conforms to the program logic. There is your compatibility of versions! Apparently, the Mathcad 8 Pro compiler recognizes spaces between the operators – as missing multiplication signs (see the Tip 9). The program will return the correct answer, if to arrange it «vertically» again or even if to write the operator ñ ¬ 4 on a separate line.
From this the resume follows! If the reader will take the risk of writing several operators on one program line in the Mathcad-environment, he ought to remember that the last line of the program (or operand of the operator return) should keep only one operator.
The Mathcad programming language – is relatively «young». All programming languages (BASIC, Pascal, C, fortran etc.) allowed only one operator on a line in their early versions. Then («at a mature age») this restriction was lifted to make the programmer decide himself whether to write several or only one operator on a line. Let's hope, that in the future Mathcad versions the Add Line button on the toolbar will be supplemented with the Add Operator button and the described technique of making a program more compact will become legal.
34 Simulation of conventional programming languages in the Mathcad environment
Many Mathcad users, having operated with conventional programming languages, find the «multi-stored» technique of formula recording quite natural beyond the program body, and rather «wild» within the program. Such users have got used to the text («one-stored», single-level) formula recording technique. To leave habits alone (and they are, as is known — the second nature) and to make programs more compact one may remember the technique of the built-in function redefinition and renaming, described in the 20 Tip. But it is also possible to redefine the basic built-in operators. For the greater program compactness and for the further indulgence to the user habits the «multi-stored» Mathcad operators can be recorded like «single-stored» ones: integration, differentiation, summation, product etc. The figure formulates the function V, returning the value of the cone volume using «basic-like» operators and functions (sqr, «^» and so on). The ordinary way to formulate the function V is presented on the figure of the Tip 30. The figure in this Tip creates functions with non-standard names (+, -, *, /, ^, è **) by means of the technology of the non-standard symbol insertion, described in the pieces of Tip 14 and 16.
35 Operators in the programless mode
Before creating a program it is desirable to «sound» operators it should contain. The item 1 in the figure presents operators computing the volume of our cone by measured parameters — the foot diameter and the element length. The 2-nd item comprises these operators in the program body where variables r, S and h already become local and inaccessible for monitoring (inaccessible, if to be not acquainted with the following Tip).
If there is a loop put into a program (and, in fact, loops are the very reason requiring programming in the Mathcad environment), then, outside the program body, one may try to realize it through successive approximations, which peculiarities are outlined in the Tip 46.
Tips 36-99 in translating
 «Not numbered» pieces of Tip are frequent in this book.
 The operator background marking mode is cut off here — compare this figure with figures of the pieces of Tip 1 and 3.
 Numerical value output instruction in the Symbolic menu allows up to 4000 points
 The variable names in the Mathcad-worksheet of this Tip contain spaces. To obtain this result — see the pieces of Tip 14, 15 and 16.
 The minus sign is displayed just a little bit shorter than the subtraction operator symbol, though they are entered through pushing the same key.
 The Mathcad 8 environment returns several roots of the system in the case that the first approximation is a vector, rather than scalar.
 As a rule, any action in the Mathcad environment is executable in three different wayss. Thus, a built-in function can be defined using a corresponding dialog box and, moreover, its name can be keyed in or copied from another place.
 We have employed the addition operator as logic «and» in the figure of the Tip 6: the input value lower than –20 or higher than 40 degrees Celsius is considered to be an input error.
 There are true relative units, rather than US dollars (see the Tip 17): the currency type does not influence the task solution.
 A new function may have a name that consists of not a space (a chain of spaces), but of a «visible» symbol written in a white font on a white background (see the 50-th Tip). That will lead to the same effect.
 At a period when this book was being written there were appearing programs with an addition «2000» in their names. Just for a change there was possible to write also «MM», i.e. 2000 in Roman numbers.
??? There is used an English (Chair1) rather than Russian (I-ûé ñòóë) variable name for the Mathcad 8 pro symbolical mathematics is not capable of operating with such nonstandard variables.
 Rather, «headly», in a manner of speaking. Expression «manual calculations in the Mathcad environment» is frequent in the book. A Mathcad-worksheet is meant to be a simple sheet of paper.
 The max function operates here as logic «or» (see the Tip 12). The point is plotted on the diagram in the case that the function product appears to be negative near the given coordinate — that is functions are of equal sign each way from the point.
 It is possible to hasten calculations by not computing the two function values all the time, but using a previous one.
 Or, rather, of Maple V: symbolical mathematics from the Maple V program package is integrated into the Mathcad environment.
 But this is not always true. If, for example, a function is not defined at x<0, a graph is plotted only in the right half of the axis.
 The authors of the book deliberately illustrate the pieces of Tip by simple tasks, being of the opinion that the reader should not be presented of both an unknown algorithm and a new programmer method.
 The variable named l — [el] appears to be not very handy in programs for it is possible to confuse it with unit or capital Roman letter I.
 Mathcad PLUS 6.0 is popular in Russia for its Russified version and for it is capable of operating under Windows 3.* control.
 The involution operator is duplicated to satisfy both adherents of BASIC (^) and Fortran (**) languages. The given user functions are called in the form of infix operators.