iconEuler Home

A Crash Course for EMT

This is a really fast introduction into Euler Math Toolbox (EMT) with lots of links and examples.

If you need a more thorough overview or a starter tutorial, see the following notebooks.

00 - A first Welcome
00 - The Syntax of Euler

These are links to HTML exports of tutorials which are installed with EMT. You can double click such links in notebooks. The links will open in the browser. But you can also open all tutorials as notebooks via the help menu.

Syntax

EMT is command line oriented. It allows one or more commands in one command line. The commands have to be ended by comma or semicolon, besides the last command, where a comma is assumed.

In the following example, we define a variable r. The output of this definition would be the value of the variable. But because of the semicolon this output is not printed.

>r=1.25; pi*r^2, 2*pi*r
4.90873852123
7.85398163397

Make sure to use a decimal dot, not a decimal comma for numbers! Use * for multiplication and ^ for exponentials. As usual, * binds stronger than +, and ^ binds stronger than *, so that pi*r^2 works correctly. If necessary, you can add round brackets, of course.

The command r=1.25 is an assignment to a variable in EMT. You can also write r:=1.25 if you prefer. You can leave spaces as you like.

You can also end a command line with a comment.

>r := 1.25 // This is an optional form for an assignment.
1.25

Functions are called with round brackets.

>sin(45°), log(sqrt(E))
0.707106781187
0.5

As you see, trigonometric functions work with radians, and degrees can be converted with °. If your keyboard does not have the degree character press F7, or use the function deg() to convert.

EMT knows a great lot of mathematical functions and operators. For details see

Full Reference
Core Functions
Special Functions

EMT can convert units to the international standard system (IS). Append the unit to the number for a simple conversion.

>3.56miles
5729.26464

A list of available units can be found on the page below.

Reference for Units

For the conversion to and between units, EMT has a special operator -> which can convert to a number or to a string.

>4km->miles, 4inch->" mm"
2.48548476895
101.6 mm

The default output format prints with around 10 places. Other output formats can be set globally or only for one output. For details on output formats, see

Reference for Output Formats

For short, long, or longest output of a single value, you can use the following operators.

>short 1200*1.03^10, long E, longest pi
1612.7
2.71828182846
      3.141592653589793 

You can switch the output format for all subsequent commands. Reset the format with defformat() or reset().

>longestformat; pi, defformat;
3.141592653589793

The numerical kernel of EMT works with floats (in contrast to the symbolic part of EMT). But numerical results can be converted to fractions.

>fraction 1/7+1/4
11/28

A multi-line command stretches over several lines connected with "..." at the end. To generate a break in a command, use Ctrl-Return. To join a line to the previous line, use "Ctrl-Backspace".

The following multi-line command can be executed whenever the cursor is in any of its lines. It also shows that the ... must be at the very end of the line even if it contains a comment.

>a=4; b=15; c=2; // solve a*x^2+b*x+c=0 manually ...
 diskr=sqrt(b^2/(a^2*4)-c/a); ...
 -b/(2*a) + diskr, ...
 -b/(2*a) - diskr
-0.138444501319
-3.61155549868

The percent character % refers to the previous result. It should be used only within a single command line.

>3+sqrt(5), %^2
5.2360679775
27.416407865

To see all user variables use "listvar".

>listvar
r                   1.25
a                   4
b                   15
c                   2
diskr               1.73655549868123

This searches for the user defined variables only. It is possible to search for other variables. For this, a string must be added which must be contained in the name of the variable.

The following finds all units ending with "m" by searching for all variables that contain "m$".

>listvar m$
km$                 1000
cm$                 0.01
mm$                 0.001
hquantum$           6.62606957e-034
nm$                 1853.24496
gram$               0.001
m$                  1
atm$                101325

To remove a variable without having to restart EMT use "remvalue".

>remvalue a,b,c,diskr

To get help on any function in EMT, open the help window by F1 and search for the function. You can also double click on a function in the notebook window to open the help window.

Try double clicking on "intrandom" in the following command.

>intrandom(10,6)
[4,  2,  6,  2,  4,  2,  3,  2,  2,  6]

The result of this functions is a vector of random dice throws.

A vector can be defined by square brackets. See below for matrices.

>[4,5,6,3,2,1]
[4,  5,  6,  3,  2,  1]

Matrices are available too. In fact, EMT has very advanced features to do Linear Algebra, Statistics or Optimization.

For an example, we can define a vector of uniformly spaced values like the following, and compute the mean of the squared values.

>0:0.1:1, mean(%^2)
[0,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1]
0.35

EMT can use complex numbers too. Many functions work for complex numbers as expected.

>exp(2+3*I)
-7.31511+1.04274i

Symbolic Mathematics

EMT does symbolic math with the help of Maxima. For details, start with the following tutorial, or browse the reference for Maxima. Experts in Maxima should note that there are differences in the syntax between the original syntax of Maxima and the default syntax of symbolic expressions in EMT.

Tutorial on Symbolic Math with Euler
Maxima Reference

Symbolic math is integrated seamlessly into Euler with &. Any expression starting with & is a symbolic expression. It is evaluated and printed by Maxima.

>&expand((1+x)^4), &factor(diff(%,x))
                       4      3      2
                      x  + 4 x  + 6 x  + 4 x + 1


                                       3
                              4 (x + 1)

Again, % refers to the previous result.

A direct input of Maxima commands is available too. Start the command line with "::". The syntax of Maxima is adapted to the syntax of EMT (called the "compatibility mode").

>:: factor(20!)
                        18  8  4  2
                       2   3  5  7  11 13 17 19

If you are an expert in Maxima, you may wish to use the original syntax of Maxima. You can do this with ":::".

>::: av:g$ av^2;
                                   2
                                  g

Variables can be stored as symbolic variables using the assignment "&=".

>fx &= x^3*exp(x)
                                 3  x
                                x  E

Such variables can be used in other symbolic expressions. Note, that in the following command the right hand side of &= is evaluated before the assignment to Fx.

>Fx &= integrate(fx,x)
                         3      2             x
                       (x  - 3 x  + 6 x - 6) E

For the evaluation of an expression with specific values of the variables, you can use the "with" operator.

The following command line also demonstrates that Maxima can evaluate an expression numerically with float().

>&(Fx with x=10)-(Fx with x=5), &float(%)
                                10       5
                           754 E   - 74 E


                         1.659697263551068e+7

Expressions

Hte most elementary way to define a simple function is to store its formula in a symbolic or numerical expression. If the main variable is x, the expression can be evaluated just like a function.

As you see in the following example, global variables are visible during the evaluation.

>fx &= x^3-a*x;  ...
 a=1.2; fx(0.5)
-0.475

All other variables in the expression can be specified in the evaluation using an assigned parameter.

>fx(0.5,a=1.1)
-0.425

An expression needs not be symbolic. This is necessary, if the expression contains functions, which are only known in the numerical kernel, not in Maxima (albeit Maxima has the incomplete gamma function too, called gamma_incomplete(), but with a different definition).

>fx="gammai(x^2,2)*cos(x)"; fx(0.5)
0.0232550788778

The easiest way to plot a function is to use an expression in x. This can be a symbolic expression or a simple string. We demonstrate more advanced plots later.

In the following example, we also demonstrate that a colon ":" at the end of the command line inserts the current plot into the notebook. By default, the plot is visible in a separate window only. (EMT can also use a one-window interface - press Ctrl-G).

Press TAB to bring the plot window or the plot area to the front.

>plot2d(fx,0,2pi):

00 - A Crash Course in Euler

To demonstrate plots of vector of data, we use the matrix language of EMT to evaluate a function at the data.

>n=0:30; plot2d(n,bin(30,n)/2^30,>points):

00 - A Crash Course in Euler

Functions

In this crash course, we can only show one-line functions. For multi-line functions refer to the following tutorial. EMT contains a complete programming language.

Tutorial for EMT Programs

A one-line function can be numerical or symbolic.

>function f(x) &= x^3-x
                                 3
                                x  - x

With &= the function is symbolic, and can be used in other symbolic expressions.

>&integrate(f(x),x)
                                4    2
                               x    x
                               -- - --
                               4    2

With := the function is numerical. A good example is a numerical integral like

00 - A Crash Course in Euler

which can not be evaluated symbolically. (See below for numerical integration.)

>function f(x) := integrate("x^x",1,x);

A function can be plotted by its name. We add a label to the function.

>plot2d("f",0,2,-1,3); label("f",1.6,f(1.6)):

00 - A Crash Course in Euler

If we redefine the function with the keyword "map" it can be used for vectors x. Internally, the function is called for all values of x once, and the results are stored in a vector.

>function map f(x) := integrate("x^x",1,x);
>f(0:0.5:2)
[-0.783431,  -0.410816,  0,  0.676863,  2.05045]

Functions can have default values for parameters.

>function mylog (x,base=10) := ln(x)/ln(base);

Now the function can be called with or without a parameter "base".

>mylog(100), mylog(2^6.7,2)
2
6.7

Moreover, it is possible to use assigned parameters.

>mylog(E^2,base=E)
2

Assigned parameters are used by many functions and algorithms in EMT. E.g., the plot2d function can get parameters in exactly this way.

>plot2d("exp(-x^2)",style="--",color=red,thickness=2):

00 - A Crash Course in Euler

For boolean parameters there is a shortcut. There, >param is equivalent to param=true, and <param is equivalent to param=false. By the way, "false" is defined as 0, and "true" is defined as 1 in EMT. There is no boolean data type.

>plot2d("sin(1/x)",0.1,1,<frame):

00 - A Crash Course in Euler

Often, we want to use functions for vectors at one place, and for individual elements at other places. This is possible with vector parameters.

>function f([a,b]) &= a^2+b^2-a*b+b
                           2              2
                          b  - a b + b + a

Such a symbolic function can be used for symbolic variables. In the example we find the critical point of the gradient.

>&gradient(f(x,y),[x,y]), &solve(%)
                        [2 x - y, 2 y - x + 1]


                                 2        1
                         [[y = - -, x = - -]]
                                 3        3

But the function can also be used for a numerical vector.

>v=[3,4]; f(v)
17

Some numerical algorithms, like the Nelder-Mead method, require a numerical function working for vectors. The Nelder-Mead method determines the minimum of a function.

>nelder("f",[0,0])
[-0.333333,  -0.666667]

There are also purely symbolic functions, which cannot be used numerically.

>function lapl(expr,x,y) &&= diff(expr,x,2)+diff(expr,y,2)
                 diff(expr, y, 2) + diff(expr, x, 2)

>&realpart((x+I*y)^4), &lapl(%,x,y)
                           4      2  2    4
                          y  - 6 x  y  + x


                                  0

But of course, they can be used in symbolic expressions or in the definition of symbolic functions.

>function f(x,y) &= factor(lapl((x+y^2)^5,x,y))
                          2     3     2
                     10 (y  + x)  (9 y  + x + 2)

To summarize

Solving Expressions

Expressions can be solved numerically and symbolically.

To solve a simple expression of one variable, we can use the solve() function. It needs a start value to start the search. Internally, solve() uses the secant method.

>solve("x^2-2",1)
1.41421356237

This works for symbolic expression too. Take the following function.

>px &= 4*x^8+x^7-x^4-x, plot2d(px,-1,1):
                             8    7    4
                          4 x  + x  - x  - x

00 - A Crash Course in Euler

Now we search the point, where the polynomial is 2. In solve(), the default target value y=0 can be changed with an assigned variable.
We use y=2 and check by evaluating the polynomial at the previous result.

>solve(px,1,y=2), px(%)
0.966715594851
2

Solving a symbolic expression in symbolic form returns a list of solutions. We use the symbolic solver solve() provided by Maxima.

>sol &= solve(x^2-x-1,x)
                       1 - sqrt(5)      sqrt(5) + 1
                  [x = -----------, x = -----------]
                            2                2

The easiest way to get the numerical values is to evaluate the solution numerically just like an expression.

>longest sol()
    -0.6180339887498949       1.618033988749895 

To use the solutions symbolically in other expressions, the easiest way is "with".

>&x^2 with sol[1], &expand(x^2-x-1 with sol[2])
                                         2
                            (sqrt(5) - 1)
                            --------------
                                  4


                                  0

There are many more functions to solve equations, like the Newton method or the bisection method. Moreover, there are interval methods, which return guaranteed inclusions of the solution.

Reference on Numerical Algorithms
Tutorial on Numerical Analysis
Tutorial on Interval Methods

Solving systems of equations symbolically can be done with vectors of equations and the symbolic solver solve(). The answer is a list of lists of equations.

>&solve([x+y=2,x^3+2*y+x=4],[x,y])
          [[x = - 1, y = 3], [x = 1, y = 1], [x = 0, y = 2]]

Solving systems of nonlinear equations numerically requires advanced numerical methods.

In general, a function is required for this. Here is just one short example.

>function f([x,y]) := [sin(x)+y-2,x^3+2*y+x-4];

The Broyden method is a good general method to solve such equations. But there are more methods. See

Newton and Quasi-Newton Methods

The Broyden algorithm does not need the derivative of the function.

>broyden("f",[1,1]), f(%)
[0.870078,  1.23562]
[0,  0]

We can also use a function instead of an expression. We define a tough integration, and like to solve

00 - A Crash Course in Euler

>function f(x) := integrate("x^x",1,x);

The secant method solves this very well.

>solve("f",2,y=1)
1.65678354575

The function f() can see global variables. But often we want to use local parameters.

00 - A Crash Course in Euler

with a=3.

>function f(x,a) := x^a-a^x;

One way to pass the additional parameter to f() is to use a list with the function name and the parameters (the other way are semicolon parameters).

>solve({{"f",3}},2,y=0.1)
2.54116291558

This does also work with expressions. But then, a named list element has to be used. (More on lists in the tutorial about the syntax of EMT).

>solve({{"x^a-a^x",a=3}},2,y=0.1)
2.54116291558

The Matrix Language

The documentation of the EMT core contains a detailed discussion on the matrix language of Euler.

The Matrix Language

Vectors and matrices are entered with square brackets, elements separated by commas, rows separated by semicolons.

>A=[1,2;3,4]
            1             2 
            3             4 

The matrix product is denoted by a dot.

>b=[3;4]; A.b
           11 
           25 

The main point of a matrix language is that all functions and operators work element for element.

>A*A
            1             4 
            9            16 

This is not the matrix product, but a multiplication element by element. The same works for vectors.

>b^2
            9 
           16 

With this and the colon operator a:delta:b, vectors of values of functions can be generated easily.

In the following example, we generate a vector of values t[i] with spacing 0.1 from -1 to 1. Then we generate a vector of values of the function

00 - A Crash Course in Euler

>t=-1:0.1:1; s=t^3-t
[0,  0.171,  0.288,  0.357,  0.384,  0.375,  0.336,  0.273,  0.192,
0.099,  0,  -0.099,  -0.192,  -0.273,  -0.336,  -0.375,  -0.384,
-0.357,  -0.288,  -0.171,  0]

In the following plot, we plot each point of the graph of the function. I.e., we plot the points [t[i],s[i]].

>plot2d(t,s,>points):

00 - A Crash Course in Euler

The parameter >points is a shortcut for the assigned parameter points=true. If we omit this flag, we get a coarse image of the function. To get more details, we simply add more points.

The function linspace(-10,10,1000) computes 1001 points with 1000 intervals between -10 and 10.

>t=linspace(-10,10,1000); s=sin(t/(t^2+1)); plot2d(t,s):

00 - A Crash Course in Euler

Of course, it is easier to use symbolic or numerical expressions. Moreover, EMT can use an adaptive algorithm to improve the plot in critical areas for expressions or functions.

>plot2d(&diff(x/(x^2+1),x),-10,10):

00 - A Crash Course in Euler

EMT expands operators for scalars, vectors, and matrices in the obvious way.

E.g., a column vector times a row vector expands to matrix, if an an operator is applied. In the following, v' is the transposed vector (a column vector).

>shortest (1:5)*(1:5)'
        1         2         3         4         5 
        2         4         6         8        10 
        3         6         9        12        15 
        4         8        12        16        20 
        5        10        15        20        25 

Note, that this is quite different from the matrix product. The matrix product is denoted with a dot "." in EMT.

>(1:5).(1:5)'
55

Using the matrix language, very complicated tables can be generated and plotted. We try

00 - A Crash Course in Euler

for x between 0 and 1, n=10, and k from 0 to 10. These polynomials are called the Bernstein polynomials.

In the computation of the matrix y, each row belongs to one value of k.

>n=10; k=0:n;  ...
 x=linspace(0,1,100); y=bin(n,k')*x^k'*(1-x)^(n-k');  ...
 plot2d(x,y):

00 - A Crash Course in Euler

A function which does not work for vector input should be "vectorized". This can be achieved by the "map" keyword in the function definition. Then the function will be evaluated for each element of a vector parameter.

The numerical integration integrate() works only for scalar interval bounds. So we need to vectorize it.

>function map f(x) := integrate("x^x",1,x)

The "map" keyword vectorizes the function. The function will now work
for vectors of numbers.

>f([1:5])
[0,  2.05045,  13.7251,  113.336,  1241.03]

Note that plots vectorize the functions automatically. This is necessary to compute adaptive plots anyway.

>plot2d("f",0,2):

00 - A Crash Course in Euler

Linear Algebra

EMT has lots of functions to solve linear systems, sparse systems, or regression problems.

Reference for Linear Systems

For linear systems Ax=b, you can use the Gauss algorithm, the inverse matrix or a linear fit. The operator A\b uses a version of the Gauss algorithm.

>A=[1,2;3,4]; b=[5;6]; A\b
           -4 
          4.5 

For another example, we generate a 200x200 matrix and the sum of its rows. Then we solve Ax=b using the inverse matrix. We measure the error as the maximal deviation of all elements from 1, which of course is the correct solution.

>A=normal(200,200); b=sum(A); longest totalmax(abs(inv(A).b-1))
 2.660094367001875e-013 

If the system does not have a solution, a linear fit minimizes the norm of the error Ax-b.

>A=[1,2,3;4,5,6;7,8,9]
            1             2             3 
            4             5             6 
            7             8             9 

The determinant of this matrix is 0.

>det(A)
0

We can only get an optimal fit. That is a vector x which minimizes the norm of Ax-b. Euler uses orthogonal transformations to find this fit.

>b=[1,2,4]'; x=fit(A,b)
     0.166667 
     0.333333 
            0 

The same fit is used for polynomial fits to data. We fit a polynomial of degree 2 to the following data.

>x=1:5; y=[2,3,3,4,5.2]; p=polyfit(x,y,2)
[1.92,  0.14,  0.1]

The we can plot the points and the polynomial into one plot using two calls to plot2d and the parameter >add.

>plot2d(x,y,>points); plot2d("polyval(p,x)",>add):

00 - A Crash Course in Euler

Since EMT contains strong algorithms for minimization, we can also fit an exponential function or a mixed exponential and linear function to the data.

The function modelfit() makes this easy. It needs a model function.

>function fexp (x,[a,b,c]) := a*x+b*exp(c*x)

Then it takes either the Nelder-Mead or Powell's algorithm to minimize the error.

>p=modelfit("fexp",[1,2,-1],x,y)
[0.978624,  1.92445,  -0.535012]
>plot2d(x,y,>points); plot2d("fexp(x,p)",>add):

00 - A Crash Course in Euler

Statistics

EMT can very well be used for statistical experiments or evaluations.

Tutorial
Reference

There are many functions, which generate a distribution. For a normal distribution with mean and variance use randnormal(n,m,mean,dev). It produces a nxm matrix of random values. The plot2d() functions can plot distributions of values.

>a=randnormal(1,1000,5,1); plot2d(a,>distribution, ...
   style="/",fillcolor=darkgray):

00 - A Crash Course in Euler

A cumulative sum of a 0-1-normal distributed values produces a random walk.

>plot2d(cumsum(randnormal(1,1000))):

00 - A Crash Course in Euler

Using two rows shows a walk in two dimensions.

>X=cumsum(randnormal(2,1000)); plot2d(X[1],X[2]):

00 - A Crash Course in Euler

There are special plots for statistics. One is columnsplot(). We use it to display the number of 1s to 6s in 600 dice throws.

To simulate dice throws, we can use intrandom(n,m,6). Then we need to count the multiplicities of the values 1 to 6 in this vector.

>w=intrandom(1,600,6);  ...
 columnsplot(getmultiplicities(1:6,w),style="-",color=darkgray):

00 - A Crash Course in Euler

Together with the matrix language, this can be used for effective analysis of statistical data.

For a simple example, we generate a million events uniformly between 0 and 1 using random(n,m) or random(m). Then we plot the distribution of the waiting times between the events. To get the waiting times, we sort the random values and get our vector of events. Then the function differences() computes successive differences between the elements of this vector.

>n=1 000 000; s=random(n); d=differences(sort(s));  ...
 plot2d(d*n,>distribution):

00 - A Crash Course in Euler

The mean waiting time is 1/n.

>mean(d)*n
0.999999550396

The standard deviation of the waiting time is less obvious. But it is also 1/n.

>dev(d)*n
0.998355575507

The distribution of the differences (scaled with the number of data n) is indeed an exponential distribution.

>plot2d("exp(-x)",color=red,thickness=2,>add):

00 - A Crash Course in Euler

A boxplot shows the quartiles of this distribution and lots of outliers. By definition, outliers in a boxplot are data which exceed 1.5 times the middle 50% range of the plot.

>boxplot(d*n,pointstyle="."):

00 - A Crash Course in Euler

Plots

The main functions are plot2d and plot3d.

Tutorial for plot2d
Tutorial for plot3d
Reference

For 2D plots of data, functions, curves, points or polygons we use plot2d(). We have already shown how to plot expressions or functions.

>function f(x,s) := 1/(s*sqrt(2*pi))*exp(-x^2/(2*s^2))

To plot functions, we can use the function name.

If necessary, additional parameters are passed by semicolon parameters. In the following, we call f(x,s) with s=2. The semicolon parameter belongs between the parameters and the assigned parameters.

>plot2d("f",-10,10;2,color=2,thickness=2):

00 - A Crash Course in Euler

All utility functions in EMT, which accept function names, do also accept semicolon parameters. These extra parameters will be passed to the function f(x,...).

>integrate("f",-20,20;2), integrate("f(x,2)",-20,20)
1
1

For plots of data, we can use two vectors.

Again, we add another plot with >add in the second plot2d() command.

>n=10; k=0:n; b=bin(n,k)*2^-n;  ...
 plot2d(k,b); plot2d(k,b,>points,>add):

00 - A Crash Course in Euler

The function statplot() adds lines and points automatically and is designed for statistics.

In the example, we generate a matrix with two rows for two different binomial distributions using the matrix language of EMT. We plot each row with a different color.

>n=10; k=0:n; p=[0.4,0.7]'; b=bin(n,k)*p^k*(1-p)^(n-k);  ...
 statplot(k,b,color=[green,blue]):

00 - A Crash Course in Euler

Polygons are drawn with a vector of x and y coordinates. They can be filled. Use the matrix language to get vectors elegantly!

In this example, we plot the curve

00 - A Crash Course in Euler

with

00 - A Crash Course in Euler

>t=linspace(0,2pi,1000); r=1+sin(3*t)/2; x=r*cos(t); y=r*sin(t); ...
 plot2d(x,y,>filled,fillcolor=red,style="/",r=1.5):

00 - A Crash Course in Euler

For column plots, we already saw the function columnsplot(). But with the parameter >bar plot2d() can produce column plot of all kinds.

>plot2d([-2,0,1,3,4],[1,2,3,2],>bar,fillcolor=gray,style="\/"):

00 - A Crash Course in Euler

Note that you are not restricted to square plots. You can either set a non-square aspect globally, or for only a few plots.

>aspect(2); plot2d(["sin(x)","cos(x)"],0,2pi,color=[red,blue]):

00 - A Crash Course in Euler

You need to release the 1:2 aspect to revert to the default square ratio.

>aspect();

A 3D plot of a function uses plot3d. The easiest way is to plot an expression in x and y. The parameter r set the range of the plot around (0,0).

>plot3d("x^2+sin(y)",r=pi):

00 - A Crash Course in Euler

The default style can be changed. For the following contour plot, we use a finer grid fo 100x100 points, scale the function and the plot, and use different angle of view.

>plot3d("exp(-x^2-y^2)",r=2,n=100,level="thin", ...
   >contour,>spectral,fscale=1,scale=1.1,angle=45°,height=20°):

00 - A Crash Course in Euler

For 3D objects, you need to provide a matrix of x-, y- and z-values, or three functions or expressions fx(x,y), fy(x,y), fz(x,y).

In the following example, we use a vector of t values and a column vector of s values to parameterize the surface of the ball. In the drawing we can mark regions, in our case the polar region.

>t=linspace(0,2pi,180); s=linspace(-pi/2,pi/2,90)'; ...
 x=cos(s)*cos(t); y=cos(s)*sin(t); z=sin(s); ...
 plot3d(x,y,z,>hue, ...
   color=blue,<frame,grid=[10,20], ...
   values=s,contourcolor=red,level=[90°-24°;90°-22°], ...
   scale=1.4,height=50°):

00 - A Crash Course in Euler

Here is a plot with three functions.

>plot3d("x","x^2+y^2","y",r=2,zoom=3.5,frame=3):

00 - A Crash Course in Euler

It is also possible to plot a curve in 3D. In this case, it is easier to precompute the points of the curve.

>t=linspace(0,8pi,500); ...
 plot3d(sin(t),cos(t),t/10,>wire,zoom=3):

00 - A Crash Course in Euler

Of course, a point cloud is also possible.

>plot3d(normal(500),normal(500),normal(500),>points):

00 - A Crash Course in Euler

EMT can also plot in anaglyph mode. To view such a plot, you need red/cyan glasses.

>X=cumsum(normal(3,100)); ...
 plot3d(X[1],X[2],X[3],>anaglyph,>wire):

00 - A Crash Course in Euler

Special Characters

Special characters can be used in plots, comments and output. For this, the easiest way is to use Unicode entities of the form &text; in string constants of the form u"...". These Unicode strings can be concatenated like other strings.

>u"&alpha; = " + deg(acos(0.6)) + u"&deg;"
α = 53.1301023542°

In comments, the same entities can be inserted. Thus greek letters like α, β or symbols like Windows® can be used.

In plots, titles, labels, label boxes, text boxes and other text can contain Unicode characters.

>plot2d("x^3-x",title=u"x &rarr; x&sup3;-x"):

00 - A Crash Course in Euler

This does also work in vertical labels.

>plot2d("sinc(x)",-2pi,2pi,xl=u"&alpha;",yl=u"sinc(&alpha;)",>vertical):

00 - A Crash Course in Euler

Linear Programming

EMT contains an implementation of the Simplex algorithm for linear optimization, a simple implementation of the branch and bound algorithm for integer optimization, and algorithms from the LPSOLVE package.

Tutorial
Reference

Additionally, there are some non-linear algorithms. For a start, we solve a very simple problem

00 - A Crash Course in Euler

00 - A Crash Course in Euler

>A=[1,2;2,1;1,1]
            1             2 
            2             1 
            1             1 
>b=[3,3,2]'
            3 
            3 
            2 
>c=[1,1]
[1,  1]
>simplex(A,b,c,>max)
            1 
            1 

The default for the simplex is to assume conditions of type <=, but other conditions are possible too (>= or =). Let us try changing the first condition to

00 - A Crash Course in Euler

We need to change b[1], and add an additional parameter, which denotes the type of condition (0=equal, -1=less equal, 1=greater equal).

>b[1]=3.1; eq=[0,-1,-1]'
            0 
           -1 
           -1 
>simplex(A,b,c,eq,>max)
          0.9 
          1.1 

EMT has also a very simple implementation of the branch-and-bound algorithm for integer problems.

>intsimplex(A,4*b,c,>max)
            4 
            4 

The algorithm can relax the integer condition for specific variables, or relax the condition x>=0 etc. For more information

Tutorial on Optimization

There is also an efficient implementation of algorithms for optimization in the LPSOLVE package. The interface is very similar to intsimplex().

>ilpsolve(A,4*b,c,>max)
            4 
            4 

Differential Equations

EMT has symbolic and numerical algorithms to solve a differential equation.

Tutorial on Differential Equations

To solve the first order equation

00 - A Crash Course in Euler

numerically, we use ode() and an expression. By default, the LSODA algorithm is used.

>x=linspace(0,4pi,100); y=ode("sin(y)*x",x,1); plot2d(x,y):

00 - A Crash Course in Euler

We can also solve to the left side of the initial point 0.

>x1=linspace(0,-4pi,100); y1=ode("sin(y)*x",x1,1);

To plot both into the same plot, we use add.

>plot2d(x,y,a=-4pi,b=4pi,c=0,d=4); plot2d(x1,y1,>add):

00 - A Crash Course in Euler

For a symbolic example, we solve

00 - A Crash Course in Euler

exactly. The main symbolic function for this is ode2(). We first get a general solution.

>sol &= ode2('diff(y,x,2)+'diff(y,x)+y=x^2,y,x)
             - x/2          sqrt(3) x            sqrt(3) x      2
        y = E      (%k1 sin(---------) + %k2 cos(---------)) + x
                                2                    2
                                                                 - 2 x

With ic2, we can determine the constants.

>yx &= y with ic2(sol,x=0,y=0,'diff(y,x)=1)
                         - x/2     sqrt(3) x     2
              2 sqrt(3) E      sin(---------) + x  - 2 x
                                       2

Let us evaluate this in x=2.

>longest yx(2)
      1.257838888998996 

For a numerical solution, we need to rewrite the second order equation into a system of equations.

>function f(x,y) := [y[2],x^2-y[2]-y[1]]

Then we can solve the equation with ode. Note that the default adaptive algorithm does not need intermediate points. We get only the values at x=0 and x=2.

>x=[0,2]; longest ode("f",x,[0,1])
                      0       1.257838889010009 
                      1       1.193884206433507 

For a more instructive example, we plot a vector field for the autonomic equation

00 - A Crash Course in Euler

>function f(x,y) := [-y[2],y[1]+y[1]^2]; ...
 vectorfield2("-y","x+x^2",-2,2,-2,2):

00 - A Crash Course in Euler

Now, we solve the equation, starting from

00 - A Crash Course in Euler

>x=0:0.1:2pi; y=ode("f",x,[0,-1]); ...
 plot2d(y[1],y[2],color=red,thickness=2,>add):

00 - A Crash Course in Euler

Good Luck

The author wishes you a lot of fun with math and EMT! There is a lot to learn. So keep experimenting. That's what EMT was made for.

Euler Home