Rewrite expression in terms of another function
Rewrite Between Trigonometric and Exponential Functions
Rewrite any trigonometric function in terms of the exponential function by specifying the target
syms x sin2exp = rewrite(sin(x),"exp")
tan2exp = rewrite(tan(x),"exp")
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")
exp2tan = rewrite(-(exp(x*2i)*1i - 1i)/(exp(x*2i) + 1),"tan")
exp2tan to the expected form by using
exp2tan = simplify(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.
tan(x) in terms of the sine function by specifying the target
syms x tan2sin = rewrite(tan(x),"sin")
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.
acot(x) in terms of the
syms x acos2log = rewrite(acos(x),"log")
acot2log = rewrite(acot(x),"log")
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
syms x matrix = [sin(x) cos(x); sinh(x) cosh(x)]; R = rewrite(matrix,"exp")
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")
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
rewrite can rewrite
syms x R1 = rewrite(sin(x),"cos")
R2 = rewrite(sin(x)^2,"cos")
Find the root of a polynomial by using
root. The result is a column vector in terms of the
root function with
k = 1,
5 for the
kth root of the polynomial.
syms x sols = root(x^5 - x^4 - 1,x)
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")
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)
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
Find the indefinite integral of a polynomial fraction. The result is in terms of the
root functions as represented by the and symbols.
syms x F = int(1/(x^3 + x - 1),x)
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")
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||Rewrite or Replace These Functions||In Terms of These Functions|
|All trigonometric and hyperbolic functions including inverse functions|
|All inverse trigonometric and hyperbolic functions|
|Target trigonometric function|
|Target hyperbolic function|
|Target inverse trigonometric function|
|Target inverse hyperbolic function|
|Arithmetic operations such as ^, *, /, +, and –|
|Arithmetic operations such as + and –|