| | Calculation / Examples
The examples below cover a cross section of the functionality provided by the system.
Example 1: Data types Example 2: Programming Example 3: Optimization Example 4: Numerics Example 5: Set theory Example 6: Logic & Boolean Algebra Example 7: Calculus and symbolic computation Example 8: Data analysis and statistics Example 9: Graphics and visualization Example 10: Linear Algebra
Example 1: Data types Keywords: Ratios, floating point values and if-then-else expressions Assume a FIFO queue with Poisson arrival process and one server. Following parameters are given: Offered traffic (A), average service time (s) and form factor (f). Using the Pollaczek-Khintchines formula the average waiting time for all customers (W1) and the average waiting time for customers with a positive waiting time (W2) are calculated. Input:
temp:=f/(2*(1-A)): W1:=if (A<=1) then A*s*temp else -1; W2:=if (A<=1) then s*temp else -1;
Output:
-: W1 := (if (A<=1) then (A*s*f/(2*(1-A)))else -1)
Output:
-: W2 := (if (A<=1) then (s*f/(2*(1-A)))else -1)
Assigning values to the input variables let us calculate W1 and W2. Input:
temp:=f/(2*(1-A)): W1:=if (A<=1) then A*s*temp else -1: W2:=if (A<=1) then s*temp else -1: A:=7/10: s:=1: f:=4 /* Typical form factor for phone services */: W1; W2;
Output:
-: (14/3)
Output:
-: (20/3)
Notice how the answers are given as ratios. The system tries to keep ratios as long as they are not involved in a real expression. Using a real number in calculations yields real numbers in the answers. Input:
temp:=f/(2*(1-A)): W1:=if (A<=1) then A*s*temp else -1: W2:=if (A<=1) then s*temp else -1: A:=7/10: s:=1: f:=4 /* Typical form factor for phone services */: W1: W2: 1.0*W1; 1.0*W2;
Output:
-: 4.66666666667
Output:
-: 6.66666666667
goto top
Example 2: Programming Keywords: Functions, procedures, loops, for-next statement, while statement, intervals and if-then-else statement Functions of type sin(t^n), t ∈ [0..2π] are to be plotted for different integer values of n. First the time vector t is constructed as 150 values spread evenly in the interval. The function space(<expression>,<variable> == <interval>,<n>) evaluates the expression for n different evenly distributed values of the variable in the given interval and returns the evaluated values of the expression as a row vector. Input:
t := space(x,x==[0..4.0*pi],150);
Output:
-: t := {0.0 , 0.0490873852123 , 0.0981747704247 ...
The functions of type sin(t^n) are stored in the environment as a function of to variables. Input:
f(x,n) := sin(x^n);
Output:
-: f(x,n) := sin(x^n);
A procedure utilizing the above defined function is created to output the value of n before plotting the function sin(t^n). Input:
proc plotproc(x,n) := local y:=f(x,n); plot(line,x,y) end;
Output:
-: proc plotproc(x,n) := local y:=f(x,n); plot(line,x,y) end;
Using a for statement the procedure plotproc is called for a number of different integer values of n Input:
for counter :=1 to 3 do counter; do plotproc(t,counter) next;
The for-next and the while statements provide for loops. Here a vector - or matrix with only one row - is constructed with every other element a square and the rest negativ numbers Input:
for a:=1 to 9 step 2 do M[a]:=a^2: next; M
Input:
a:=2: while a<9 do M[a]:=-a: a:=a+2: end; M
Another way of constructing the matrix M is to use the if-then-else statement to seperate between then two halfs. Input:
for a:=1 to 9 do if (a mod 2) == 0 then M[a]:=-a: else M[a]:=a^2: end: next; M
goto top
Example 3: Optimization Keywords: Vectors, matrices, loops, for-next, while, if-then-else Mr. Smith would like to open a booth at the upcoming city fair. He has to find out which products to sell. From a store he can buy boxes of apples (A), bananas (B), oranges (O), nuts (N) and pears (P). He is sure, that selling both apples and pears will not do (not (A and P)), that he should only sell oranges with nuts and vice versa (O <-> N), that he should at least sell one of the more solid fruits - nuts excluded (A or B or O or P), that he needs to sell apples or pears (A or P) and that he should either sell bananas or oranges, but not both of them (B xor O). Mr. Smith finds the set of possible solution to his criteria. Input:
solveb({not(A and P), O <-> N, A or B or O or P, A or P, B xor O},{A,B,O,N,P});
Output:
-: {false , false , true , true , true ; false , true , false , false , true ; true , false , true , true , false ; true , true , false , false , false}
Each row contains a valid solution to the variables. The management at the fair requires all sellers to carry at least three different products. Having to choose between the first and the third option, he select the third based on his personal preferences. He will sell apples, oranges and nuts, but no bananas and pears. Input:
X:={A,O,N}:
At the store he has to pay 4 for boxes of apples and 5 for boxes of oranges or nuts. Input:
P:={4,5,5}:
A box of apples weighs 8, a box of oranges 5 and a box of buts 3. Input:
W:={8,5,3}:
Knowing that he can only load 10 boxes in his car, he can buy from zero to 10 of each type of fruit, that he in total can pay the store up to 70 and that his car can carry boxes with a total weight of 40, he finds a number of possible solutions to his assignment problem. He uses the solvei function to find the valid solutions and then the nrows function to find the number of valid solutions. Input:
result := solvei({X*P` <= 70, X*W` <= 40},{A == [0..10], O==[0..10], N==[0..10]}): nrows(result);
Output:
-: 153
Having a large amount of solutions, Mr Smith wants to find the solution where he can make the biggest profit. He knows he can earn 3 for each box of apples, 4 for each box of oranges and 7 for each box of nuts. He uses the solvei function again - this time with a criteria to maximize. Input:
E := {3,4,7}: criteria := X*E`: result1 := solvei({X*P` <= 70, X*W` <= 40},{A == [0..10], O==[0..10], N==[0..10]},criteria);
Output:
-: result1 := {0,2,10}
Now he only gets only solution: He should buy two boxes of oranges and 10 of nuts but leave the apples alone. This is not consistent with his first decision to sell apples, oranges and nuts. He decides to add requirements to make sure he sells all three products. Input:
result2 := solvei({X*P` <= 70, X*W` <= 40, A>0, O>0, N>0},{A == [0..10], O==[0..10], N==[0..10]},criteria);
Output:
-: result2 := {1,1,9}
Once again he gets one solution: He should buy one box of apples, one boxes of oranges and nine boxes of nuts. His estimated profit is then Input:
result2*E`;
Output:
-: 70
Had he stayed with only oranges and nuts his profit would have been Input:
result1*E`;
Output:
-: 78
goto top
Example 4: Numerics Keywords: Curve fitting, nonlinear regression, differentiation, integration, rootfinding In an experiment the dependent values in vector y have been measured for the independent values in vector x, i.e. y = f(x). Input:
x:={2.0,2.2,2.6,2.7,2.8,2.9,3.1,3.2,3.3,3.6}`: y:={1.6,2.0,1.8,2.8,2.1,2.0,2.6,2.2,2.6,3.0}`:
A nonlinear relationsship between the values ist assumed. In this case the model y = a + b*x + c*x^2 is chosen. The coefficients a, b and c are unknown. The vector f defines this relationsship using the variable q. Input:
f:={1, q, q^2}:
The coefficients a, b and c are calculated using least-square regression. Input:
for temp:=nrows(x) to 1 step -1 do M[temp,...]:=map(x[temp,...],f,q): next; temp:='temp': M: {coeff,H}:=solvem(M,y): /* H is NaN for an over-determined system */ {a,b,c}:=coeff`;
Output:
-: a := 0.971497202483 -: b := 0.24799530993 -: c := 0.0717281648134
Now one can plot the measurements, the regression fit and the residual. Input:
Y:=M*coeff: plot(x,y); /* Measurements */ plot(line,x,Y); /* Fitted curve */ plot(x,y-Y); /* Residual */
Using the coefficients (a, b, c ) and the model (f) the fitted polynomium p as a function of the independent variable q is constructed. Input:
p:=sum(f.*coeff`); plot(p,q==[-10..10]);
The derivative of p is found and evaluated for q==2.5. Input:
dp:=diff(p,q); subs(dp,q,2.1);
The derivative of p in q==2.5 can also be found directly without first explicitly differentiating the polynomium. Input:
difff(p,q==2.1);
The indefinite and one definite integral of the ploynomium p is found. Input:
int(p,q); int(p,q==[2.0..3.0]);
Roots in p are determined. Input:
solve(p,q)
goto top
Example 5: Set theory Keywords: Union, intersection, difference, member, discrete mathematics Given a number of elements in a vector or matrix one can construct the equivalent set, where identical elements are left out Input:
A:={1,2,2,3,x,y}; A:=makeset(A);
The union as well as the intersection of two sets are found using the buildin functions. The difference of two sets are the elements of the first set that do no occur in the second set. Input:
B:={3,4,5,y,z}; union(A,B); difference(A,B);
We can also find the elements that belong in one of the set, but not in both. Input:
symdifference(A,B);
Let's see if x is a member of the two sets A and B. Input:
ismember(A,x); ismember(B,x);
We can also find out if a set is a subset of another set using the ismember function in addition to the all function. Input:
C:={3,x,y}; all(ismember(A,C)); all(ismember(B,C));
goto top
Example 6: Logic & Boolean Algebra Keywords: Logic, booleans, relations, discrete mathematics Given a number of boolean expressions one can try to simplify them using the default simplification capabilities. Input:
x and (x and y); (x or not x) and y;
To find the solutions to more advanced boolean expressions one can use the solveb function. A number of expressions, which should evaluate to true and a list of variables are given as function arguments. Input:
solveb({x and (x and y), (x or not x) and y},{x,y})
Bit vectors are constructed using the bvmake function. We construct two 6-bit bit vectors for the integers 13 and 5. Input:
x:=bvmake(6,13); y:=bvmake(6,5);
Boolean operators for bit vectors are applied. Input:
a:=bvnand(x,y); b:=bvxor(x,y);
Some arithmetic functions for bit vectors can also be used. Input:
m:=bvmul(x,y); {hi,lo}:=bvsplit(m); bvtoint(hi); bvtoint(lo);
goto top
Example 7: Calculus and symbolic computation Keywords: Derivative, integral, equation solving, expand, factor Given a function f one can find the derivative and the integral Input:
f:=2*x^3-sin(x); df:=diff(f,x); F:=int(f,x);
Let's see if the results are correct. Input:
int(df,x); f == ans; diff(F,x); f == ans;
Solving an equation is equal to find the roots of the equivalent function. Input:
f:=2*(1/(a*ln(x)^2))^3; solve(f==12,x);
We plot the function to see the . Input:
a:=0.5; plot(f,x==[2..20]);
The extrema is found by setting the derivative equal to zero. Numerical differentiation is used to check the result. Input:
solve(diff(f,x),x); difff(f,x==ans);
An expression being a sum of many subexpressions containing common factors can be rewritten to better see the structure. Input:
f:=x*a*y*b+c*x*d*y; factor(f);
Likewise an expression containing powers and multiplication can be expanded. Input:
f:=2*(x+3)^3-(5*x-2)^4; expand(f);
goto top
Example 8: Data analysis and statistics Keywords: Discriptive statistics, summary statistics, central tendency Given a data set in a matrix, a number of statistics can be calculated. Input:
X:={1,2,3,4;5,6,7,8;9,10,11,12}; s:=sum(X); p:=prod(X); m:=mean(X); v:=variance(X); sd:=stddev(X);
Beside the arithmetic mean, the geometric and the harmonic mean can be found. Input:
g:=geomean(X); h:=harmean(X);
Finding the minimun, first quartile, mean, third quartile and maximum is achieved through the quartiles function. Input:
{min, q1, av, q2, max} := quartiles(X);
A distribution of the data can be plotted. Input:
X2:=sort(X); plot('col',X2);
goto top
Example 9: Graphics and visualization Keywords: Graphics, visualization, plots, diagrams The visualization functionality is centered around the plot statement. A function of one variable can be plotted directly Input:
f:=2*x^2-5*x+3; plot(f,x==[-5..10]);
A parametric plot is constructed using two function expressions. Please note, that the plot type (par) and the plot symbol (cross) can be left out. Input:
plot(par, cross, sin(t), cos(t), t==[0..2*pi]);
A data set can be presented in a bar plot, a column plot or in pie chart. Input:
X:={1,4,2,7,3,2,6,4}; plot(bar,X); plot(col,X); plot(pie,X);
Assuming the data set represents the dependent variables corresponding to an independent variable 1,2,...,n we plot using the default xy plot type and the dot plot symbol. Input:
plot(xy,dot,X);
In case we had both the independent and the dependent values we still use the xy plot type. This time the ring plot symbol is used. Input:
X:={1,3,5,6,7,8}; Y:={5,3,2,7,6,4}; plot(xy,ring,X,Y);
goto top
Example 10: Linear Algebra Keywords: Vectors, matrices, linear algebra, system of equations, change of basis Given three vectors a1, a2 and a3 in an ordinary three-dimensional basis e. Input:
a1 := {1,2,-1}; a2:= {0,3,-1}; a3:={-4,11,-2};
If the three vectors are a basis for the space, the matrix eMa consisting of the vectors as colums must be regular. Us use the inv functions to find the inverse matrix aMe. If however the function returns NaN, the matrix eMa is not regular. Input:
eMa[...,1] := a1`: eMa[...,2] := a2`: eMa[...,3] := a3`; aMe := inv(eMa);
Having a regular matrix, we also have another basis for the space. We can transform the coordinates of the vectors eX1 and eX2 from basis e to basis a. Input:
eX1 := {1,5,-2}`; eX2:= {-1,3,-1}`;
Input:
aX1 := aMe * eX1; aX2 := aMe * eX2;
The inverse transformation results in the old coordinates in basis e. We check to make sure. Input:
eX1 == eMa * aX1; eX2 == eMa * aX2;
Source: Eising, Jens: Lineær Algebra, Matematisk Institut, Danmarks Tekniske Højskole 1983, p. 154 goto top Please note, that Mate only stores values between calculations within sessions. Therefore all calculations based on each other must be entered at the same time if used outside of a session. | | This free online symbolic calculator and solver enables you to define variables and functions as well as to evaluate expressions containing numbers in any number system from 2 (binary) over 8 (octal), 10 (decimal) and 16 (hexadecimal) to 36, roman numerals, complex numbers, intervals, variables, matrices, function calls, Boolean values (true and false) and operators (and, or, not ...), relations (e.g. greater than) and the if-then-else control structure. Comments are C-style /* */ or //. Plots are available using the plot statement. | |