# Jasymca

## Quick Guide

#### Numbers

Numbers are entered in the usual Computer format: with optional decimal point and decimal exponent following the letter e (or E). The numbers 5364 and $-1.723478265342 10^\left\{12\right\}$ should be entered like:

>> 5364
ans = 5364
>> -1.723478265342e12
ans = -1.7235E12


Most of the time these will be stored as floating point data (double, IEEE standard 754). They are rounded to 5 significant digits for display, but for calculations the full precision of this format is always preserved (15-16 decimal digits). By switching the format (format long) all significant places are displayed.

>> format long
>> -1.723478265342e12
ans = -1.723478265342E12


As an extension Jasymca offers the command format Base Number, which is used to display numbers in a system with arbitrary Base with any Number of significant digits. To display numbers with 15 digits in the binary system we type:

>> format 2 15
>> -1.723478265342e12
ans = -1.1001000101001E40


Using format short returns the display mode to default (short decimal). It should be emphasized, that none of the format commands influences the internal representation and accuracy of floating point numbers. Numbers, which are entered without decimal point and exponent, and which are larger than $10^\left\{15\right\}$ are stored as exact rational datatype. These numbers are internally represented as quotient of two variable length integers (java datatype BigInteger), which allows you to perform calculations without any rounding errors. In the first case of the following example a floating point number is generated, in the second case an exact rational:

>> 10000000000000001.
ans = 1.0E16
>> 10000000000000001
ans = 10000000000000001


Each floating point number Z can be converted to an exact number using the command rat(Z). The conversion is accomplished by continued fraction expansion with an accuracy determined by the variable ratepsilon (default: $10^\left\{-8\right\}$).

>> rat(0.33333333333333333)
ans = 1/3


Operations between exact and floating point numbers always lead to the promotion of floating point numbers. Calculations can be performed without rounding errors by rationalizing just the first number.

>> 1/21/525/21/5*7*175*63*15-1
ans = -4.4409E-16
>> rat(1)/21/525/21/5*7*175*63*15-1
ans = 0


Conversely, the command float(Z) converts numbers into floating point format. Both commands also work for composite datatypes, like polynomials and matrices, whose coefficients are transformed in one step. Irrational function values of exact numbers and constants like pi remain unevaluated until the float-command is issued.

>> sqrt(2)
ans = 1.4142
>> sqrt(rat(2))
ans = sqrt(2)
>> float(ans)
ans = 1.4142


The exact datatype is useful especially for unstable problems, like solving systems of linear equations with ill-conditioned matrix. The Hilbert-matrix is an extreme example:

>> det( hilb(20)*invhilb(20) )
ans = 1           % correct
>> det( float(hilb(20))*float(invhilb(20)) )
ans = 1.6713E151  % slightly wrong


Imaginary numbers are marked with an immediately following i or j. This will work even if the predefined variables i and j have been overwritten.

>> 2+3i ans = 2+3i

How to print out the output values if you are working with the script, rather than with the prompt. Just use ?printf? method:

x=log(sqrt(854)); % natural logarithm printf('Answer=%f\n', x);

#### Symbolic Variables

In contrast to the examples Octave and Matlab, Jasymca integrates numeric and symbolic datatypes at the core of the program; symbolic math is not treated as an add-on. This means that with few exceptions most operations accept any mixture of numeric and symbolic arguments using the same commands and command syntax. Symbolic variables should not be confused with variables as discussed until now. These latter variables serve as address for an object in memory (the ?environment?), while symbolic variables are algebraic objects on their own. That means if x is a conventional variable, entering x in the text input field makes Jasymca search in the environment for the corresponding object, which then replaces x. If however x is a symbolic variable, the same action will lead to the creation of a first-degree polynomial with variable x and coefficients 1 and 0. In Octave-mode, each symbolic variable x must be declared as symbolic by entering syms x before using it. The command clear x deletes the symbolic (actually any) variable x.

>> x=3;             % nonsymbolic variable
>> x^2+3-2*sin(x)   % placeholder for '3'
ans = 11.718
>> syms x           % symbolic variable
>> x^2+3-2*sin(x)   % create function
ans = -2*sin(x)+(x^2+3)

##### Variables

Variables are declared by supplying a name and value in the format name=value. The name can be any character sequence. With the exception of the first character it may also contain numbers. The value is any number or expression.

>> x=24+3i
x = 24+3i



##### eigen(A)
% Eigenvalues of A (1 returnvalue).

>> A=[2 3 1; 4 4 5; 2 9 3];
>> eigen(A)
ans = [ 11.531  1.062  -3.593 ]


Comparison of LAPACK and Jasymca Routines We calculate the 4-th degree regression polynomial for the following x,y-data:

>> x=[1:6],y=x+1
x = [ 1  2  3  4  5  6 ]
y = [ 2  3  4  5  6  7 ]
>> polyfit(x,y,4)
p =
5.1958E-14   -9.6634E-13  -2.4727E-12  1   1


The coefficients p(1),p(2),p(3) should vanish since x and y represent a perfect straight line. This is an unstable problem, and it can be easily extended to make Jasymca completely fail. In our second attempt we use the Lapack-routine linlstsq:

>> x=[1:6],y=x+1;
>> l=length(x);n=4;
>> X=(x'*ones(1,n+1)).^(ones(l,1)*(n:-1:0))
>> linlstsq(X,y')
ans =
-1.6288E-18
-7.0249E-17
1.0653E-15
1
1


The coefficients p(1),p(2),p(3) are now significantly smaller. This particular problem can be solved exactly using Jasymca-routines and exact numbers, which avoids any rounding errors:

>> x=rat([1:6]);y=x+1;
>> polyfit(x,y,4)
p =
0  0  0  1  1


## Equations

##### Systems of Linear Equations

Solving systems of linear equations is accomplished by either the function linsolve(A,b) (all versions of Jasymca), or linsolve2(A,b) (LAPACK, not in applet and midlet). In both cases A is the quadratic matrix of the system of equations, and b a (row or column) vector representing the right-hand-side of the equations. The equations may be written as A?z=b and we solve for z.

>> A=[2 3 1; 4 4 5; 2 9 3];
>> b=[0;3;1];
>> linsolve(A,b)
ans =
-0.25
-0.13636
0.90909
>> linsolve2(A,b) % not Applet or Midlet
ans =
-0.25
-0.13636
0.90909


For large numeric matrices one should use the LAPACK-version if available. The Jasymca version can also handle matrices containing exact or symbolic elements. To avoid rounding errors in these cases it is advisable to work with exact numbers if possible:

>> syms x,y
>> A=[x,1,-2,-2,0;1 2 3*y 4 5;1 2 2 0 1;9 1 6 0 -1;0 0 1 0]
A =
x    1    -2   -2   0   % symbolic element
1    2    3*y  4    5   % symbolic element
1    2    2    0    1
9    1    6    0    -1
0    0    1    0    0
>> b = [1 -2  3  2  4 ];
>> trigrat( linsolve( rat(A), b) )

ans =
(-6*y-13/2)/(x+8)
(20*y+(-9*x-151/3))/(x+8)
4
((-3*x+10)*y+(-49/4*x-367/6))/(x+8)
(-34*y+(13*x+403/6))/(x+8)

##### Nonlinear Equations

?Equation? in the following means the equation expression = 0. Equations are solved for a symbolic variable x by the function solve(expression, x). If expression is a quotient, then nominator = 0 is solved. Jasymca uses the following strategy to solve equations: Unordered List ItemFirst, all occurrences of the variable x in expression are counted, both as free variable and embedded inside functions. Example: In x3?sin(x)+2x2?x?1????? x occurs three times: as free variable, in sin(x) and in x?1?????. Unordered List ItemIf this count is one, then we are dealing with a polynomic equation, which is solved for the polynomial's main variable, e.g. z. This works always, if the polynomial's degree is 2 or of it is biquadratic, otherwise only, if the coefficients are constant. In the next step the solution is solved for the desired variable x. As an example: Jasymca has to solve sin2(x)?2sin(x)+1=0 for x. It first solves z2?2z+1=0 for z and then sin(x)=z for x. Examples with free variables:

>> syms x,b
>> solve(x^2-1,x)
ans = [ 1  -1 ]
>> solve(x^2-2*x*b+b^2,x)
ans = b


An example with function variable (exp(j?x)):

>> syms x
>> float( solve(sin(x)^2+2*cos(x)-0.5,x) )
ans = [ 1.438i  -1.438i  -1.7975  1.7975 ]


If count is 2, only one case is further considered: The variable occurs free and inside squareroot. This squareroot is then isolated, the equation squared and solved. This case leads to additional false solutions, which have to be sorted out manually.

>> syms x
>> y=x^2+3*x-17*sqrt(3*x^2+12);
>> solve(y,x)
ans = [ -32.501  26.528 -1.3931E-2-2.0055i  -1.3931E-2+2.0055i ]


In all other cases Jasymca gives up.

##### Systems of Nonlinear Equations

Coupled systems of equations can be solved by the function algsys([expressions],[symbolic variables]). First, all linear equations are solved using the Gauss-method, then each equation is fed through solve() and the solution used to eliminate one variable in all other expressions. The equations are treated in the order they are supplied. This method only works for simple systems. The solution is provided as vector of solution vectors, each individual solution in as linear factor: In the first example below there is one solution with xs=-2/3, a2=3/4, a0=2, a1=0, the second example has two solutions.

>> syms xs,a0,a1,a2
>> algsys([2-a0,a1-0,a2*xs^2+a1*xs+a0-3-xs, 2*a2*xs+a1+1],[a2,a1,a0,xs])
ans = [ [ xs+2/3  a2-3/4  a0-2  a1 ] ]
>> syms a,xs
>> algsys([a*xs+3*a-(3-xs^2),a+2*xs],[a,xs])
ans = [ [ -sqrt(6)+(xs+3)  2*sqrt(6)+(a-6) ]  [ sqrt(6)+(xs+3)  -2*sqrt(6)+(a-6) ] ]
>> float(ans)
ans = [[ xs+0.55051  a-1.101 ] [ xs+5.4495  a-10.899 ]]


## Polynomials

Jasymca can handle polynomials with symbolic variables. In this chapter, however, we work with the Matlab/Octave/Scilab-approach of using vectors as list of polynomial coefficients: A polynomial of degree n is represented by a vector having n+1 elements, the element with index 1 being the coefficient of the highest exponent in the polynomial. With poly(x) a normal polynomial is created, whose zeros are the elements of x, polyval(a,x) returns function values of the polynomial with coefficients a in the point x, roots(a) calculates the zeros, and polyfit(x,y,n) calculates the coefficients of the polynomial of degree n, whose graph passes through the points x and y. If their number is larger than n+1 a least square estimate is performed. The regression analysis in exercise 8 was performed using this method. The roots of a 4th-degree polynomial are -4,-2,2,4 and it intersects the y-axis at (y=-64). Calculate its coefficients:

>> a=poly([-4,-2,2,4])
a = [ 1  0  -20  0  64 ]
>> a = -64/polyval(a,0) * a
a =
-1   0    20   0    -64
>> a = polyfit([-4,-2,2,4,0],[0,0,0,0,-64],4)
a =
-1   0    20   0    -64


Example 2: On a grid with x,y-Koordinaten we have the following greyvalues of a digital image:

y\x     1     2     3     4
1     98     110     122     136
2     91     112     131     141
3     73     118     145     190
4     43     129     170     230


Which greyvalue do you expect at position x=2,35;y=2,74? Calculate the bicubic interpolation.

Solution:

>> Z=[98,110,122,136;
> 91,112,131,141;
> 73,118,145,190;
> 43,129,170,230];
>> i=1; p=polyfit(1:4,Z(i,:),3); z(i)=polyval(p,2.35);
>> i=2; p=polyfit(1:4,Z(i,:),3); z(i)=polyval(p,2.35);
>> i=3; p=polyfit(1:4,Z(i,:),3); z(i)=polyval(p,2.35);
>> i=4; p=polyfit(1:4,Z(i,:),3); z(i)=polyval(p,2.35);
>> p=polyfit(1:4,z,3); zp=polyval(p,2.74)
zp = 124.82

##### Polynomial functions. Summary table

We have learnt that polynomials may be represented by the vector of their coefficient. Using a symbolic variable x we will now create a symbolic polynomial p. Conversely, we can extract the coefficients from a symbolic polynomial using the function coeff(p, x, exponent). The command allroots(p) returns the zeros.

>> a=[3 2 5 7 4];   % coefficients
>> syms x
>> y=polyval(a,x)   % symbolic polynomial
y = 3*x^4+2*x^3+5*x^2+7*x+4
>> coeff(y,x,3)     % get one coefficient
ans = 2
>> b=coeff(y,x,4:-1:0)  % or all at once
b = [ 3  2  5  7  4 ]
>> allroots(y)      % same as roots(a)
ans = [ 0.363-1.374i  0.363+1.374i
-0.697-0.418i  -0.697+0.418i ]


Up to this point there is little advantage of using symbolic calculations, it is just another way of specifying a problem. The main benefit of symbolic calculations emerges when we are dealing with more than one symbolic variable, or, meaning essentially the same, when our polynomial has nonconstant coefficients. This case can be treated efficiently only with symbolic variables. Notice in the example below how the polynomial y is automatically multiplied through, and brought into a canonical form. In this form the symbolic variables are sorted alphabetically, i.e. z is main variable compared to x. The coefficients can be calculated for each variable separately.

>> syms x,z
>> y=(x-3)*(x-1)*(z-2)*(z+1)
y = (x^2-4*x+3)*z^2+(-x^2+4*x-3)*z+(-2*x^2+8*x-6)
>> coeff(y,x,2)
ans = z^2-z-2
>> coeff(y,z,2)
ans = x^2-4*x+3

##### Roots

The command allroots functions with variable coefficients also, but only, if the polynomials degree in the main variable is smaller than 3, or it is biquadratic. If roots of other variables x are searched, one should use the more general solve(p,x), which will be discussed in more detail later.

>> syms x,z
>> y = x*z^2-3*x*z+(2*x+1);
>> allroots(y)
ans = [ sqrt((1/4*x-1)/x)+3/2  -sqrt((1/4*x-1)/x)+3/2 ]
>> solve(y,x)
ans = -1/(z^2-3*z+2)

##### Squarefree Decomposition

The decomposition of p in linear, quadratic, cubic etc factors is accomplished by sqfr(p). Returned is a vector of factors sorted in ascending order of the exponents.

>> syms x
>> y=(x-1)^3*(x-2)^2*(x-3)*(x-4)
y = x^7-14*x^6+80*x^5-242*x^4+419*x^3-416*x^2+220*x-48
>> z=sqfr(y)
z = [ x^2-7*x+12  x-2  x-1 ]

##### Division and Common Denominator

The division of two polynomials p and q in one polynomial and remainder is calculated using divide(p,q). If the polynomials have more than one variable, an optional variable can be specified, which will be used for division. gcd(p,q) returns the greatest common denominator of two expressions. Both functions also work with numbers as arguments.

>> divide(122344,7623)
ans = [ 16  376 ]
>> divide(2+i,3+2*i)
ans = [ 1/2  1/2 ]
>> syms x,z
>> divide(x^3*z-1,x*z-x,x)
ans = [ x^2*z/(z-1)  -1 ]
>> divide(x^3*z-1,x*z-x,z)
ans = [ x^2  x^3-1 ]
>> gcd(32897397,24552502)
ans = 377
>> gcd(z*x^5-z,x^2-2*x+1)
ans = x-1

##### Real- and Imaginary Part

realpart(expression) and imagpart(expression) is used to decompose complex expressions. Symbolic variables in these expressions are assumed to be real-valued.

>> syms x
>> y=(3+i*x)/(2-i*x)
y = (-x+3i)/(x+2i)
>> realpart(y)
ans = (-x^2+6)/(x^2+4)
>> imagpart(y)
ans = 5*x/(x^2+4)


## Printf output

When you are working in the prompt, the answers are given automatically. If you have chosen to print output by executing a macro file (with the extension ?m?), use the ?printf()? method as in this example:

x=log(sqrt(854));        % natural logarithm

You can output any symbolic variable as well. Here is a more complicated example where we print a value, symbolic equation and a matrix:

syms x,y;
a=2*sqrt(x)*exp(-x^2);
ff=subst(3,x,a);
printf("%f",ff);

ss=trigrat(sqrt(4*y^2+4*x*y-4*y+x^2-2*x+1));
printf("%f\n",ss);

M=[1:3 ; 4:6 ; 7:9];
C=M<4;
printf("%f\n",C);

##### Plot

Data may be graphed using the plot(x,y)-function, x and y being equalsized vectors, which denote the coordinates of the data points to be plotted. A third optional argument plot(x,y,option) specifies plot options like colors and symbols, see exercise 7ff. The graphic may then be decorated (axis, title) and exported as encapsulated-postscript file for inclusion in text documents. With hold (or hold on) the graphic gets locked so that subsequent plot commands use the same window. Repeating hold (or hold off) deletes the graphic. The method

plot(x,y [,option])

plots x versus y using option x and y are equalsized vectors. option is a string, specifying color (one of r,g,b,y,m,c,w,k) and symbol (one of +,*,o,x). Default: blue lines. Logarithmic and semilogarithmic plots are provided with the functions loglog, linlog and loglin. Here are the major plot methods:

Name(Arguments) Function
plot(x,y [,option]) Plot x versus y
loglog(x,y[,option]) logarithmic plot
linlog(x,y[,option]) semilogarithmic plot
loglin(x,y[,option]) semilogarithmic plot
print(name ) write graphic in eps-file

Example 1 let us plot the function: $y=\frac\left\{1\right\}\left\{1+2x^2\right\}$ in the range x=0.01?100 linear and logarithmic. The code is below:

>> x=0.01:0.01:100; y=1./(1+0.5*x.*x); plot(x,y)
>> x=0.01:0.01:100; y=1./(1+0.5*x.*x); loglog(x,y)


Example 1a let us plot the function cos(x):

>> x=-10:0.1:10;
>> plot(x,cos(x));


Example 2: Display of Lissajous-figures: From the vector t=0:0.1:4*pi; create the trigonometric expressions x=sin(0.5*t+1); and y=cos(1.5*t);. The plot x vs. y is called Lissajous-figure. Create different figures by variating the constants 0.5,1,1.5 in the definition. Partial solution:

>> t=0:0.1:4*pi;
>> x=sin(0.5*t+1);
>> y=cos(1.5*t);
>> plot(x,y)


Example 3 Calculate the first 100 elements of the sequences $x_n = \frac\left\{n-1\right\}\left\{n\right\}; x_n=\frac\left\{n+1\right\}\left\{n\right\}; x_n=\frac\left\{n+\left(-1\right)^n\right\}\left\{n\right\}$ Plot xn versus n using the command plot(n, xn). Variate the plotoptions (colors, symbols).

>> n=1:100;
>> x1=(n-1)./n; x2=(n+1)./n; x3=(n+(-1).^n)./n;
>> plot(n,x1)        % Standard: blue, lines
>> hold
Current plot held.
>> plot(n,x2,'r')    % Color:  r,g,b,c,m,y,w.
>> plot(n,x3,'g')


Example 4 Plot the datapoints of the following table using plot and colored symbols. Calculate the linear regression using polyfit, and plot the regression line in the same graph. Add title and labels, and export the graphic to a file suitable for inclusion in a text document.

x  0       1    2    3       4    5     6     7     8     9
y -3.1 -0.7  1.8  4.1     6.2  8.9  11.3     13.5     16     18.3

>> x=0:9;
>> y=[-3.1,-0.7,1.8,4.1,6.2,8.9,11.3,13.5,16,18.3];
>> plot(x,y,"+r")    % Symbol: o,x,+,*
>> hold
Current plot held.
>> plot(x,polyval(polyfit(x,y,1),x)) % Regression
>> xlabel("x-Achse")
>> ylabel("y-Achse")
>> title("Beispielgraph")
>> print("graph.eps") % not Applet or Midlet!

For More Help