Tip 59. Color in Mathcad-programs

  

Fig. 59. Color in Mathcad-programs

The traditional programming languages (Visual Basic, for example) paint the data-sheets of programs in the following colors:

·        commentaries – green color;

·        built-in constructions of a programming language – blue color;

·        everything else (user’s constructions) – black color.

If you really want it is possible to paint this way Mathcad-programs (see fig. 59).

Color in programs is a special story, which has been told about in the sketch «Color in programs» (http://twt.mpei.ac.ru/ochkov/Color_in_Program/index.htm).

Tip 60. Dimensional zero value

 

Fig 60. Dimensional zero value

When you output zero dimensional value Mathcad shows needless formalism adding unit to zero. This not quite corresponds with generally accepted standard of work with dimensions: for example, we say that some equipment was on the grade (on the height) 20 meters, but another one – on zero height, at that we do not specify the unit (meters). If we speak about absolute scales of measure, then the output of zero values we specify only dimension, but not units.

How it is possible to remove unit of zero value in Mathcad-document is showed in the fig. 60. For that it is enough to make this unit … invisible one – to assign it the «invisible» style (we write in white and white).

Remark. It is worth to hide unit only in that case if it is known in advance that the insertion operator of numerical value will give only zero value. At that it is necessary to remember that zero meters and zero millimeters are not the same things if we speak about numerical (approximate) mathematics of Mathcad.

One more example of hiding a constant by change its color from black one to white color is showed in the fig. 60à.

Fig. 60à. Invisible argument of a function (an operand of the operator)

In Mathcad there is undocumented function time, which returns the time (in seconds) of work of a computer[1]. The argument of such function may be any number, zero for example and that is done in the first part of the fig. 60à, where the time of addition of numbers of natural scale from 1 to million in Mathcad 2001 is fixed. In the second part of the fig. 60à the function time is called already in the form of the prefix operator, which operand is written in white and white and it is invisible on the display’s screen (on the printer’s paper). As since this argument (operand) does not have an influence on the value that has been returned by the function (the operand). Hence there is no reason to show it.

Remark. Qualitative assessment of new machine arithmetic that has been built in Mathcad 2001 is showed in the fig. 60à.  This arithmetic allows quickening the calculations.

See http://twt.mpei.ac.ru/ochkov/Invisible/Eng/Index.htm too.

See http://twt.mpei.ac.ru/ochkov/Units/Part_1_eng.htm too.

Tip 61. Control of dimensions in a program

 

Fig. 61. Control of dimensions in a program

As we can see in the fig. 21 it is possible to control a dimension of an input value out of a programs.

It is showed in the fig. 61 how it is possible to carry on such control inside a program when you call the user’s function. It is done by already described in tip 21 technology: the value that keeps controlled dimension is added and at the same time is subtracted to the controlled variable. There are no changes with the value, which keeps in the variable if it keeps necessary dimension (in the fig. 61 the variable diameter and length have to keep only the length and nothing else, but the variable density – density). Otherwise the mistake appears (see three last «ablush» operators in the fig. 61). The «program» variant (see the fig. 61) only differs from the technology that is showed in the fig. 21 that there is no necessity to have the same name but different styles variables.

As well in the programs the function UnitsOf is to the point. This function returns the dimension of its argument. The function UnitsOf has very interesting feature. It has been inputted only in Mathcad 2000, however it already was in the program but not in the documentation, Help and in Master function of Mathcad 8. It is very remarkable fact: it turns out that new functions can appear in Mathcad earlier than the documentation will tell about it. In early version of Mathcad it is worth to «probe» new, unknown functions. At that it is not worth to enable them in the calculations (tip without number). This tip fluently spills over the tip 62, where it is recommended to «be behind one step» in use different software. User of Mathcad 8 can input the function UnitsOf in the document if he has forgotten that there is not still such function. By the way it is one more reason, which decides in favour of the method of input the built-in functions with help of Master function, but not with help of manual type of their names. Point is that the function UnitsOf (and similar) in Mathcad 8 is able to be unfinished. The developer of Mathcad did not have time to shape it up: in the program they left it (forgot?), but they did not describe in the documentation, Help and in Master function.

See http://twt.mpei.ac.ru/ochkov/Units/Part_1_eng.htm too.

Tip 62. Structuring of Mathcad-documents, or «The best – the enemy of good»

 

Fig. 62. Structuring of Mathcad-documents

Three types of Mathcad-document are showed in the fig. 62 where the volume of a cone (as well this problem is considered in tip 30, 35, 36, 37 and 69) is calculated. The upper part of the fig. 62 is the name of the document (commentary[2]) and the horizontal line, which is marked the slammed area, where all calculation is situated. If we open the area, then user sees the middle part of the fig. 62: the title and the opened area, inside of which there are three subtitles and three new slammed areas (the sub areas – if we remember the word structure in the title of the tip), which contents in its turn is showed (opened) in the lower part of the fig. 62.

Embedded areas (i.e. the areas that are created inside the other range – the area of upper level) are created by the moving the limits of the area of lower level (subtitle) in the area of upper level (title). It is impossible to create new area inside already created one. This feature of Mathcad it is possible to consider as a mistake or limit. More over when a user try to open the embedded slammed area then sometimes the system of Mathcad does not «understand» completely what the user wants and it opens incorrect areas. The author removes this tip but gives new one on base of it.

The possibility of separation («enclosure») of the area (areas of the same level) for the following its protection from the editing (Lock) in Mathcad-document has appeared in the seventh version of the package. Apart in the eighth version it is possible to slam (Collapse) these areas and we use it forming the fig. 62. It is possible to say that «the age» of the tools of creation and protection of an area is two «years», but the tool of slammed is one year. Following such criterion it is possible to sort out all tools of Mathcad and to draw enough sound conclusion: the «younger» tool the higher probability to find a mistake or a defect[3] in it. It is not worth to use the tools «with one passing year» for writing serious programs (the main point of the tip).

It is worth to formulate this tip to wide extent the following way: the newest version of Mathcad it is possible to use in familiarization and educational purposes. Some serious work it is better to do in the environment of rolled versions.

This principle of «the old horse, which does not spoil the furrow» touches not only soft but also hard part of the computation systems. The computers, which control the serious industrial projects, work under DOS, but not under Windows.

Tip 63. Automation of symbolic calculations

    

Fig. 63. Automation of symbolic calculations

One of the imperfections of the symbolic mathematics of Mathcad[4] is so that it is difficult enough to automatize the analytical transformations in such a way that introduced changes in the initial expression tell on all line of transformations at once.

One of the solutions of this problem is showed in the fig. 63 – union of the operators «:=» (input of numerical value) and «®» (output of symbolic result). Outputted expression (in our case it is any user’s function with the name y) as a by-product (see the icon of the question after the title of the tip) forms new user’s function dy(x), with which all subsequent transformations are possible (see fig. 63). Now if we make changes in the initial function y(x) then the result will change.

The process of the qualitative change of the tools of symbolic mathematics «stiffened» on the seventh version of Mathcad. This explains the following way: as we have said above the symbolic mathematics of Mathcad is not work of MathSoft, Inc., but acquisition of the elements of the symbolic mathematics from the famous package Maple made by the firm Waterloo Maple, Inc. In the tip 63 we underline (see once more the icon of the question after the title of the tip), that the forming of the user’s function with help of symbolic transformation (the line of the operators «:=» and «®») is by-product (undocumented method). And symbolic mathematic is by-product (tool) of Mathcad.

Tip without number. Very often the «fruit» of the symbolic transformations «lie down» in Mathcad-document badly. They are bad both in its form and its matter: reformed by the operator «®» the expression takes along the right pages across of the document; simplification of the formulas is not done completely and etc. In this case the author recommends to finish off all simplifications manually and to carry them in Mathcad-document in the form of the figure following the technology in tip 56 relating to graphs of Mathcad.

Tip 64. Work with degrees Celsius

   

Fig. 64. Work with degrees Celsius

In tip 16 the recommendations about work with units of physical values wanting in the list of built-in in Mathcad have been given. There is nothing difficult: user's units are attached to built-in ones with help of the factors (MPa := 106 Pa end etc.). But this rule cannot be applied for temperature. Point is that there are two notions related to these physical values – unit of temperature (Kelvin, degree Celsius, Fahrenheit degree and etc.) and scale of temperature (Kelvin, Celsius, Fahrenheit and etc.).

Celsius (or Fahrenheit if we speak about British system of measure) is the outboard scale, which, nevertheless is widely used for reflection the value of the temperature[5]. However the temperature of absolute scale Kelvin (or Rankine in British system of measure) are used in scientific and technical calculations. The formulas of the conversion of the temperature from one scale to another one are simple enough (see «Day’s tip» ¹74), but above-mentioned technology (see tipå 16) of the input of user’s units does not work in them.

One of the solutions of this issue is showed on a simple problem in the fig. 64two temperatures are given; we have to find a difference between them. It is clear, that it is not arithmetic problem, but metrological one: all values have the dimensionality of temperature. At that user can input and output the value of temperature in any of four units and scales: Kelvin degrees (scale), Rankine, Celsius and Fahrenheit. If there are no problems with degrees (scales) Kelvin and Rankin (they are built-in in Mathcad: R = 1.8 K), then with respect to degrees (scales) Celsius and Fahrenheit we have to use some ruses. These ruses are the main point of the tip.

Eight objects with the names °C and °F are introduced into the calculation:

·        two functions with the name °C (the first one °C(t) := (t+273.15) K style – Variables, and the second °C(t) := (T/K-273.15) – Units 1, at that the color a type of the name of the second function is white. It is invisible on the screen of the display[6])[7];

·        two constants with the name °C (first one is °C := 1 style – Units 2, but the second one is °C := K – Units 3);

·        two functions with the name °F (the first one is °F(t) := (t+459.67) R the style – Variables, and the second one is °F(T) := (T-459.67) – Units 1, at that the color a type of the name of the second function is white. It is invisible on the screen of the display);

·        two constants with the name °F (the first one is °F := 1 the style – Units 2 and the second one is °F := R – Units 3).

The names of the objects are the same, but these are different objects, as since they have different styles (Variably, User 1, User 2 è User 3).

When we work with temperature there are three situations. Above mentioned function and constants help to react for these situations correctly:

Situation 1. It is necessary to input the value of temperature on a scale Celsius (or Fahrenheit). For it the first function °Ñ (or °F) with the style Variables is called in the form of the afterfixing operator: t1 := 0 °C (or t2 := 212 °F). At that the variable t1 (or t2) is assigned the value of temperature on an absolute scale of measures.

Situation 2. It is necessary to output the value of temperature on a scale Celsius (or Fahrenheit). For this purpose in the operator «=» it is necessary to make the variable that we are going to outtype as the operand of the prefix operator, which name (symbol) is °Ñ (or °F) – the second above mentioned function: °F t1 = 32 (or °Ñ t2 = 100). If we make invisible (see tip 19) the name of the function, and we print in addition the first constant of the user to a numerical constant in the answer °F (or °Ñ), then the illusion of an output the absolute value of temperature on a relative scale will be complete:  t1 = 50 °F and   t2 = 100 °Ñ.

Situation 3. It is necessary to output the value of a difference of temperatures: t2 – t1, for example as in the fig. 64. In this case we can apply the usual rule of Mathcad. This rule is a change of unit Ê (or R) to °Ñ (or °F) – to the second constant that we have defined.

The three above mentioned methods allow completely to realize work with temperature: input of a value of temperature by any of four scales, output of a value of the temperature, input and output of a value of a difference of the temperatures.

Relative scales meet in the engineering calculations not only during the work with the temperature. If we want to know what pressure of water was at 100 m depth, then we’ll obtain the reply: 10 atmospheres. But if we want to define any parameter of water pressure dependent, water density for example, then we have to input into the calculation not 10, but 11 atmospheres: to obtain absolute pressure we have to add atmosphere pressure to overpressure (relative one).

Relative scales are used, for example, in measure of length. The growth of an adult man was measured in vershoks (vershok:=7/4 inch) in old Russia. But when one said that some man has the growth of 12 vershoks, he had in mind two arshines and 12 vershoks (1 arshine=16 vershoks). Point is that the growth of an adult man as rule kept within the range from 2 to 3 vershoks (from 142 to 213 cm). That’s why two arshines were usually left out, using relative (relatively two arshines) scale of measure the length (this problem is described more detailed in the sketch «Mathcad and some secrets of literature» – http://twt.mpei.ac.ru/ochkov/Gerasim/Gerasim.htm).

The «tricks» that are used when you work with degrees Celsius and Fahrenheit (the prefix and postfix operators, the invisible symbol of the operator) can be applied for work with decibels (see fig. 64a).

Fig. 64a. Work with decibels

Bel is the common logarithm of the ratio of two-dimensional values and the second of them (denominator) is taken as some base. Decibel is correspondingly (see fig. 89) one tenth of the bel. Usually decibels are connected with the acoustic intensity, but they can be applied to any dimensional value. This way the value will be deprived its dimension.

See http://twt.mpei.ac.ru/ochkov/Units/Part_1_eng.htm too.

Tip 65. Two graphs of one function

  

Fig. 65. Two Plots of one function

The question about points, on which the interval of construction of graph of the function of one variable is divided, is toughed in tip 29. The number of such point is given either by a user (see the first graph in the fig. 29) or by the system Mathcad (the second graph). The description of the advantages of the second technology is the main point of tip 29. In the given tip we’ll speak about the defects of automatic choice of the points’ number and about ways of solution this problem.

If a few lines, which visualize a few functions, are constructed at the same graph, then, as rule, they are registered in different ways. Usually the color is used for it in the screen of the display. But if it is necessary to print the graph but a printer is black-and-white, then we have to mark in addition the lines using circles, squares, criss-crosses[8]. In the case when the points are near each other the marking signs can flow together in one line, which will look like the openwork lace (see the second graph in the fig. 29). On the other hand if we «disengage» the graph – to decrease the number of points, which form it then the quality of the graph will become worse (see the first graph in the fig. 65). Point is that the system Mathcad draws the line[9]segments between the different points and because of it the graphs are serrated. The way out is showed in the same figure – in the fig. 65 where actually two graphs of the same function are drawn. The first graph (the argument x1) is continuous line without marking signs, but the second one (the argument x) is points without connecting line[10].

Tip 66. Groups of the operators of a program

  

Fig. 66. Groups of the operators of a program

Two methods of software-based forming of user’s function with the name ηt and three arguments P1, P2 and T1[11], which returns … are showed in the fig. 66. But now we are interested in the form of the function but not in what the function[12] returns, i.e. its content.

The algorithm of forming this function is very simple: the values of three local variables IT, Ií and q1 are calculated by line of the operators. The function returns the value of the expression, which is formed of these variables – (IT - Ií) / q1. In the first function all operators are written abreast[13]. But in the second function all operators are grouped so that the separate parts of the program are the independent (see tip 30) notional blocks with commentaries – calculations of values of the main variables which are part of the «returnable» formula (IT - Ií) / q1. This increases appreciably the «readability» of the program. Apart the separate parts can be withdrawn for debugging and compilation. At the end of the fig. 66 this operation is executed about the variable x2.

Two functions, which are showed in the fig. 66, differ not only by their form but also by their contents. Point is that the call of the function hSS[14] is duplicated in the different parts at the second forming of the function.  It is made with a view of independent debugging of the program’s parts.

Tip 67. Trace of search a minimum

 

Fig. 67. Trace of search a minimum

The function minimum, which is created with the help of programming, is presented in the fig. 67. The function returns the coordinates of a minimum point of a function of many variables (its name is the argument f) and it begins the search with the initial approximation points (the vector X). The algorithm of search the minimum is very simple. From the initial approximation point according to all coordinates the step D is made (the third argument of the function minimum). In new places[15] the values of the minimizing function are calculated. Where the function has minimum the crossing is made to this point; the given point becomes new approximation point of minimum, but all above-mentioned operations («probing» of the function close to the approximation point) are repeated. If it turns that all new values of the function increase close to the new point, then the step of search (D) will be decreased twice and all operation will be repeated again. The search finishes when the value D becomes less then TOL (the system variable of Mathcad, which regulates the accuracy of calculations).

The function minimum returns not only the vector of values of the variable where the function is minimum but also the matrix with the «history» of search. This matrix we have to transpose (ÌÒ), to define the number of the last column (L) and to calculate it (Ì<L>). But such overload of the program is not vain. The simplest but nevertheless very effective tool of debugging the programs is observation the intermediate results (the main point of the tip): the traces of search the minimum of two functions: õ8 + y6 and x8 + y6 + z6 are showed in the fig. 67.

Tip 68. Abortive trace of search a minimum

 

Fig. 68. Abortive trace of search a minimum

It is possible to modify the function, which is showed in the fig. 67 – to add one more argument: maximum number of cycles of approximation to the minimum. The modified function minimum is showed in the fig. 68. The main point of the tip: when you debug a program, which has a high probability of circularity, then it is worth to limit the number of execution cycles. It is possible to take away the limitation of number of execution cycles and to insert as the formal parameter N infinity ().



[1] What moment – it is the single question. Certainly there is not such information in the documentation of Mathcad, as since the function time is not documented. But we’ll be interested in the difference between two such values, i.e. the time of execution of one or another part of the program, but not the absolute value. By chance the author «met the function time, when he checked up «availability» of the same name variable for keeping the user’s value according to the method, described in tip 1.

[2] The other method of the commentation of an Area is described in tip 88.

[3] The author has found very serious mistake in the slammed tool. If in the area there is the operator that is inserted using the command Math Region from the menu Insert, then such operator turns off from the work after we slam the area. As we have said above the mistake was corrected.

[4] Symbolic mathematics (mathematics of analytical transformations) is enough «foreign body» in Mathcad. This way we can explain but not justify the fact that very little time is attended to symbolic mathematics in the given book.

[5] Celsius is not simply widely used but it is mainly used in «human scale of measure «it is cold – it is warm – it is hot».

[6] For we can see on the display it is possible to change the color of background from white to blue, for example.

[7] The symbol «°» (degree) is inputted with help of pressure Alt+0176.

[8] Lines on a graph can differ the thickness. But for identification the lines on the graph we do not need the quantitative differences (thicker – thinner) but we need the qualitative differences (if there are markers and what they are). One more way out is use dotted lines and the other lines with gaps. The firm-creator as it knew this defect in 2000-th version of Mathcad has provided for eventuality of marking the line on a graph by explaining text (or symbol – see the eagle in the figure of the bridge in the sketch «It is no trouble at all for us to built… a Bridge…» – http://twt.mpei.ac.ru/ochkov/Bridge/Bridge.htm). We have to refuse from the color on a screen of the display if potential user (viewer) is color-blind person: see the sketch «Color in programs» (http://twt.mpei.ac.ru/ochkov/Color_in_Program/index.htm).

[9] For example, the package Excel is able to smooth the points.

[10] This way the experimental points are marked on a graph.

[11] The function name and names of its formal parameters, as well names of local variables have textual index. Their defect is that all mix up textual index with numerical one.

[12] It returns cycle efficiency (coefficient of efficiency) of the simplest steam turbine cycle – the Rankine cycle, if it is interesting for somebody. By the way one of two units of temperature, which are built-in in Mathcad uses the Rankine name (see tip 64).

[13] For compactness a few operators are written on one line (see tip 33).

[14] hss is the enthalpy (h) of superheated steam. At the site http://twt.mpei.ru/orlov/waterprosteam reader will find collection of the functions, which return heat-transfer properties of water and water vapour. The main developer of the program is K. Orlov (orlov@twt.mpei.ac.ru) – the author’s student. He has done 55 % of work. This number is fixed during registration of the program. The other 45% of work are distributed between À.À. Àlexandrov (alexandrovAA@mpei.ru), À.V. Ochkov (al@twt.mpei.ac.ru) and the author of the tips (ochkov@twt.mpei.ac.ru). The program WaterSteamPro™ is transmitted by the technology shareware (see tip 55).

[15] There are four of them in two-dimensional problem; there are six of them in three-dimensional problem and etc. It is possible to optimize the search of a minimum if we’ll keep in mind the previous point. In this case when we optimize the function of two variables then, for example, we’ll have to «probe» not four but only three points.