I've got a following question. I've got function f(t) = C3*exp(t*x*1i) + C4*exp(-t*x*1i) as a solution of a differential equation (as syms). But I need this solution as a real function (C3*cos + C4*sin). How can I do it? And how can I get real and imaginary parts of this function? Is there a function in matlab allowing me to do it?
You can use rewrite
to rewrite the expression in terms of cosines and sines, then collect
to collect coefficients in terms of i
, giving you your real and imaginary terms:
f = C3*exp(t*x*1i) + C4*exp(-t*x*1i);
g = collect(rewrite(f, 'sincos'), i)
g =
(C3*sin(t*x) - C4*sin(t*x))*1i + C3*cos(t*x) + C4*cos(t*x)
You can see from the above that the imaginary term is zero if C3
is equal to C4
.
You can rewrite the expression/function in terms of sine and cosine using rewrite
. Still you cannot apply the real
and imag
functions to get the respective parts in as nicer form as you get in case of non-symbolic computations. The trick to get the real and imaginary parts in a complex expression is to substitute i
with 0
to get the real part and then subtract the real part from the original expression to get the imaginary part. Use simplify
for the surety.
An example:
syms C3 C4 t x
f(t) = C3*exp(t*x*1i) + C4*exp(-t*x*1i);
fsincos = rewrite(f, 'sincos');
realf = simplify(subs(fsincos, i,0));
imagf = simplify(fsincos-realf);
%or you can use the collect function to avoid simplify
>> fsincos
fsincos(t) =
C3*(cos(t*x) + sin(t*x)*1i) + C4*(cos(t*x) - sin(t*x)*1i)
>> realf
realf(t) =
cos(t*x)*(C3 + C4)
>> imagf
imagf(t) =
sin(t*x)*(C3*1i - C4*1i)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.