You are a guest. Restricted access. Read more

# Transformations

Symbolic transformations can be done by specifying “syms x” keyword in front of your code. It can be commented out if x no need to be a symbolic value.

It is possible to define variables whose value is a function. In this case the function's name must be preceded by the character

$ to suppress evaluation. These variables can be used like the function they stand for. For example, who dislikes the built-in function realpart(x) name can shorten: real=$realpart
printf('%f\n',real)
ans=real(24+3i)
printf('%f\n',ans)

## Substitution

Parts of an expression may be replaced by other expressions using subst(a,b,c): a is substituted for b in c. This is a powerful function with many uses.

First, it may be used to insert numbers for variables, in the example $3$ for $x$ in der formula .

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

Second, one can replace a symbolic variable by a complex term. The expression is automatically updated to the canonical format. In the following example is inserted for x in .

syms x,z
p=x^3+2*x^2+x+7;
a=subst(z^3+2,x,p)
printf('%f',a)

Finally, the term b itself may be a complex expression (in the example $z^2+1$). jMathLab then tries to identify this expression in c (example: $\frac{z\cdot x^3}{\sqrt{z^2+1}})$. This is accomplished by solving the equation a = b for the symbolic variable in b (example: $z$), and inserting the solution in c. This does not always succeed, or there may be several solutions, which are returned as a vector.

syms x,y,z
c=x^3*z/sqrt(z^2+1);
d=subst(y,z^2+1,c)
printf('%f\n',d)
d=trigrat(d)
printf('%f',d)

## Simplifying and Collecting Expressions

The function trigrat(expression) applies a series of algorithms to expression.

• All numbers are transformed to exact format.
• Trigonometric functions are expanded to complex exponentials.
• Addition theorems for the exponentials are applied.
• Square roots are calculated and collected.
• Complex exponentials are back transformed to trigonometric functions.

It is often required to apply float(expression) to the final result.

syms x
a=trigrat(sin(x)^2+cos(x)^2)
printf('1)= %f\n',a)
b=sin(x)^2+sin(x+2*pi/3)^2+sin(x+4*pi/3)^2;
a=trigrat(b)
printf('2)= %f\n',a)
a=trigrat(i/2*log(x+i*pi))
printf('3) %f\n',a)

And with 2 variables, x,y

syms x,y
a=trigrat(sin((x+y)/2)*cos((x-y)/2))
printf('%f\n',a)
a=trigrat(sqrt(4*y^2+4*x*y-4*y+x^2-2*x+1))
printf('%f\n',a)

trigexpand(expression) expands trigonometric expressions to complex exponentials. It is the first step of the function trigrat above.

syms x
a=trigexp(i*tan(i*x))
printf('%f\n',a)
a=trigexp(atan(1-x^2))
printf('%f',a)