Professional Documents
Culture Documents
IntroductionSMathStudio088May2010 PDF
IntroductionSMathStudio088May2010 PDF
IntroductionSMathStudio088May2010 PDF
Getting started
You will find the icon for SMath Studio in the folder SMath\SMath Studio under your
Program Files folder in your Windows machine, or under your Program Files (x86)
folder if you have a 64-bit version of Windows. The program is called
SMathStudio_Desktop.exe. You may want to create a shortcut to the program to place in
your desktop or in your Quick Launch. Double click on the program icon or on the
shortcut to open the SMath Studio interface.
You can go back one page in the Reference book by pressing the option Back, or return to
the Contents page by pressing the option Home. The menu option Copy lets you copy
equations from the Reference book that you can then paste in the main interface. To copy
a specific equation, first highlight the equation you want to copy, then press Copy, and
paste the equation (using, for example, Cntl-v) onto the main screen.
The Help menu also contains the option Examples, which opens the following menu of
examples:
Click on a given example to select it, and press the [ Open ] button. The worksheet
corresponding to the example Numeric integration method (Simpson's rule) is shown
below.
The Plugins item in the Tools menu produces the following interface:
SMath Studio 0.88 includes the three plugins shown above, namely:
Selecting the option Operator in the Insert menu produces a list of Boolean, arithmetic,
and other operators, e.g.,
The option Text region in the Insert menu allows the user to insert text fields in the
worksheet with the purpose of documentation. This is equivalent to pressing the double
quote (") after clicking in any position in the worksheet. For example:
Inserting a Separator simply means inserting a horizontal line to separate regions in the
worksheet, e.g.,
The insertion of plots and pictures will be illustrated in other sections of this document.
The drop-down menu shows functions day (a unit, described in the box to the right in
terms of the unit s, or seconds), deg (a unit, degrees), det (determinant of a matrix), dfile
(delete file), diag (create a diagonal matrix out of a vector), etc. Scrolling the cursor to
the right, up or down, will provide additional function or unit definitions.
The only active options shown above are Auto calculation and Recalculate page. The
meaning of these, and the other options shown above, is obvious.
To illustrate the use of some of the other items in the Calculation menu, click
somewhere in the main screen and enter the expression 5+7/3. Then, click on
the expression, and drag the cursor over the expression to highlight it. The
highlighted expression should look as shown in the figure to the right.
To check the use of the option Determinant first you need to enter a matrix in the main
screen. Click somewhere in the main screen, and use the option Insert > Matrix, and
change the number of rows and columns to 2, i.e.,
Click on each of the place holders, one at a time, and enter the values 2, 5, 3, and 1, so
that the resulting matrix is:
Click somewhere inside the matrix, and drag the cursor over the matrix to highlight it:
Next, click on the Calculation menu and select the Determinant option. The result is the
value -13, which is the determinant of the matrix shown above, namely, 21 53 = -13.
Note: I'm not familiar with the use of the options Solve and Differentiate in the
Calculation menu. Therefore, I am not including any examples of those options.
Examples of the function solve and of derivatives in SMath Studio are presented later in
this document.
Items (1) through (4) manipulate new or existing worksheets. Items (5) through (7) are
well-known editing functions. Items (8) and (9) will un-do and re-do the most recent
action. Items (10) through (12) adjust font or background properties. Item (13) allows
you to put a frame over an entry, for example, to show a solution to a problem. Items
(14) and (15) re-align selected cells. Item (16) and (17) open the Function and Unit
menus, as shown earlier under the Insert menu. Item (18) is also available under the
Help menu. Items (19) and (20) were shown earlier in the Calculation menu. Item (19)
shows or hides the palettes on the right-hand side of the page.
1+2/3+4/5 =
Notice the way that SMath interprets the two fractions. Try the following example also:
click somewhere else in your worksheet and type:
23
Suppose that you want to calculate the expression . Use the following
4
keystrokes:
2 + 3 [space] 4 =
Here [space] means to press the space bar. The result is:
The figure above shows the result immediately after entering the equal sign (=). If you
click somewhere else in the worksheet, then the result of this operation is shown as:
More complex expressions can be built using the palette functions shown on the right-
hand side of the screen, for example:
Expressions such as the one shown above can be edited by clicking on the location where
the editing is to be done, and then entering factors or functions. For example, to modify
the expression above, we could click on the right side of the 3 within the square root in
the numerator, i.e.,
and type * 3 [space] 4. Then, select the 1 within the exp function in the denominator
and type over the expression exp(1.2). The modified expression will be the following:
In this example, function arg is selected. The argument (arg) of a complex number is the
angle that a vector representing the complex number in the complex plane forms with the
real (x) axis. As indicated in the figure above, as you select a particular function the
Insert Function form provides an Example as well as a brief Description of the
function.
Typically, you will start an expression in the worksheet and, at the proper location, insert
the function that you need. For example, calculate the expression that uses the
hyperbolic function asinh:
To enter any of those functions simply place a cursor in the desired position and click on
the name of the function, e.g., type:
In these expressions we used symbols that are available in the Functions palette as well
as other symbols available in the Arithmetic palette: Infinity (), Symbolic evaluation
() , and (the ratio of the length of the circumference to the diameter of a circle).
The examples above show the summation and product producing numeric results, while
the derivative and the definite integral produce symbolic results. Notice the difference
between numeric and symbolic results in the following integral:
In the current version of SMath Studio, the main difference between numeric and
symbolic results is whether a result is shown in its decimal form (numeric) or as a
fraction (symbolic) and other symbolic results such as square roots, etc. A proper
symbolic result in the integral above would have been the expression ln(3), however,
SMath Studio does not handle such type of symbolic calculations when the result
involves a function definition.
For an open-source, free program that would handle such type of symbolic calculations
please refer to this web site: http://www.neng.usu.edu/cee/faculty/gurro/Maxima.html.
Note: Use function eval to convert from symbolic to numeric results, e.g., eval(sqrt(3)).
2.5*sin(2.5)+1.2*cos(2+3/(1+4*1.2^2))
As indicated before, examples and brief explanations of the functions are available in the
Insert-Function menu.
The All menu includes all existing functions in SMath Studio. Herein we group some of
those functions according to their applications:
The table below shows the letters of the Greek alphabet and its closest equivalent English
letters.
Clicking on any of the letters in the Symbols palette will copy that letter to any entry
point or text in the worksheet. To illustrate this fact we perform symbolic and numeric
calculations with trigonometric functions that features the value :
Notice that SMath Studio assigns the proper value to the symbol in the calculation.
Once you have made variable assignments, you can use the assigned variables to
calculate expressions. As an example, assign the values x: =-2 and y:=3, then calculate
the following expression: x2+y2. The result may look somewhat like this:
The following example shows variable assignments that use Greek letters:
The symbol <click> indicates that the user must click somewhere in the worksheet.
Using units
Units are important in calculations in the physical sciences. Units in SMath Studio can be
incorporated by using the Unit button in the toolbar (see item (17) in page 9). For
example, to enter the quantity 25 hp (25 horsepower), click somewhere in the worksheets,
type 25 and the click on the Unit button in the toolbar. Scroll down to find the unit
category Power, and select the unit horsepower ('hp), as shown below.
If you next press the equal sign, the quantity entered will be expressed using the basic
units of the SI (Systeme International, or International System of units), e.g.,
Notice that the units are shown in blue italics font. This way the symbol m is different
than the symbol m in your worksheet, so you can use m for meters and m for mass, for
example.
Explore the units menu to see all the available unit categories (Angle, Area, Capacitance,
,
Volume), and the available units. For example, for Angle, you have available the units
Degree('o), Degree ('deg), Radian ('rad), and Revolution ('rev).
Notice that when you type the single quote (apostrophe) symbol, SMath Studio shows the
following symbol to indicate an impending unit insertion (call this symbol the unit
placeholder):
Click on the placeholder so that the placeholder gets selected, as illustrated here:
Then, type the replacement unit(s). For example, if we want to show the previous result
using micronewtons (N), type, in the placeholder, 'm Cntl-g N, i.e.,
By a similar procedure we can replace the units with millinewtons (mN) as follows: click
on the right-hand side of the N units, press the [ Backspace] key twice to remove the
units, so that the unit placeholder shows up, i.e.,
1
When the unit place holder is active, there is no need to type the single-quote (apostrophe) symbol to enter
units.
The unit substitution was not accomplished because the nanonewton unit (nN) is not
defined. You can check this by using the option Insert > Unit and select the Force
category in the resulting menu:
The units are listed in alphabetical order and, obviously, there is no nanonewton (nN)
units listed. (Press the [Cancel] button to remove the menu.) Hence the impossibility of
replacing N with nN.
You can, however, define your own units if they're missing. For example, to define the
symbol for nanonewtons (nN) you can type:
'nN : 10 ^ -9 'N
This definition must be entered at a location in the worksheet above the point where the
calculation takes place. In this case, the conversion to nanonewton (nN) is successfully
performed as shown below:
Example 1: Universal gravitation law. Calculating the force between masses m1 and m2,
separated by a distance d. The formula, shown below, requires us to use the universal
gravitational constant G, found elsewhere:
Notice that in the equation above we used the symbol for c from the collection of Units in
SMath Studio.
A text or calculation field can also be selected by clicking in a region near the field and
dragging the mouse towards the field. In this case, the field is enclosed by a frame with
blue boundaries and a blue background, e.g.,
When selected in this way, a field can be dragged and positioned somewhere else in the
worksheet. For example, the result shown above could be re-organized as follows:
A field selected by dragging can be erased from the worksheet by pressing the [delete] or
the [backspace] key. The selected field can also be copied and pasted by using Ctrl-C
and Ctrl-V, respectively. By dragging on top of two or more fields, multiple fields can be
selected simultaneously for editing.
Consider the following text and calculation cells that are misaligned horizontally:
The four cells were selected by dragging the mouse, while holding the left button, over
the cells. Next, press button (14) in the toolbar to get the following result:
Now, for an exercise on vertical alignment, consider the following vertically misaligned
cells:
After pressing button (15) in the toolbar we get the following result:
1 Copying a figure from other software, clicking and using Paste (or Ctrl-v).
2 Using Insert > Picture > From file , to insert a picture contained in a file.
3 Using Insert > Picture > Create , to insert a picture frame where you can draw
your own picture. Use the mouse as a pencil. SMath Studio does not provide for other
drawing tools
Examples of the three cases are illustrated below. The only images allowed to insert
from a file are bitmap images. Through the cut-and-paste procedure, you can paste
images from any format.
By selecting the field containing a figure, the figure can be repositioned in the worksheet.
Be careful, however, when dragging your mouse across a figure whose field has not been
highlighted. In such case, the mouse dragged across the figure will add lines to the figure
that you may not intended to add. In such case use the Undo button in the menu.
With text and calculation cells, plus image cells imported into or created within SMath
Studio, you can produce technical reports as would be required for class assignments or
for an engineering project.
If you prefer, you can click off the grid using the View menu to produce the following:
Note: To enter multiple lines in a text box, use Shift-Enter at the end of each line.
These functions are available, unclassified, by using the Insert > Function menu and then
selecting the All category of functions:
Since trigonometric and hyperbolic functions apply also to real numbers, we provide a
list of those functions available under the Function Insert form (see above) under the
headings Trigonometric and Hyperbolic:
Trigonometric:
sin sine asin inverse sine
cos cosine acos inverse cosine
tan tangent atan inverse tangent
cot cotangent acot inverse cotanget
sec secant arcsec inverse secant
csc cosecant arccosec inverse cosecant
Hyperbolic:
sinh hyperbolic sine csch hyperbolic cosecant
cosh hyperbolic cosine asinh inverse hyperbolic sine
tanh hyperbolic tangent acosh inverse hyperbolic cosine
coth hyperbolic cotangent atanh inverse hyperbolic tangent
sech hyperbolic secant acoth inverse hyperbolic cotanget
Notice that, in its numerical evaluation, the last example shows both numerator and
denominator multiplied by 1015. These two factors obviously cancel when the fraction is
put together again, but it serves to emphasize that SMath Studio calculates values with 15
decimals.
The function abs, when applied to a complex number, produces the modulo (length) of
the complex number. Function abs is not included in the listing of Complex Numbers
functions in the Insert Function form. However, abs, and many other functions that we
applied to real numbers above, can be applied to complex numbers as illustrated next:
SMath Studio provides functions xy2pol to convert from rectangular (x,y) into polar (r,)
coordinates, and pol2xy to convert from polar (r,) to rectangular (x,y) coordinates.
Thus, with these functions one can go easily go from rectangular to polar representations
of a complex number, and vice versa.
All other operations follow the rules of algebra with the caveat
that i2 = -1, etc. Other powers of the unit imaginary number
are shown in the vector to the left.
Note: the Gamma function currently defined in SMath Studio 0.85 cannot handle negative
arguments, or complex arguments whose real part is negative. For many applications this
definition will be fine, but the full definition of the Gamma function should be able to
handle negative arguments. Based on the paper A note on the computation of the
convergent Lanczos complex Gamma approximation by Paul Godfrey (2001), found in
http://home.att.net/~numericana/answer/info/godfrey.htm#lanczoscoeffs , I redefined the
Gamma function to include negative arguments, as follows:
http://en.wikipedia.org/wiki/Gamma_function
function_name(argument(s)): function_expression
In the place holder on the lower left corner of the graph window you can enter the name
of a function to plot, e.g.,
to insert more than one function in the function placeholder for the graph window. Here
is an example:
Try these options on your own to learn more about the different Plot palette actions. The
example below shows a proportional scaling using item (3) above, while holding down
the Shift key.
x 1, x 2
F ( x) 2 .
x , x2
If the definition of the function requires more than two expressions, it will be necessary
to use nested if programming structures. Consider the following example:
x 1, x 2
F ( x) ( x 1) 2 , 2 x 4
( x 1) 3 , x 4
Notice that the functions f(x) and g(x), whose argument defines the upper limit of the
summation and product, are only defined for integer values.
Examples using both symbolic and numeric results are shown below:
Function solve provides only real roots. Polynomial equations can be solved using
polyroots, as described below.
1
v 2 .
5
Using SMath Studio, the roots of this polynomial are calculated as follows:
Introducing the notation Dn = dn( )/dtn, the general n-th order equation can be written as:
.
This expression can be factored in terms of the D operators to read:
If we take the expression between parentheses and replace the D operators with the
variable and make it equal to zero, we would have produced the so-called
characteristic equation of the ODE, i.e.,
e mt ( A1 A2 t A3 t 2 Ak t k 1 )
The calculation of derivatives is all performed using symbolic calculations, and the
results are very long. Also, the current version of SMath Studio (version 0.85, 09/2009)
doesn't simplify results and keeps the value exp(0) instead of 1. Helping SMath Studio
with these shortcomings, and with a lot of patience, you can get to put together the
system of linear equations needed to solve for the coefficients A1, A2, and A3, in the
solution to this ODE. (The figure below shows only parts of some calculations).
The only function from the Matrices palette not available from the Insert > Function >
Matrix and vector functions is (6) Cross product, which applies to two vectors of three
elements.
It is important to indicate that even though you can form row vectors, many of the matrix
and vector functions defined in SMath Studio apply only to column vectors. Thus,
column vectors are considered true vectors for the purpose of matrix operations in SMath
Studio. For example, to calculate a cross-product you will need two column vectors of
three elements. Dot products, on the other hand, can be performed on column vectors of
any length. The dot product of two column vectors is calculated as the matrix product of
the transpose of one of the vectors times the second vector. Examples of matrix and
vector operations follow. The vector and matrix names used in the examples suggest the
dimensions of the vector or matrix. For example, u2x1 suggest the vector u21, and A3x3
suggests the matrix A33.
'
Example 4 shows an impossibility since the value of start + increment < start, but
end > start
Example 5 shows a vector from 1.2 to 4.4 in increments of 2
Example 6 shows a vector from 0.5 to 5 in increments of 0.5
Example 7 shows a vector from 10 down to 1 in negative increments of -1
The following examples illustrate three different ways to calculate products of vectors of
the same lengths:
Vectors of the same length can be put together in a matrix through function augment:
The operation of these programming structures will be illustrated by the use of flow
charts. A flow chart is just a graphical representation of the process being programmed.
It charts the flow of the programming process, thus its name. The figure below shows
some of the most commonly used symbols in flowcharts:
Sequential structures
A complete programming flowchart would have start and end points, and at least one
process block in between. That would constitute the simplest case of a sequential
structure. The following figure shows a sequential structure for the calculation of a sum:
This flowchart can be simplified slightly by using a single input block to enter both a and
b:
Start
Input a, b
c a + b2
Display c
End
Here is another example of a sequential structure in SMath Studio showing more than one
calculation step. The sequential structure in SMath Studio doesn't have to follow a strict
vertical direction, as illustrated below.
2
The algorithmic statement c a+b represents the assignment c := a + b in SMath Studio
Decision structure
A decision structure provides for an alternative path to the program process flow based
on whether a logical statement is true or false. As an example of a decision structure,
consider the flowchart for the function listed below:
start
input x
if x < -1 then
y |x+1|
else
y |x-1|
display x,y
end
The following figure illustrates examples of logical statements. It also includes examples
of the four logical operations: (1) negation (not ); (2) conjunction (and ); (3)
disjunction (or ); and, (4) exclusive or (xor ). The figure also includes the truth tables
for these four operations. A truth table is a table showing the outcome of all possible
combinations of true and false statements.
The symbols for the comparison operators (=, <, >, , , ) and
logical operations ( , , , ) are available in the Boolean
palette shown here:
Notice the presence of the Boolean equal (bold =) which represents a comparison, rather
than an evaluation. The Boolean equal sign can be used to define equations in SMath
Studio, e.g., in this solve command:
Nested if statements
Decision structures, i.e., if statements, can be nested as illustrated below:
Loop structures
In a loop structure the process flow is repeated a finite number of times before being send
out of the loop. The middle part of the flowchart to the right illustrates a loop structure.
The flowchart shown represents the calculation of a sum, namely,
n
1
S n = .
k=1 k
This summation can also be calculated using SMath Studio's summation command:
While loops can be nested as illustrated in the left-hand side of the figure shown below.
The corresponding double summation is shown on the right-hand side of the figure
below.
The index, therefore, takes values k = k0, k0+k, k0+2k,, kend, such that kend kf, within
one k.
The for command in SMath Studio uses a range of values to indicate the values that the
index k takes to complete the loop.
The double summation programmed earlier using while loops, can be programmed also
using for loops:
Errors in programming
Typically there are three main types of errors in developing a program:
(1) Syntax errors: when the command doesn't follow the programming language
syntax. These are easy to detect as the program itself will let you know of the syntax
violations.
(3) Logical errors: these are errors in the algorithm itself. These are more difficult to
detect, thus the need to test your programs with known values. Check every step of your
algorithm to make sure that it is doing what you intend to do.
C:\Users\Gilberto E. Urroz\Documents\NUMERICAL_APPLICATIONS\SMath
In your computer it could be any location where you don't need Administrator rights to
modify content. I then proceeded to create the folder user, where the data files would
reside. Thus, the full address of this new folder, in my case, is:
C:\Users\Gilberto E. Urroz\Documents\NUMERICAL_APPLICATIONS\SMath\SMath
Studio\user
To facilitate access to the program, at this point, I created a new shortcut to SMath Studio
in my desktop. Now we proceed to illustrate the use of the wfile command.
You can open the files using, for example, Notepad in a Windows system to check the
contents of the file:
3
I recommend you use only lowercase letters in the filenames used to store data. I had difficulties getting
SMath Studio to read data out of files with uppercase letters in their names.
If you attempt to read from a non-existing file you'll get an error message:
Then, close the newly saved file myMatrix.csv, and open it using Notepad (right-click on
the icon, and select Open with ):
Copy the file contents into another text file, let's call it mymatrixfile4, and edit it to read:
4
A few notes: (1) Use only lowercase letters in the name of the file. I couldn't get SMath Studio to
recognize filenames that include uppercase letters. (2) Make sure the filename to be read has no extension,
therefore, the file should be named, in this case, mymatrixfile, and not mymatrixfile.txt or mymatrixfile.dat,
etc. (3) Do not try to simply use the same .csv file without an extension as your input file to SMath Studio.
It won't work. You need to create a new file.
Of course, to get this simple 3x3 matrix into SMath Studio it will be easier to type it in
into the worksheet. The procedure illustrated above would be more practical for very
large spreadsheet data sets.
Deleting files
Use function dfile(filename) to delete files in the folder /*SMath Studio Installation
Folder*/user. Examples:
The directory /*SMath Studio Installation Folder*/user is shown below before and after
the execution of the dfile commands shown above:
-4
In this example we plot the
-8 function: f(x) = sin(x)
-16 -8 0 8 16
12
sin x
64 y y
2
48
0 x
32 -2
-8 -4 0 4 8
16 sin x
y
0 x 8
0 x
-4
-8
0 -16 -8 0 8 1
sin x sin x
1 / 11
10 May 2010 19:08:24 - Graphs.sm
Moving the axes about the graph window:
y y
16
4
12
0 x
8
-4
4
-8
0 x
-12
-4
-24 -16 -8 0 8 -16-8 0 8 16 24
sin x sin x
0 x
-4
y
-8
1
-2 -1 0 1 2
12
sin x 0.5
To zoom the x-axis only: ---->
1 - Click on "Scale" in the "Plot" palette 0 x
2 - Click on the graph window
3 - Hold down the "Control" key
4 - Roll the mouse wheel up or down -0.5
-1
y -16 -8 0 8 16
0.75 sin x
0.5
0.25
<--- You can zoom both axes by zooming one
axis at a time. In this case, I zoomed
0 x
the x axis first, and then the y axis.
-0.25
-0.5
-0.75
-6 -4 -2 0 2 4 6
sin x
2 / 11
10 May 2010 19:08:24 - Graphs.sm
12 y
1 - create a 2D graph
2 - click on the function placeholder
8 (lower left corner) to select it
3 - click on the "Equation System"
4 option in the "Functions" palette
to produce a minimum of two
function entries
0 x
4 - Type the two functions to be plotted
-4
Note: the first function listed is plotted
using a blue line, the second one
-8 uses a red line
-16 -8 0 8 16
12
sin x
cos x
Notes:
-2
-3 -2 -1 0 1 2 3
1 - As you add functions to plot, SMath Studio
uses the following colors for the plots:
sin x
cos x 1 - blue 2 - red
2 sin 2 x 3 - black 4 - magenta, etc.
x
cos 2 - In this example the graph has been zoomed
2
using the approach described above
Plotting a function using vectors: Vectors of x and y data are created using ranges,
example:
x , .. Create x vector as follows
20
Type: x : range - p cntl-G , p cntl-G ,
- p cntl-G + p cntl-G / 20
n length x
Calculate the length of vector = n
n 41
Fill out y vector using a for loop. Click "for"
for k 1 .. n in the "Programming" palette, then use:
2 range 1 , n
y sin x sin 2 x
k k k
Use sub-indices, e.g., y [ k ... etc.
3 / 11
10 May 2010 19:08:24 - Graphs.sm
M augment x , y Form augmented matrix M with vectors x and y,
place M in graph as a function name:
1.5
< --- The graph was zoomed in and the axes
1 moved by using the following procedures:
y
Using the sparse data in matrix M we reproduce
2
the graph above, but then we selected the
"Graph by points" option in the "Plot" palette
1.5 to produce the graph shown to the left.
0 x
-0.5
-4 -2 0 2 4
-1
M
4 / 11
10 May 2010 19:08:24 - Graphs.sm
y y
2 2
1 1
0 x 0 x
-1 -1
-2 -2
-1 -0.5 0 0.5 1 -1 -0.5 0 0.5 1
M M
for k 1 .. n
Generate coordinates:
xx r cos
k k k
yy r sin x = r cos()
k k k y = r sin()
y y
6 6
5 5
4 4
3 3
2 2
1 1
0 x 0 x
-4 -2 0 2 4 -4 -2 0 2 4
-1 -1
P P
5 / 11
10 May 2010 19:08:24 - Graphs.sm
Using graphs in solving equations:
In this example we seek the solution(s) for the equation:
2 3
x 1 x 2 x 5
A solution can be found by determining the intersection of the functions:
2 3
f x x 1 g x x 2 x 5
Using graphics and zooming the intersection we estimate the solution to be close to
x = 1.80
16 4.375
12 4.25
8 4.125
4 4
0 x 3.875
-4 -2 0 2 4 1.625 1.6875 1.75 1.8125 1.875
-4
f x f x
g x g x
2 3
The exact solution can be found using: solve x 1 x 2 x 5, x 1.776
z
z8
6
6
4
4
2 2
20 2 4 6 x 02 4 6 8x
64 2
y 4
6
y8
x y x y
Use the option "3D" in the "Functions" palette, Use the "Rotate" option in the "Plot" palette
and enter the function f(x,y) in the place- to change the surface view.
holder. The result is a 3D surface, in this
case, a plane. The original plot is shown
above.
6 / 11
10 May 2010 19:08:24 - Graphs.sm
z8 z8
6 6
4 4
2 2
02 4 6 8x 02 4 6 8x
2 2
4 4
6 6
y8 y8
x y x y
This figure uses the option "Graph by This figure uses the option "Graph by
Lines" in the "Plot" palette (default). Points" in the "Plot" palette.
z
27
z
18
6
4 9
2
9 0 9 x
18 27
18
20
2 4 6 x
4 y27
6
y
x y x 2 y
Note: Use the "Refresh" option in the "Plot" menu to recover the original version of any plot.
7 / 11
10 May 2010 19:08:24 - Graphs.sm
The following examples use more complex surfaces:
z z
6 6
4 4
2 2
02 2 4 6 x 20 2 4 6 x
46 64
y
y
sin x 2 2
5 x y
z
8
z 6
6
4
4
2 2
x6420 2
02 4 6 8x
2 4
4 6
6 y8
y
x y x y
x y
The type of 3D graphs of surfaces produced by SMath Studio are referred to as wireframe plots.
The following examples show more than one surface plot together in 3D:
z
8
6
z8
4 6
2 4
6420 2
8x 4 6 y8 2
0 2 4 6 8x
2
4
6
y8
x y
x y
x y
x y
x 2 y
8 / 11
10 May 2010 19:08:24 - Graphs.sm
z
9 z8
6 6
4
3
2
6 3
0 3 6 9 x 0 2 4 6 8x
2
y9 4
6
y8
2 2 sin x
5 x y
sin y
2 2
x y
Drawing a space curve in 3D: A space curve is defined by a matrix of three columns
corresponding to coordinates x, y, and z of the curve.
M augment x , y , z
Build matrix M with coordinates (x,y,z)
z
6
4
2
20 2 4 6 x
64
y
9 / 11
10 May 2010 19:08:24 - Graphs.sm
Drawing a surface and a space curve: Use the "Equation System"
option in the "Plot" palette,
and enter the equation of the
surface (e.g., x+y) and the
matrix that represents the
space curve (e.g., M)
z8
6
4
2
0 2 4 6 8x
2
4
6
y8
x y
M
for k 1 .. n for k 1 .. n
x1 1 5 t x2 1 3 t
k k k k
y1 1 4 t y2 1 t
k k k k
z1 1 8 t z2 t
k k k k
P augment x1 , y1 , z1 Q augment x2 , y2 , z2
z z
6 6
4 4
2 2
20 2 4 6 x 20 2 4 6 x
64 64
y y
P Q
Individual plots of curves given by matrices P and Q.
10 / 11
10 May 2010 19:08:24 - Graphs.sm
z
6
4 <--- Join plot of
2 lines given
by matrices
20 2 4 6 x
64 P and Q
y
P
Q
11 / 11
10 May 2010 19:22:19 - GeometricFigures01.sm
Straight-line segment:
----------------------
Given the two points, A and B, representing the
extremes of the straight-line segment, plot the
segment.
5 3
* Extreme points: A B
2 10
* Generate matrix of points:
B A
2 2
* Slope: m m 1.5
B A
1 1
* Intercept: b A m A b 5.5
2 1
* Number of points: n 5
B A
1 1
* Increment: x x 1.6
n
* x-series: xS A ,A x .. B
1 1 1
* y-series:
for k 1 .. n 1
yS m xS b
k k
12 y
0 x
-4
-8
-16 -8 0 8 16
12
MS
1/5
10 May 2010 19:22:19 - GeometricFigures01.sm
1 1
* Mid-point: M A B M
2 4
1
* Normal slope: mP mP 0.6667
m
* Normal x series: xP A ,A x .. B
2 2 2
* Normal y-series:
for k 1 .. n 1
yP mP xS bP
k k
12 y
0 x
-4
-8
-16 -8 0 8 16
12
MS
MP
Circle:
-------
3
* Center: C * Radius: r 12.5
5
for k 0 .. n
xC C r cos k
k 1 1
yC C r sin k
k 1 2
MC augment xC , yC
2/5
10 May 2010 19:22:19 - GeometricFigures01.sm
20
y
16
12
0 x
-4
-8 -24 -16 -8 0 8 16
MC
* Number of sides: n 5
3
* Center of polygon: C
2
* Radius of circums- r 12
cribed circle:
* Initial angle: 0
6
2
* Incremental angle: 1.2566
n
for k 0 .. n
* Generate matrix
xPol C r cos 0 k
of points: k 1 1
yPol C r sin 0 k
k 1 2
0 x
-4
-8
-12
-16 -8 0 8 16
-16
MPol
3/5
10 May 2010 19:22:19 - GeometricFigures01.sm
Calculating the area and perimeter of the regular polygon:
---------------------------------------------------------
13.3923 4
0.5051 9.7378
8.9343 0.7457
MPol
1.8808 12.9625
11.9177 10.0296
13.3923 4
M MPol
n rows M 1 n 5
Area 0
Perim 0
for k 1 .. n
y
1
Area eval Area M M 8
2 k1 k 12
1 4
Area eval Area M M
2 k 11 k2
0 x
2
d2 eval M M -4
k 11 k1
2 -8
d2 eval d2 M M
k 12 k2 -12
Perim eval Perim d2 -8 0 8 16
-16
Area Area MPol
-----------------------------------------------------------------------
Irregular polygon:
------------------
Enter the (x,y) coordinates of the n polygon vertices in
a nx2 matrix, e.g.,
5 2
1 3
0 7
MI 1 1
7 1
1 3
2 5
T
T T
MN augment MI , row MI , 1
4/5
10 May 2010 19:22:19 - GeometricFigures01.sm
5 2
1 3
0 7
1 1
8 y MN
7 1
6 1 3
2 5
4 5 2
0 x
-2
-4
-8 -4 0 4 8
MN
5 2
As with the case of the 1 3
regular polygon, the 0 7
matrix representing the 1 1
vertices of the irregular MN
7 1
polygon include an extra
1 3
repetition of the first
vertex: 2 5
5 2
With such a matrix available, the area and perimeter of the
polygon is calculated as follows.
Area Area MN
5/5
10 May 2010 21:17:09 - NewtonRaphsonApplications.sm
f x 0
requires the knowledge of the derivative f'(x). This can be easily
accomplished in SMath Studio using the "Derivative" option in the
"Functions" palette:
d
fp x f x
dx
Given an initial guess of the solution, x x , the solution can
0
be approximated by the iterative calculation:
f x
k
x x
k 1 k f' x
k
for k 0 ,1, ...
2
Example: Solve the equation: x 2 x 5 0
--------
2
Define the function: f x x 2 x 5
16
0 x
-8
-4 -2 0 2 4 6
f x
1/5
10 May 2010 21:17:09 - NewtonRaphsonApplications.sm
The graph shows solutions near x = -2 and x = 3. We can
implement the solution using the Newton-Raphson method
as follows:
d
fp x f x fp x 2 1 x
dx
6
Parameters of the solution are: 1.0 10 nmax 100
First solution:
Starting with a guess of xG 2.5
we find a solution by using the following iterative procedure:
k 0
while k nmax f xG
f xG
xGp1 xG
fp xG
k k 1
xG xGp1
Second solution:
Starting with a guess of xG 4.2
we find a solution by using the following iterative procedure:
k 0
while k nmax f xG
f xG
xGp1 xG
fp xG
k k 1
xG xGp1
1.4495
solve f x 0, x
3.4495
2 1.4495
solve x 2 x 5 0, x
3.4495
2/5
10 May 2010 21:17:09 - NewtonRaphsonApplications.sm
3 - The Newton-Raphson method for a system of equations:
-------------------------------------------------------
f1 x ,x .. x 0
1 2 n
f2 x ,x
.. x 0
12 n
...
fn x , x .. x 0
1 2 n
or simply, f x 0 , with
f1 x ,x .. x
1 2 n f1 x
f2 x ,x .. x f2 x
f x 1 2 n
. .
fn x ,x .. x fn x
1 2 n
x
1
x
x 2
.
x
n
1
x x J x f x
k 1 k k k
J x
for k 0 ,1,... In this formula, k , is the Jacobian
matrix of the function defined as [to be 100% correct the
derivatives in this matrix should be partial derivatives]:
dy dy dy
1 1 1
.
dx dx dx
1 2 n
dy dy dy
2 2 2
.
J x dx dx dx
k 1 2 n
. . . .
dy dy dy
n n n
.
dx dx dx
1 2 n
Example:
--------
How to calculate the Jacobian matrix of a system of three
equations. Given the system of three equations:
3/5
10 May 2010 21:17:09 - NewtonRaphsonApplications.sm
x x x 6 This is obvious, but could
1 2 3
useful for larger functions:
x x x 6
f x 1 2 3 n length f x
2 2 3
x x x 14 n 3
1 2 3
for i 1 .. n
for j 1 .. n
d
jac f x
ij dx i
j
5
An initial guess is: xG 1
4
The iterative process for the solution is expressed as:
k 0
3 15
3.9328 10
xG 2
f xG 14
1 1.0068 10
14
3.253 10
4/5
10 May 2010 21:17:09 - NewtonRaphsonApplications.sm
----------------------------------------------------------------
Note: The function representing the system of equations solved
above, namely,
x x x 6
1 2 3
x x x 6
f x 1 2 3
2 2 3
x x x 14
1 2 3
x y z 6 0 x y z 6
x y z 6 0 or x y z 6
2 2 2 2 2 2
x y z 14 0 x y z 14
5/5
10 May 2010 21:21:32 - RK4SingleEquation.sm
Problem description
-------------------
This worksheet provides two different versions of the 4th-order
Runge-Kutta method for solving a first-order ordinary differential
equation (ODE) of the form:
d
y f x, y
dx
subject to the initial condition: y xs ys
The solution will be provided in the range: xs x xe
which will be divided into n subintervals to produce solution
vectors "xsol" and "ysol", as detailed below.
xe xs
x
n
ysol ys
1
1/6
10 May 2010 21:21:32 - RK4SingleEquation.sm
1
xM x0 x K1 x f x0 , y0
2
1
yM y0 K1 K2 x f xM , yM
2
1
yM y0 K2 K3 x f xM , yM
2
y1 y0 K3 K4 x f x1 , y1
1
ysol y0 K1 2 K2 2 K3 K4
k 1 6
2/6
10 May 2010 21:21:32 - RK4SingleEquation.sm
Example of 4th-order Runge-Kutta solution -version 1
----------------------------------------------------
dy
Solve the ODE: sin x , subject to the initial
dx
condition: y 0 1 in the interval: 0 x 20
using 100 intervals in the solution.
Solution:
---------
xe xs
x eval x 0.2
n
Create the x solution vector: xsol eval xs , xs x .. xe
Create the y solution vector as a zero matrix of n rows and 1
column, and initialize the y solution vector with the initial
condition ys:
for k 1 .. n
x0 eval xsol
k
y0 eval ysol
k
1
xM eval x0 x
2
K1 eval x f x0 , y0
1
yM eval y0 K1
2
K2 eval x f xM , yM
1
yM eval y0 K2
2
K3 eval x f xM , yM
y1 eval y0 K3
x1 eval xsol
k 1
K4 eval x f x1 , y1
1
ysol eval y0 K1 2 K2 2 K3 K4
k 1 6
3/6
10 May 2010 21:21:32 - RK4SingleEquation.sm
y y
2 2
1.5 1.5
1 1
0.5 0.5
00 4 8 12 16 x
20 0 0 4 8 12 16 20
x
M M
The plot to the left uses the "Graph by lines" option in the "Plot"
palette, while the plot to the right uses the "Graph by points"
option in the "Plot" palette.
4/6
10 May 2010 21:21:32 - RK4SingleEquation.sm
1 2
x13 x0 x x23 x0 x K1 x f x0 , y0
3 3
1
y13 y0 K1 K2 x f x13 , y13
3
1
y23 y13 K2 K3 x f x23 , y23
3
y1 y0 K1 K2 K3 K4 x f x1 , y1
1
ysol y0 K1 3 K2 3 K3 K4
k 1 8
dy
Solve the ODE: sin x , subject to the initial
dx
condition: y 0 1 in the interval: 0 x 20
using 100 intervals in the solution.
Solution:
---------
xe xs
x eval x 0.2
n
Create the x solution vector: xsol eval xs , xs x .. xe
Create the y solution vector as a zero matrix of n rows and 1
column, and initialize the y solution vector with the initial
condition ys:
5/6
10 May 2010 21:21:32 - RK4SingleEquation.sm
for k 1 .. n
x0 eval xsol
k
y0 eval ysol
k
1
x13 eval x0 x
3
2
x23 eval x0 x
3
K1 eval x f x0 , y0
1
y13 eval y0 K1
3
K2 eval x f x13 , y13
1
y23 eval y13 K2
3
K3 eval x f x23 , y23
y1 eval y0 K1 K2 K3
x1 eval xsol
k 1
K4 eval x f x1 , y1
1
ysol eval y0 K1 3 K2 3 K3 K4
k 1 8
2.5 y 2.5 y
2 2
1.5 1.5
1 1
0.5 0.5
0 x 0 x
0 4 8 12 16 2 0 4 8 12 16 2
N N
The plot to the left uses the "Graph by lines" option in the "Plot"
palette, while the plot to the right uses the "Graph by points"
option in the "Plot" palette.
6/6
10 May 2010 21:23:54 - RK4SystemEquations.sm
Problem description
-------------------
dy y
1 1
f1 x , f1 x , y
dx y
2
dy y
2 1
f2 x , f2 x , y
dx y
2
ys y and ys y
1 1 xs 1 2 2 xs 2
y
dy 1 f1 x , y
f x, y , with y and f x, y
dx y f2 x , y
2
ys1
The initial conditions are given by the vector: ys
ys2
Example
-------
dy
1
sin x cos y sin y
dx 1 2
dy
2
cos x sin y
dx 2
y1 0 1 and y2 0 1
1/4
10 May 2010 21:23:54 - RK4SystemEquations.sm
Solution (version 1):
---------------------
1
The initial conditions are: xs 0 ys
1
xe xs
x eval x 0.2
n
Create the x solution vector: xsol eval xs , xs x .. xe
ysol ys 1
ysol
1
for k 1 .. n
x0 eval xsol
k
y0 eval col ysol , k
1
xM eval x0 x
2
K1 eval x f x0 , y0
1
yM eval y0 K1
2
K2 eval x f xM , yM
1
yM eval y0 K2
2
K3 eval x f xM , yM
y1 eval y0 K3
x1 eval xsol
k 1
K4 eval x f x1 , y1
1
y1 eval y0 K1 2 K2 2 K3 K4
6
ysol augment ysol , y1
T
ysol ysol
2/4
10 May 2010 21:23:54 - RK4SystemEquations.sm
M1 augment xsol , col ysol , 1
M2 augment xsol , col ysol , 2
y y
4 4
3 3
2 2
1 1
0 x 0 x
-1 0 4 8 12 16 20 -1 0 4 8 12 16 20
M1 M1
M2 M2
The plot to the left uses the "Graph by lines" option in the "Plot"
palette, while the plot to the right uses the "Graph by points"
option in the "Plot" palette.
1
The initial conditions are: xs 0 ys
1
xe xs
x eval x 0.2
n
Create the x solution vector: xsol eval xs , xs x .. xe
ysol ys 1
ysol
1
The following "for" loop calculates the Runge-Kutta algorithm
(version 1) to produce the solution:
3/4
10 May 2010 21:23:54 - RK4SystemEquations.sm
for k 1 .. n
x0 eval xsol
k
y0 eval col ysol , k
1
x13 eval x0 x
3
2
x23 eval x0 x
3
K1 eval x f x0 , y0
1
y13 eval y0 K1
3
K2 eval x f x13 , y13
1
y23 eval y13 K2
3
K3 eval x f x23 , y23
y1 eval y0 K1 K2 K3
x1 eval xsol
k 1
K4 eval x f x1 , y1
1
y1 eval y0
K1 3 K2 3 K3 K4
8
ysol augment ysol , y1
y y
4 4
3 3
2 2
1 1
0 x 0 x
0 4 8 12 16 20 0 4 8 12 16 20
N1 N1
N2 N2
The plot to the left uses the "Graph by lines" option in the "Plot"
palette, while the plot to the right uses the "Graph by points"
option in the "Plot" palette.
4/4
10 May 2010 21:26:49 - RK4-2ndOrderODE.sm
Appendix 6 - The 4th -order Runge-Kutta method for a 2nd order ODE
-----------------------------------------------------------------
By Gilberto E. Urroz, Ph.D., P.E.
January 2010
Problem description
-------------------
u 1 and u 1 at x 0 .
1 2
d
u u [Eq. 1]
dx 1 2
or: d
u u u 3 u sin x [Eq. 2]
dx 2 1 2
1
u u
d 1 2
dx u u u 3 u sin x
2 1 2
1
or, u
d 1
u f x , u , where u and
dx u
2
u
2
f x, u
u u 3 u sin x
1 2
1
1
The initial conditions are us at xs 0
1
We'll solve the ODEs in the interval: 0 x 20 using 100 intervals.
1/4
10 May 2010 21:26:49 - RK4-2ndOrderODE.sm
Solution (version 1):
---------------------
u
2
f x, u
u u 3 u sin x
1 2
1
1
The initial conditions are: xs 0 us
1
xe xs
x eval x 0.2
n
Create the x solution vector: xsol eval xs , xs x .. xe
usol us 1
usol
1
for k 1 .. n
x0 eval xsol
k
u0 eval col usol , k
1
xM eval x0 x
2
K1 eval x f x0 , u0
1
uM eval u0 K1
2
K2 eval x f xM , uM
1
uM eval u0 K2
2
K3 eval x f xM , uM
u1 eval u0 K3
x1 eval xsol
k 1
K4 eval x f x1 , u1
1
u1 eval u0 K1 2 K2 2 K3 K4
6
usol augment usol , u1
T
usol usol
2/4
10 May 2010 21:26:49 - RK4-2ndOrderODE.sm
M1 augment xsol , col usol , 1
M2 augment xsol , col usol , 2
y
3
0 x
-1
0 2 4 6 8 10 12 14 16 18 20
M1
M2
The blue line represents u[1]=y while the red line represents
u[2] = dy/dx.
u
2
f x, u
u u 3 u sin x
1 2
1
1
The initial conditions are: xs 0 us
1
xe xs
x eval x 0.2
n
Create the x solution vector: xsol eval xs , xs x .. xe
usol us 1
usol
1
3/4
10 May 2010 21:26:49 - RK4-2ndOrderODE.sm
for k 1 .. n
x0 eval xsol
k
u0 eval col usol , k
1
x13 eval x0 x
3
2
x23 eval x0 x
3
K1 eval x f x0 , u0
1
u13 eval u0 K1
3
K2 eval x f x13 , u13
1
u23 eval u13 K2
3
K3 eval x f x23 , u23
u1 eval u0 K1 K2 K3
x1 eval xsol
k 1
K4 eval x f x1 , u1
1
u1 eval u0
K1 3 K2 3 K3 K4
8
usol augment usol , u1
3
y
0 x
-1
-2 0 2 4 6 8 10 12 14 16 18 20
N1
N2
The blue line represents u[1]=y while the red line represents
u[2] = dy/dx.
4/4
10 May 2010 22:02:44 - EigenValuesAndEigenvectors4x4(case1).sm
1 2 4 2
The following program calculates the
coefficients of the characteristic 2 2 2 6
A
equation: A I 0 4 2 3 8
2 6 8 4
The resulting equation is the polynomial:
2 n 1 n
p p p + ... + p p 0
1 2 3 n n 1
n rows A
p matrix n , 1
I identity n
p 1.0
n 11
B A
p tr B
n1
for j n 1 , n 2 .. 1
B A B p I
j 11
tr B
p
j1 n j 1
n
p 1 p
polyroots p
V matrix n 1 , 1
for j 1 .. n
A A I
j
Am submatrix A , 1 , n 1 , 1 , n 1
b submatrix A , 1 , n 1 , n , n
1
x Am b
V augment x , V
V submatrix V , 1 , n 1 , 1 , n
Vb matrix 1 , n
for j 1 .. n
Vb 1
1j
V stack V , Vb
for j 1 .. n
x col V , j
x
x
norme x
V augment V , x
V submatrix V , 1 , n , n 1 , 2 n
1/2
10 May 2010 22:02:44 - EigenValuesAndEigenvectors4x4(case1).sm
1300
1.952 0.2746 0.0037 0.4651 0.8416
530
6.3272 0.2188 0.5103 0.7569 0.3446
p 93 V
7.8257 0.6082 0.5693 0.374 0.4076
10
13.4505 0.7119 0.6446 0.2662 0.0823
1
T 16
x1 x2 2.8843 10 norme x1 1
T 14
x1 x3 2.2924 10 norme x2 1
T 14 norme x3 1
x1 x4 3.5213 10
norme x4 1
T 14
x2 x3 1.0401 10
T 14
x2 x4 8.4221 10
T 14
x3 x4 2.6966 10
2/2