以下是一些Octave腳本(至少大概)重現上述MATLAB腳本。您必須按照X腳本手動輸入IVP = 0的解決方案到每個腳本的第2部分,但它們確實起作用。如果任何人有無法像MATLAB解決函數那樣根據X解決Octave求解IVP = 0,我很樂意聽到它。
這一對解決了$ \ 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 \點{X} - 3×= T^2個,$ X (0)= 4 $,$ \ dot {x}(0)= 5 $。
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))
非常感謝所有幫助和建議!這是真正的讚賞!
「幾乎」作品是什麼意思?順便說一句,'收集'[看起來](https://uk.mathworks。com/help/symbolic/collect.html)來收集係數;我在符號包中發現了一個'coeffs'函數,它似乎是等價的(儘管如此,似乎在這種情況下「factor」似乎有效)。 –
根據我運行此代碼時得到的錯誤('Python異常:AttributeError:MutableDenseMatrix沒有屬性is_Relational'),這可能是相關的:http://stackoverflow.com/questions/42802588/solving-two-non- linear-equations-in-octave –
非常感謝您的回覆! coeffs功能的確出現在我感興趣的地方!一個簡單的矩陣乘[1; X]再現MATLAB的收集。我有一些複製了上述MATLAB腳本的腳本(大約至少),但我會在下面的「答案」中發佈這些腳本。非常感謝回覆! –