Question

I am trying to solve a RLC circuit as a symbolic differential equation in MATLAB using dsolve, the equation being

U(t) = L*Q''(t) + R*Q'(t) + (1/C)*Q(t)

with the initial conditions

Q(0)  = 0
Q'(0) = 0

and

U(t) = 10*sin(2*t)
L = 1
R = 0
C = 1/4

While this works ...

When I implement it explicitly (and using strings) as

Q = dsolve('D2Q(t) + 4*Q(t) = 10*sin(2*t)', 'DQ(0)=0, Q(0)=0');
Q = simplify(Q);

I'll get

Q =

  5 sin(2 t)   5 t cos(2 t) 
  ---------- - ------------ 
      4             2

which is correct.

... this does not.

For purely esoteric reasons I tried computing it directly using symbolic equations, as the documentation on dsolve stated it could be done.

So starting with

syms L R C t Q(t)
U = sym('10')*sin(sym('2')*t)
DEQ = L*diff(Q(t),t,2) + R*diff(Q(t),t) + (1/C)*Q(t)
DEQ = subs(DEQ, [L R C], [sym('1'), sym('0'), sym('1/4')])
eqn = (U == DEQ)

I receive

eqn =
10*sin(2*t) == 4*Q(t) + diff(Q(t), t, t)

Which is correct. If I now feed it into dsolve though, using

Q = dsolve(eqn, ...
    Q(t) == 0, ...
    diff(Q(t),t) == 0);

Matlab throws the error

Error using symengine (line 58)
Could not extract differential variables to solve for. Use 'solve' or 'vpasolve'
to compute the solutions of non-differential equations.

Why is that?

Was it helpful?

Solution

It looks like you're using sym/diff and symfuns incorrectly. Q(t) is what is referred to as an arbitrary (help sym/diff uses the term "abstract" instead) symbolic function, i.e., a function with no definition. Your function's name is Q (think of it as a function handle) and it is represented by the abstract formula Q(t), which just means that it's a function of t. When you want to take the derivative of an abstract function, pass in the name of the function - in your case, Q (the online documentation makes this slightly clearer, but not really). When you want evaluate the function use the formula, e.g., Q(0), the output of which is a sym rather than a symfun.

Here is how I might write the code for your second case:

syms L R C t Q(t)
U = 10*sin(2*t); % No need to wrap integer or exactly-represenable values in sym
dQ = diff(Q,t);
d2Q = diff(dQ,t);
DEQ = L*d2Q + R*dQ + Q/C;
DEQ = subs(DEQ, {L, R, C}, {1, 0, 1/4});
eqn = (U == DEQ);
Q = dsolve(eqn, Q(0) == 0, dQ(0) == 0);
Q = simplify(Q)

which returns

Q =

(5*sin(2*t))/4 - (5*t*cos(2*t))/2

You also forgot to evaluate your initial conditions at zero in the second case so I fixed that too. By the way, in current versions of Matlab you should be using the pure symbolic form for symbolic math (as opposed to strings).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top