Main Content

rewrite

Rewrite expression in terms of another function

Description

example

R = rewrite(expr,target) rewrites the symbolic expression expr in terms of another function as specified by target. The rewritten expression is mathematically equivalent to the original expression. If expr is a vector or matrix, rewrite acts element-wise on expr.

Examples

collapse all

Rewrite any trigonometric function in terms of the exponential function by specifying the target "exp".

syms x
sin2exp = rewrite(sin(x),"exp")
sin2exp = 

e-xii2-exii2

tan2exp = rewrite(tan(x),"exp")
tan2exp = 

-e2xii-ie2xi+1

Rewrite the exponential function in terms of any trigonometric function by specifying the trigonometric function as the target. For a full list of target options, see target.

syms x
exp2sin = rewrite(exp(x),"sin")
exp2sin = 

-2sin(xi2)2-sin(xi)i+1

exp2tan = rewrite(-(exp(x*2i)*1i - 1i)/(exp(x*2i) + 1),"tan")
exp2tan = 

-tan(x)-iitan(x)+i+itan(x)-itan(x)+i-1

Simplify exp2tan to the expected form by using simplify.

exp2tan = simplify(exp2tan)
exp2tan = tan(x)

Rewrite any trigonometric function in terms of any other trigonometric function by specifying the target. For a full list of target options, see target.

Rewrite tan(x) in terms of the sine function by specifying the target "sin".

syms x
tan2sin = rewrite(tan(x),"sin")
tan2sin = 

-sin(x)2sin(x2)2-1

Rewrite any inverse trigonometric function in terms of the logarithm function by specifying the target "log". For a full list of target options, see target.

Rewrite acos(x) and acot(x) in terms of the log function.

syms x
acos2log = rewrite(acos(x),"log")
acos2log = -log(x+1-x2i)i
acot2log = rewrite(acot(x),"log")
acot2log = 

log(1-ix)i2-log(1+ix)i2

Similarly, rewrite the logarithm function in terms of any inverse trigonometric function by specifying the inverse trigonometric function as the target.

Rewrite each element of a matrix by calling rewrite on the matrix.

Rewrite all elements of a matrix in terms of the exp function.

syms x
matrix = [sin(x) cos(x); sinh(x) cosh(x)];
R = rewrite(matrix,"exp")
R = 

(e-xii2-exii2e-xi2+exi2ex2-e-x2e-x2+ex2)

Rewrite the cosine function in terms of the sine function. Here, the rewrite function rewrites the cosine function using the identity cos(x)=1-2sin(x2)2, which is valid for any x.

syms x
R = rewrite(cos(x),"sin")
R = 

1-2sin(x2)2

rewrite does not rewrite sin(x) as either -1-cos2(x) or 1-cos2(x) because these expressions are not valid for all x. However, using the square of these expressions to represent sin(x)^2 is valid for all x. Thus, rewrite can rewrite sin(x)^2.

syms x
R1 = rewrite(sin(x),"cos")
R1 = sin(x)
R2 = rewrite(sin(x)^2,"cos")
R2 = 1-cos(x)2

Since R2023a

Find the root of a polynomial by using root. The result is a column vector in terms of the root function with k = 1, 2, 3, 4, or 5 for the kth root of the polynomial.

syms x
sols = root(x^5 - x^4 - 1,x)
sols = 

(root(x5-x4-1,x,1)root(x5-x4-1,x,2)root(x5-x4-1,x,3)root(x5-x4-1,x,4)root(x5-x4-1,x,5))

Expand the root function in sols by using rewrite with the "expandroot" option. The result is in terms of arithmetic operations such as ^, *, /, +, and – that operate on exact symbolic numbers. Because the expanded result can involve many terms that operate arithmetically, numerical approximation of this result can be inaccurate (due to accumulation of round-off errors).

R = rewrite(sols,"expandroot")
R = 

(12-3i212+3i213σ1+σ1-16σ1-σ12-313σ1-σ1i2-16σ1-σ12+313σ1-σ1i2)where  σ1=23108108+121/3

As an alternative, you can numerically approximate sols directly by using vpa to return variable-precision symbolic numbers. The resulting numeric values have the default 32 significant digits, which are more accurate.

solsVpa = vpa(sols)
solsVpa = 

(-0.66235897862237301298045442723905-0.56227951206230124389918214490937i-0.66235897862237301298045442723905+0.56227951206230124389918214490937i0.5-0.86602540378443864676372317075294i0.5+0.86602540378443864676372317075294i1.3247179572447460259609088544781)

To use sols without Symbolic Math Toolbox™, you can generate code and convert sols to a MATLAB® function by using matlabFunction. The generated file uses the roots function that operates on the numeric double data type.

matlabFunction(sols,"File","myfile");
type myfile
function sols = myfile
%MYFILE
%    SOLS = MYFILE

%    This function was generated by the Symbolic Math Toolbox version 23.2.
%    19-Aug-2023 12:32:56

t0 = roots([1.0,-1.0,0.0,0.0,0.0,-1.0]);
t2 = t0(1);
t0 = roots([1.0,-1.0,0.0,0.0,0.0,-1.0]);
t3 = t0(2);
t0 = roots([1.0,-1.0,0.0,0.0,0.0,-1.0]);
t4 = t0(3);
t0 = roots([1.0,-1.0,0.0,0.0,0.0,-1.0]);
t5 = t0(4);
t0 = roots([1.0,-1.0,0.0,0.0,0.0,-1.0]);
t6 = t0(5);
sols = [t2;t3;t4;t5;t6];
end

Since R2023a

Find the indefinite integral of a polynomial fraction. The result is in terms of the symsum and root functions as represented by the and root symbols.

syms x
F = int(1/(x^3 + x - 1),x)
F = 

k=13log(root(z3-3z31-131,z,k)3x+root(z3-3z31-131,z,k)6x-9)root(z3-3z31-131,z,k)

Rewrite the result in terms of arithmetic operations. Because the symbolic summation is the outermost operation, first apply the rewrite function with the "expandsum" option to expand symsum. Then, apply rewrite again with the "expandroot" option to rewrite the root function. The resulting symbolic expression is in terms of arithmetic operations such as ^, *, /, +, and –.

R = rewrite(rewrite(F,"expandsum"),"expandroot")
R = 

log(3x+6x-9σ3σ3)σ3-log(-3x-6x-9σ2σ2)σ2-log(-3x-6x-9σ1σ1)σ1where  σ1=162σ4+σ42-3131σ4-σ4i2  σ2=162σ4+σ42+3131σ4-σ4i2  σ3=131σ4+σ4  σ4=27119164119164+1621/3

Input Arguments

collapse all

Input to rewrite or replace, specified as a symbolic number, variable, expression, function, vector, matrix, or multidimensional array.

Target function or function to expand, specified as a string scalar or character vector. This table summarizes the rewriting rules for all allowed target options.

TargetRewrite or Replace These FunctionsIn Terms of These Functions
"exp"All trigonometric and hyperbolic functions including inverse functionsexp, log
"log"All inverse trigonometric and hyperbolic functionslog
"sincos"tan, cot, exp, sinh, cosh, tanh, cothsin, cos
"sin", "cos", "tan", or "cot"sin, cos, exp, tan, cot, sinh, cosh, tanh, coth except the targetTarget trigonometric function
"sinhcosh"tan, cot, exp, sin, cos, tanh, cothsinh, cosh
"sinh", "cosh", "tanh", "coth"tan, cot, exp, sin, cos, sinh, cosh, tanh, coth except the targetTarget hyperbolic function
"asin", "acos", "atan", "acot"log, and all inverse trigonometric and inverse hyperbolic functionsTarget inverse trigonometric function
"asinh", "acosh", "atanh", "acoth"log, and all inverse trigonometric and inverse hyperbolic functionsTarget inverse hyperbolic function
"sqrt"abs(x + 1i*y)sqrt(x^2 + y^2)
"heaviside"sign, triangularPulse, rectangularPulseheaviside
"piecewise"abs, heaviside, sign, triangularPulse, rectangularPulsepiecewise
"expandroot"rootArithmetic operations such as ^, *, /, +, and –
"expandsum"symsumArithmetic operations such as + and –

Tips

  • rewrite replaces symbolic function calls in expr with another function as specified by target only if the replacement is mathematically valid. Otherwise, it keeps the original function calls. For example, see Rewrite Between Sine and Cosine Functions.

Version History

Introduced in R2012a

expand all