Tip 39. Programme without the operator if   Fig. 39. Programme without the operator if

If we take into consideration that the alternative is the backup structural controlling construction, then the algorithm of any difficult program can be realized without the operator if from the panel of programming of Mathcad.

User’s function, which returns the root of an equation and uses half-division method, is created by the first program in the fig. 39. The cycle with pre-check, which loop body has the complete alternative. The operator if is excluded from the second program in the fig. 39: the alternative with one arm is the cycle with pre-check, which loop body is executed either once or never. The complete alternative (the alternative with two arms) is two incomplete alternatives: two alternatives with one arm. The alternative is the tool for shortcut motion along the program only on one direction (top-down), but the cycle is the shortcut motion on both directions (top-down and bottom-up). Hence we see the «uselessness» of the alternative. The continuation (opening) of the theme you will find in tip 45 and in one of the books «Programming in Mathcad».

Tip 40. The function if instead of the operator if   Fig. 40. The function if instead of the operator if

When you realize the branched algorithms the structural directive construction, namely alternative is used parallel with the cycle. For it we bring the Boolean variable in the program, which directs the alternative. Then we execute the following operations according to the first (it is better to say – according to zero) or the second (non-zero) script depending on what current value (0 – no, not zero – yes) the Boolean variable has. If every of two arms of the alternative have one by one operators, then it is possible to try to use the function if from the set of the built-in functions of Mathcad (please, compare two functions in the fig. 40) instead of the operator if from the programming bar. If at that something is bound to the same variable in two shoulders of the alternative then it is possible to do with only operator, which is used the function if. The example:

¬ if(a > b, a, b)   instead of   if(a > b, c ¬ a, c ¬ b)

Unfortunately, the method of change of the operator if to of the same name function stops to work in Mathcad 8 Pro, Mathcad 2000 Pro and Mathcad 2001 Prothe function метод_пол_дел_2 in Mathcad PLUS 6.0 and Mathcad 7 Pro gives the right result but if we transfer it in Mathcad 8 Pro and over it gets caught in an endless loop.

However for realization the branched algorithms out of a program it is possible to change the function if to the combination of the operators if and otherwise.

At that it is expedient to lead the operators return in the plural branching, which quicken the calculation (tip without number).

Actually the function if has become unnecessary: in new version of Mathcad the function is kept for compatibility with old versions. The same history happened with the function until (loop organization, which body has one operator), which the operator successfully changed. (Although in eighth and over versions of Mathcad-there is the function until and it works but there is not it Master function.)

Tip 41. Construction if with the compound operators in shoulders   Fig. 41. Construction if with the compound operators in shoulders

If there are two or more operators in shoulders of the complete alternative then they are written under the key words if and otherwise (see the functions in the fig. 33). If these blocks of the operators are enclosed in brackets then they will be situated left of corresponding key words (see fig. 41, where the function of search of minimum using the method of golden section is else «shortened» in comparison with its analogues in the fig. 33).

The important remark! It is impossible to place all operators on one line in shoulders of the alternative (see tip 33). Such program will work in Mathcad 7 Pro but it will stop to function (it will get caught in an endless loop or will return wrong answer) in Mathcad 8 and over.

Tip 42. Imitation of parallel calculations in Mathcad   Fig. 42. Imitation of parallel calculations of Mathcad

Placing of a few operators on one line (see tip 33) has one more explanation.

Two first operators of both programs can be executed in any order in the fig. 42 (for example, at first two is bound the variable b but then one is bound the variable a or vice versa as in the fig. 42). But it is better to execute these operators at the same time i.e. parallel! Compiler of Mathcad supports only the sequential calculations on uniprocessor machine. The calculation can be appreciably expedited by multisequencing the calculation. Here is the typical problem for such calculations - two matrixes are summed up: corresponding element of the second matrix is summed up every element of the first matrix.

At present time «parallel» versions of traditional programming languages (FORTRAN, for example) for work on multiple-processors machines appear.

When we place «parallel» operators on one line of Mathcad-program (see fig. 42) first of all we as if prepare it for execution on multiple-processors machine and in the second place we underscore cause-effect relation of operators and of operators’ blocks.

Tip 43. Fixing of program blocks   Fig. 43. Latchup of soft blocks

In all programming languages there are the key words and the symbols, which fix begin and end of the separate soft blocks. In the programming language Pascal – the words are begin and end, in the programming language C – the symbols are curly brackets, in the programming language BASIC – the end of a line, the key words are Next, EndIf, Then, Else and etc. Soft blocks of Mathcad are fixed by the vertical line, which appear (be prolonged) after keystroke Add Line on the programming panel. But if there is only one operator in a soft block then it will not be outlined by the vertical line. It decreases a little «readability» of the Mathcad-program. In the traditional programs a separate operator and a block of operators can be marked out by the paragraphs from the left side of a display or of a paper showing this way the nesting of structures. Mathcad is devoid of such possibility too or rather a paragraph is entered not by a programmer but by the system and more over it is not so much showed in case when there is only one operator in the block.

To pick out one operator by a vertical line in Mathcad-program it is necessary to add the commentaries to it (see tip 30). As the commentaries you can use the key words or symbols of programming languages that are acquainted the certain user of Mathcad. In the fig. 43 reader sees «almost Pascal» Mathcad-program of search of a root of the algebraic equation by the half-division method, which «normal» version you can see in the fig. 39.

Tip 44. Solution of a system of algebraic equations in the program body  Fig. 44. Solution of a system of algebraic equations in the program body

In Mathcad the built-in function Find, which works together with the key word Given, is meant for solution the system of nonlinear algebraic equations (see the example of the solution of such problem in tip 10). Unfortunately similar technology is impossible in Mathcad-program as since we can not put in the key word Given in it. There are three solutions of the problem:

1.      It is possible to try to reduce the nonlinear system to linear one using, for example, change of variables. After it the problem is solved with the help of the built-in function lsolve, which does not demand the key word Given.

2.      It is possible to try to reduce the system of equations to one algebraic equation using variables substitution. Search of roots of the equation is possible with the help of the function root in a soft block (general case) or polyroots (equation-polynomial). Solution of the problem can be obtained with the help of enclosure of the built-in function root: root (f1, root (f2,… and etc.

3.      Starting with the eighth version the functions Minimize and Maximize are brought in Mathcad, which orient for solution optimization problems. These functions can work both with the key word Given (constrained problem – see the example in tip 15) and without it (unconstrained problem). This allows searching roots of the system inside a soft block with the help of the function Minimize. At that, for example, it is possible to minimize squared maximum coherence of the system: to find such values of the variables that left and right parts differ each other minimally – see fig. 44. For it a subsidiary user’s function F(x, y) is formed. It returns maximum values (max) of deviation square of left and right parts of the equations that the current problem includes. Unfortunately it is impossible to define the user’s function inside the program (limitation of Mathcad). After it the main soft user’s function roots(x, y) is formed. Its arguments are the first approximation to solution of the system. The function returns a matrix, which the first column keeps commentaries, the first element of the second column keeps the decision vector and the second element of the second column keeps maximum residual of the system that is used for control of the accuracy of the solution. In the fig. 44 four real roots have been found. Graphical solution of the problem was considered in tip 24 but the analytical one – in tip 29.
Attention! Mathcad-document in the fig. 44 gives the right result only in Mathcad 8 Pro and it stops to work in Mathcad 2000 Pro. The author let know of the bug the developer of Mathcad. The main point of the answer was the following: «The functions, which include in Solver of Mathcad (group Solving in Master functions of Mathcad:
Minimize, Maximize, Find, MinErr, polyroots and root) are not the creation of MathSoft, Inc. but they are purchasing from the firm Frontline, Inc. That’s why all claims we have to direct to it». Nevertheless the mistake has been fixed in Mathcad 2001.

Tip 45. Units in a program  Fig. 45. Dimension inside a program

In Mathcad a function can return a few values, which are united in a vector or a matrix (array). The given soft user’s function returns the values of perimeter and square of a triangle depending on a length of its side in the point 1 in the fig. 45. But if user wants the arguments to be dimensional ones for the function returns the value of perimeter and square of the triangle with corresponding units (length and square – point 2) then failure will wait for him. At that misleading error report will appear: «Mismatch units». User thinks that he sums meters with kilogrammes, for example, but the cause hides in another thing – a vector of Mathcad can keep the variables only the same dimension. We have already met with this phenomenon when we considered the methods of programs debugging in tip 36 and 37.

It is possible to make the function in the fig. 45 to give two values not at once but by turns (see tip 37) – depending on the value of the additional argument (point 3): if i = “Perimeter” then the function Parameters_triangle returns the triangle's perimeter, if i = “Square” then the function returns the square. But in this case the work with dimensions will result in bugs too – see point 4. The function Parameters_triangle like any other function always returns unit of a dimensional variable from the first «returned» line of the program.

The developer of Mathcad was told about the given mistake (the program in the fig. 45 always outputs dimension of an operand of the first operator return). The mistake was partly fixed: if we install the first patch on Mathcad 2000 Pro – after which number of the version becomes longer on letter «A» then the program will return … non-dimensional value in point 3 in the fig. 45.

In tips of the firm MathSoft (see Tip of the daypoint 71) dimension inside the programs is not recommended to use. At that for some reason the loops are mentioned, but they do not have the possibility to work with dimensional variables. In this case the point is not only in loops, but also in return mechanism of variables and certainly in arrays, which are not able to keep variables of different dimensions.

In point 7 in the fig. 45 the alternative is changed on two loops While, which operators body execute either one time or not once. About this method we have written in tip 39 but here it is used for the attempt of rehabilitation of the program in the fig. 45. We have to admit that the attempt has turned out to be failed: both alternative and loop work with dimensions not completely correctly. Way out is in tip 52 and… in Mathcad 2001 Pro where the mistake has been finally fixed.

Tip 46. Protection from fool and from joker   Fig. 46. Protection from fool and from joker

There is a number of special jokers who get pleasure from the attempts of practical jokes of a computer: «Computer, you think too much of itself. Let’s look, if you swallow it or not!» – they think this way or about it when the input wittingly «wrong» information in the computer. Computer has to be able to protect itself from «free» or «forced» mistakes of user.

In tip 6 and 21 we have already given the advices about the protection – not a computer, certainly (though how shall I put it: for example, user can set in a computer wrong floppy disk, open shady letter, which he has received via e-mail, bring in virus and break soft or hard part of a computer), but a user himself – from the mistakes of input: control of hit inside the possible range (tip 6), control of data addition of necessary dimension ().

As rule it is impossible to provide for all potential mistakes of a user in advance. Usually these mistakes appear during operation of the program. If a mistake is noticed and it resulted in unwanted consequences then the program will be added by new operators and functions, which prevent from appearance of the given mistake.

Our old problem, which returns volume of a cone depending on its geometrical sizes, is showed in the fig. 46: diameter of base (d) and length of the cone’s generatrix (l) supplemented by control of the ration between d and l. When you protect a program it is important not only to lock its work if the initial data are wrong but also to give maximum informative error report: built-in or user’s one. But the embedding in the program of user’s error report (non-dimensional text constant) breaks its ability to work with dimension values: the function V returns non-dimensional values in case if its arguments are dimensional ones.

It is too difficult to suggest what mistakes user can do when he inputs initial data. It is possible to go of contraries – to accept only right data and to identify and cut off wrong data. In this case the operator error will be useful but not the function: ¾ ¾processing operator of alert conditions. The first operand will be executed in the given operator if the bug takes place during the execution of the second operand.

Tip 47. Critical points on a graph   Fig. 47. Critical points on a graph

They (see the title of the tip) can be marked not only as constants but also as variables, expressions and functions.

The method of numerical search of local minimum and local maximum of user’s one-argument function is showed in the fig. 47. As rule such work is accompanied by the graphical control of the result – construction of graph inclusive the obtained points in Cartesian coordinates. Mathcad allows marking to points on the graph by red dotted lines. For it in the window for formatting graphs on the tab Axes we place ticks (the options are turned on) Show Markers. After it couple of placeholders appear on the graph near the axes y and x where user generally writes constants (numbers). However we can input their variables (expressions, functions) and we have done it: not fixed, but floating markers have appeared and they mark critical points (minimum and maximum) on the graph. If we find new points of minimum and maximum (of modified initial function, for example), then the dotted lines for fixation them on the graph will remove automatically too.

As well it is possible to insert not only numbers but also the expressions in placeholders near the axes y and x for floating fixation of graph scattering by the ordinate and the abscissa. The term «floating fixation» is strange enough (it is like «boot soft-boiled») but in the given case it describes quite exactly the technology of graph formatting – we put variables in places where developer prescribes to put constants.

It is possible to recommend one more method of fixation the points on a graph for additional formatting. Very often when one publishes the results of calculation in Mathcad the graphics are «froze» by the keystroke PrnScr (or Alt + PrnScr) and then they design as a general picture in the environment of some graphics editor. In this case it is possible to add the graph as you wish. Rather well to give notice people who try to repeat such perfect picture in Mathcad of that such «appearance» of the graph is not provided in Mathcad.

Tip 48. Different scales of abscissa axis  Fig. 48. Different scales of abscissa axis

Very often in Cartesian coordinates of two and more one-variable functions some curve «lies» down the axis x because of small value of the corresponding function in selected range of the argument. In Mathcad it is impossible to have two axes y with different scales (we can have it, for example, in Excel). Nevertheless the solution of the problem exists.

The view of the function and its derivative is showed in the first graph fig. 48. «Dispersion» of values of the function and its derivative are not commensurable in the given limit of the argument (8-9) in the graph.

In the second graph values of the function are multiplied by 10 (the coefficient is sorted out experimentally) and it «reanimates» the graph: now one can see that derivatives equal zero in the points of minimum and maximum and etc.

The second difference of the graphs in the fig. 48 allows formulating one more tip (without number).

The derivative is constructed by numerical calculation of its values in the first graph. It is long enough and quite inaccurate procedure. Before we will construct the second graph we form the additional function dy, which keeps the derivative of the function y appointed by the tools of symbolic mathematics of Mathcad: speed and accuracy of construction of the graph increase abruptly.

 Here we have the same situation as in the programs in the fig. 38: control of the execution of the cycle is leaded by the value of the argument, but not the function: а ≈ b – the area of finding the root has narrowed almost till point.

 Compatibility «botton-up»: the documents that were written in seventh version of Mathcad have to work in newer versions but inside out it is not obligatory.

 Tip without number: it is worth sometimes to use the function Find for solution of the linear systems (instead of the function lsolve). For example, in this case it is possible to bring in physical values. More over the block Given-Find makes the solution more visual.

 The function lsolve appeared in Mathcad not long ago. Before it the systems of linear algebraic equations were solved only with the help of the operator X := A-1 B, where А is the matrix of the coefficients of unknowns quantities but В is the vector of absolute terms. Tip without number: it is not worth to use new tools of Mathcad without special necessity. If it is possible to say it makes a calculation deeper in terms of compatibility with old versions of Mathcad. More over «old horse does not spoil a furrow» (see tip 66).

 It is possible to do with the help of the command or the operator substitute of symbolic mathematics of Mathcad.

 For example, under the name «I love you» (tip was written in May 2000, when virus of the same name raged in Europe and).

 Mathcad very often «sins» such mistake – it breaks the execution of the program but gives not quite right built-in error report (see for example, point 2 in the fig. 45).

 In Mathcad 2000 it became possible to «bring» text in the graph, which was written on free place of Mathcad-document in advance.

 Certainly it is possible to introduce the Excel's graph in Mathcad-document. But we have come to the agreement that our tips are limited only by the tools of Mathcad.

 Reader can make sure by sight of petulance of numerical calculation of the derivative, if he constructs, for example, the graph of the derivative of function of charge's calculation mentioned in tip 58.