2017-05-05 51 views
1

我有一個MATLAB腳本,它使用拉普拉斯變換解決了非均勻的一階線性IVP。 (在這個例子中,腳本設置爲解決IVP enter image description hereenter image description here在八度中,是否有辦法爲一個變量求解兩個變量中的方程

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)) 

如果我取代「因素」爲「收集」,劇本上八度幾乎作品與象徵打包鏈接到SymPy,除了「解決」命令https://www.mathworks.com/help/symbolic/solve.html

是否有任何八度(或SymPy,如果這將起到解決方法的作用)命令將作爲一個MATLAB符號工具箱「解決」命令,所以我可以通過帶有腳本的拉普拉斯變換來解決IVP,不得不手動解決X,然後使用「ilaplace」?

在此先感謝您提供的任何幫助。

+0

「幾乎」作品是什麼意思?順便說一句,'收集'[看起來](https://uk.mathworks。com/help/symbolic/collect.html)來收集係數;我在符號包中發現了一個'coeffs'函數,它似乎是等價的(儘管如此,似乎在這種情況下「factor」似乎有效)。 –

+0

根據我運行此代碼時得到的錯誤('Python異常:AttributeError:MutableDenseMatrix沒有屬性is_Relational'),這可能是相關的:http://stackoverflow.com/questions/42802588/solving-two-non- linear-equations-in-octave –

+0

非常感謝您的回覆! coeffs功能的確出現在我感興趣的地方!一個簡單的矩陣乘[1; X]再現MATLAB的收集。我有一些複製了上述MATLAB腳本的腳本(大約至少),但我會在下面的「答案」中發佈這些腳本。非常感謝回覆! –

回答

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,爲您提供巨大的幫助!

1

以下是一些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)) 

非常感謝所有幫助和建議!這是真正的讚賞!

相關問題