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') tan2exp = rewrite(tan(x), 'exp')
sin2exp = (exp(-x*1i)*1i)/2 - (exp(x*1i)*1i)/2 tan2exp = -(exp(x*2i)*1i - 1i)/(exp(x*2i) + 1)
Rewrite the exponential function in terms of any trigonometric function by
specifying the trigonometric function as the target. For a full list of
targets, see target
.
syms x exp2sin = rewrite(exp(x), 'sin') exp2tan = rewrite(-(exp(x*2i)*1i - 1i)/(exp(x*2i) + 1), 'tan')
exp2sin = 1 - 2*sin((x*1i)/2)^2 - sin(x*1i)*1i exp2tan = -(((tan(x) - 1i)*1i)/(tan(x) + 1i) + 1i)/... ((tan(x) - 1i)/(tan(x) + 1i) - 1)
Simplify exp2tan
into the expected form by using
simplify
.
exp2tan = simplify(exp2tan)
exp2tan = tan(x)
Rewrite Between Trigonometric Functions
Rewrite any trigonometric function in terms
of any other trigonometric function by specifying the target. For
a full list of targets, 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)/(2*sin(x/2)^2 - 1)
Rewrite Between Hyperbolic Functions and Trigonometric Functions
Rewrite any hyperbolic function in terms of
any trigonometric function by specifying the trigonometric function
as the target. For a full list of targets, see target
.
Rewrite tanh(x)
in terms of the sine function by
specifying the target 'sin'
.
syms x tanh2sin = rewrite(tanh(x), 'sin')
tanh2sin = (sin(x*1i)*1i)/(2*sin((x*1i)/2)^2 - 1)
Similarly, rewrite trigonometric functions in terms of hyperbolic functions by specifying the hyperbolic function as the target.
Rewrite Between Inverse Trigonometric Functions and Logarithm Function
Rewrite any inverse trigonometric function
in terms of the logarithm function by specifying the target 'log'
.
For a full list of targets, see target
.
Rewrite acos(x)
and acot(x)
in terms
of the log
function.
syms x acos2log = rewrite(acos(x), 'log') acot2log = rewrite(acot(x), 'log')
acos2log = -log(x + (1 - x^2)^(1/2)*1i)*1i acot2log = (log(1 - 1i/x)*1i)/2 - (log(1i/x + 1)*1i)/2
Similarly, rewrite the logarithm function in terms of an 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)]; rewrite(matrix, 'exp')
ans = [ (exp(-x*1i)*1i)/2 - (exp(x*1i)*1i)/2, exp(-x*1i)/2 + exp(x*1i)/2] [ exp(x)/2 - exp(-x)/2, exp(-x)/2 + exp(x)/2]
Rewrite Between Sine and Cosine Functions
Rewrite the cosine function in terms of the
sine function. Here, rewrite
replaces the cosine
function using the identity cos(2*x) = 1 – 2*sin(x)^2
which
is valid for any x
.
syms x rewrite(cos(x),'sin')
ans = 1 - 2*sin(x/2)^2
rewrite
does not replace sin(x)
with
either or because these expressions are not valid for all
x
. However, using the square of these expressions to
replace sin(x)^2
is valid for all x
.
Thus, rewrite
replaces
sin(x)^2
.
syms x rewrite(sin(x),'cos') rewrite(sin(x)^2,'cos')
ans = sin(x) ans = 1 - cos(x)^2
Input Arguments
expr
— Input to rewrite
symbolic number | symbolic variable | symbolic expression | symbolic function | symbolic vector | symbolic matrix | symbolic multidimensional array
Input to rewrite, specified as a symbolic number, variable, expression, function, vector, matrix, or multidimensional array.
target
— Target function
character vector
Target function, specified as a character vector. This table summarizes the rewriting rules for all allowed targets.
Target | Rewrites 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 |
Tips
rewrite
replaces symbolic function calls inexpr
with the target function only if the replacement is mathematically valid. Otherwise, it keeps the original function calls.
Version History
Introduced in R2012a
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)