rewrite
Rewrite expression in terms of another function
Syntax
Description
Examples
Rewrite Between Trigonometric and Exponential Functions
Rewrite any trigonometric function in terms of the exponential function by specifying the target "exp"
.
syms x sin2exp = rewrite(sin(x),"exp")
sin2exp =
tan2exp = rewrite(tan(x),"exp")
tan2exp =
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 =
exp2tan = rewrite(-(exp(x*2i)*1i - 1i)/(exp(x*2i) + 1),"tan")
exp2tan =
Simplify exp2tan
to the expected form by using simplify
.
exp2tan = simplify(exp2tan)
exp2tan =
Rewrite Between Trigonometric Functions
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 =
Rewrite Between Inverse Trigonometric and Logarithm Functions
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 =
acot2log = rewrite(acot(x),"log")
acot2log =
Similarly, rewrite the logarithm function in terms of any inverse trigonometric function by specifying the inverse trigonometric function as the target.
Rewrite Elements of Matrix
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 =
Rewrite Between Sine and Cosine Functions
Rewrite the cosine function in terms of the sine function. Here, the rewrite
function rewrites the cosine function using the identity , which is valid for any .
syms x R = rewrite(cos(x),"sin")
R =
rewrite
does not rewrite sin(x)
as either or 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 =
R2 = rewrite(sin(x)^2,"cos")
R2 =
Rewrite root
Function
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 k
th root of the polynomial.
syms x
sols = root(x^5 - x^4 - 1,x)
sols =
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 =
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 =
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 24.2. % 05-Sep-2024 19:11:47 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
Rewrite symsum
and root
Functions
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 symbols.
syms x
F = int(1/(x^3 + x - 1),x)
F =
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 =
Input Arguments
expr
— Input to rewrite
symbolic number | symbolic variable | symbolic expression | symbolic function | symbolic vector | symbolic matrix | symbolic multidimensional array
Input to rewrite or replace, specified as a symbolic number, variable, expression, function, vector, matrix, or multidimensional array.
target
— Target function or function to expand
string scalar | character vector
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.
Target | Rewrite or Replace These Functions | In Terms of These Functions |
---|---|---|
"exp" | All trigonometric and hyperbolic functions including inverse functions | exp , log |
"log" | All inverse trigonometric and hyperbolic functions | log |
"sincos" | tan , cot ,
exp , sinh ,
cosh , tanh ,
coth | sin , cos |
"sin" , "cos" ,
"tan" , or
"cot" | sin , cos ,
exp , tan ,
cot , sinh ,
cosh , tanh ,
coth except the target | Target trigonometric function |
"sinhcosh" | tan , cot ,
exp , sin ,
cos , tanh ,
coth | sinh , cosh |
"sinh" , "cosh" ,
"tanh" ,
"coth" | tan , cot ,
exp , sin ,
cos , sinh ,
cosh , tanh ,
coth except the target | Target hyperbolic function |
"asin" , "acos" ,
"atan" ,
"acot" | log , and all inverse trigonometric and
inverse hyperbolic functions | Target inverse trigonometric function |
"asinh" , "acosh" ,
"atanh" ,
"acoth" | log , and all inverse trigonometric and
inverse hyperbolic functions | Target inverse hyperbolic function |
"sqrt" | abs(x + 1i*y) | sqrt(x^2 + y^2) |
"heaviside" | sign ,
triangularPulse ,
rectangularPulse | heaviside |
"piecewise" | abs , heaviside ,
sign ,
triangularPulse ,
rectangularPulse | piecewise |
"expandroot" | root | Arithmetic operations such as ^, *, /, +, and – |
"expandsum" | symsum | Arithmetic operations such as + and – |
Tips
rewrite
replaces symbolic function calls inexpr
with another function as specified bytarget
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 R2012aR2023a: Expand root
and symsum
functions
You can rewrite or expand symbolic expressions that use the root
and symsum
functions in terms of
arithmetic operations. For examples, see Rewrite root Function and Rewrite symsum and root Functions.
See Also
Functions
collect
|combine
|expand
|factor
|horner
|numden
|simplify
|simplifyFraction
|unitConvert
Live Editor Tasks
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)