Matlab-“无法提取微分变量来求解"溶解 [英] Matlab - "Could not extract differential variables to solve for" in dsolve
问题描述
我正在尝试使用dsolve
在MATLAB中将RLC电路作为符号微分方程进行求解,该方程为
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)
具有初始条件
Q(0) = 0
Q'(0) = 0
和
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);
我会得到
Q =
5 sin(2 t) 5 t cos(2 t)
---------- - ------------
4 2
这是正确的.
出于纯粹深奥的原因,我尝试直接使用符号方程式对其进行计算,因为dsolve
的文档指出可以做到这一点.
For purely esoteric reasons I tried computing it directly using symbolic equations, as the documentation on dsolve
stated it could be done.
所以从
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)
我收到
eqn =
10*sin(2*t) == 4*Q(t) + diff(Q(t), t, t)
这是正确的.如果我现在将其输入dsolve
,请使用
Which is correct. If I now feed it into dsolve
though, using
Q = dsolve(eqn, ...
Q(t) == 0, ...
diff(Q(t),t) == 0);
Matlab引发错误
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.
那是为什么?
推荐答案
您似乎正在使用 symfun
错误. Q(t)
是所谓的任意符号功能(help sym/diff
使用术语抽象"),即没有定义的功能.您函数的名称为Q
(将其视为函数句柄),并由抽象公式Q(t)
表示,这仅表示它是t
的函数.如果要采用抽象函数的派生形式,请传入函数名称-在您的情况下为Q
(
It looks like you're using sym/diff
and symfun
s 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)
返回
Q =
(5*sin(2*t))/4 - (5*t*cos(2*t))/2
您还忘记了在第二种情况下将初始条件评估为零,因此我也对此进行了修复.顺便说一下,在当前版本的Matlab中,您应该使用纯符号形式进行符号数学运算(而不是字符串).
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).
这篇关于Matlab-“无法提取微分变量来求解"溶解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!