我有一个MATLAB脚本,使用拉普拉斯变换解决了不均匀的一阶线性IVP。 (对于此示例,脚本设置为解决IVP , 。)
syms x(t) s X;
a0 = -3;
x0 = 4;
rhs = t^2;
lhs = diff(x,t) + a0*x;
ode = lhs - rhs
Lx = X;
LDx = s*X - x0;
LHS = LDx + a0*Lx;
RHS = laplace(rhs,t,s);
IVP = LHS - RHS;
IVP = collect(IVP,X);
X = solve(IVP, X);
X = partfrac(X);
sol = ilaplace(X, s, t)
check1 = diff(sol,t) - 3*sol
check2 = vpa(subs(sol, t, 0))
如果我替换"因素" for" collect",脚本几乎在Octave上工作,符号包链接到SymPy,除了"解决"命令https://www.mathworks.com/help/symbolic/solve.html。
是否有任何Octave(或SymPy,如果它可以作为一种变通方法)命令,它将作为MATLAB符号工具箱使用"解决"命令所以我可以用拉普拉斯变换用脚本解决IVP,所以我不必手动解决X,然后使用" ilaplace"?
提前感谢您提供的任何帮助。
答案 0 :(得分:1)
以下是一些Octave脚本(大约至少)重现上述MATLAB脚本。您必须根据X将IVP = 0的解决方案手动输入到每个脚本的第2部分,但它们确实起作用。如果有人有任何方法让Octave在X方面解决IVP = 0,就像MATLAB求解函数一样,我很乐意听到它。
这对解决了$ \ dot {x} - 3x = t ^ 2 $,$ x(0)= 4 $。
第1部分:
syms x(t) s X;
a0 = -3;
x0 = 4;
rhs = t^2;
lhs = diff(x,t) + a0*x;
ode = lhs - rhs
Lx = X;
LDx = s*X - x0;
LHS = LDx + a0*Lx;
RHS = laplace(rhs,t,s); % The t and s in laplace aren't necessary, as they are default
IVP = LHS - RHS;
coeff = coeffs(IVP,X);
IVP = coeff*[1;X]
第2部分:
syms x(t) s X;
X = -1*((-4*s^3-2)/s^3)/(s-3)
X = partfrac(X);
sol = ilaplace(X, s, t)
check1 = diff(sol,t) - 3*sol
check2 = vpa(subs(sol, t, 0))
此对解决$ \ ddot {x} - 2 \ dot {x} - 3x = t ^ 2 $,$ x(0)= 4 $,$ \ dot {x}(0)= 5 $。< / p>
第1部分:
syms x(t) s X;
a1 =-2;
a0 = -3;
x0 = 4;
xdot0 = 5;
rhs = t^2;
Dx = diff(x,t);
D2x = diff(x,t,2);
lhs = D2x + a1*Dx + a0*x;
ode = lhs - rhs
Lx = X ;
LDx = s*X - x0;
LD2x = s^2*X - x0*s - xdot0;
LHS = LD2x + a1*LDx + a0*Lx;
RHS = laplace(rhs,t,s); % The t and s in laplace aren't necessary, as they are default
IVP = LHS - RHS;
coeff = coeffs(IVP,X);
IVP = coeff*[1;X]
第2部分:
syms x(t) s X;
a1 = -2;
a0 = -3;
X = -1*((-4*s^4 + 3*s^3 - 2)/s^3)/(s^2 - 2*s - 3)
X = partfrac(X);
sol = ilaplace(X, s, t)
Dsol = diff(sol,t);
D2sol = diff(sol,t,2);
check1 = D2sol + a1*Dsol + a0*sol
check2 = vpa(subs(sol, t, 0))
check3 = vpa(subs(Dsol, t, 0))
非常感谢所有的帮助和建议!真的很感激!
答案 1 :(得分:0)
好的,所以,我的一个学生解决了这个问题(我将在本周晚些时候与他联系,看他是否希望公开承认他的解决方案)。
您只需要将coeff*[1;X]
的结果定义为等于0的等式集,比如IVPEQ = coeff*[1;X] == 0
,然后在此等式solve
上使用符号包命令X = solve(IVPEQ, X)
}。
以下是我之前的一阶IVP求解器版本与我学生的修改
syms x(t) s X;
a0 = -3;
x0 = 4;
rhs = t^2;
lhs = diff(x,t) + a0*x;
ode = lhs - rhs
Lx = X;
LDx = s*X - x0;
LHS = LDx + a0*Lx;
RHS = laplace(rhs,t,s); % The t and s in laplace aren't necessary, as they are default
IVP = LHS - RHS;
coeff = coeffs(IVP,X);
IVPEQ = coeff*[1;X] == 0;
X = solve(IVPEQ,X);
X = partfrac(X);
sol = ilaplace(X, s, t)
Dsol = diff(sol,t);
check1 = Dsol + a0*sol
check2 = vpa(subs(sol, t, 0))
这里是学生修改的二阶IVP求解器
syms x(t) s X;
a1 =-2;
a0 = -3;
x0 = 4;
xdot0 = 5;
rhs = t^2;
Dx = diff(x,t);
D2x = diff(x,t,2);
lhs = D2x + a1*Dx + a0*x;
ode = lhs - rhs
Lx = X ;
LDx = s*X - x0;
LD2x = s^2*X - x0*s - xdot0;
LHS = LD2x + a1*LDx + a0*Lx;
RHS = laplace(rhs,t,s); % The t and s in laplace aren't necessary, as they are default
IVP = LHS - RHS;
coeff = coeffs(IVP,X);
IVPEQ = coeff*[1;X] == 0;
X = solve(IVPEQ,X);
X = partfrac(X);
sol = ilaplace(X, s, t)
Dsol = diff(sol,t);
D2sol = diff(sol,t,2);
check1 = D2sol + a1*Dsol + a0*sol
check2 = vpa(subs(sol, t, 0))
check3 = vpa(subs(Dsol, t, 0))
再次感谢@Tasos_Papastylianou,感谢您的大力帮助!