# How to get the real part of a complex function with some real coefficients?

I want to get the real part of the function n3[t] (see below) which is the solution to a differential equation.

Solve the differential equation. (Suppose m, k, f, and t are real)

Clear["Global*"]

eqns = {m*n1''[t] + k*n1[t] - k*n2[t] == f*E^(I*w*t),
m*n2''[t] - k*n1[t] + 2*k*n2[t] - k*n3[t] == 0,
m*n3''[t] - k*n2[t] + k*n3[t] == 0, n1[0] == 0, n2[0] == 0,
n3[0] == 0, n1'[0] == 0, n2'[0] == 0, n3'[0] == 0};

(sol = DSolve[eqns, {n1, n2, n3}, t]) // Short[#, 5] &


Simplify the solution

solt = {n1[t], n2[t], n3[t]} /. sol // FullSimplify


I got

Then I tried to get the real part of n3[t] with ComplexExpand[], but I failed. It seems that m and k are also regarded as complex numbers. (The output is actually very long, I omit the rest of it)

I failed to get the real part of n3[t] directly with Re[] either.

First, try this:

expr = ExpandAll[solt][[1]];


And then the following:

Refine[ComplexExpand[Re[expr]], Assumptions -> {f > 0, k > 0, m > 0, t > 0, w > 0}] // Simplify


• Thank you! I ran your codes and it works. But could you explain why your codes succeed by using ComplexExpand and Re together, while I failed when I used either ComplexExpand or Re alone? Commented Jan 21, 2023 at 23:00
• I also tried ComplexExpand[Re[expr]] // Simplify but it took my computer too much time to run this code. But why does Refine make such a big difference that it doesn't take the computer a lot of time to run Refine[ComplexExpand[Re[expr]], Assumptions -> {f > 0, k > 0, m > 0, t > 0, w > 0}] // Simplify`? Commented Jan 21, 2023 at 23:05
• Besides, I am sorry for my late reply. I was really busy with other things in the past few weeks. Commented Jan 21, 2023 at 23:07
• Sorry, I forgot to accept your answer half a year ago because I didn't know how to use stack exchange at that time. Commented Jul 15, 2023 at 1:22