Solving Coupled Second Order ODE by ode45
显示 更早的评论
Hi, I'm trying to solve the 2nd order ODE's
S1'' = -k * sqrt( S1'^2 + S2'^2 ) * S1',
S2'' = -k * sqrt( S1'^2 + S2'^2 ) * S2' - g.
Which I have attempted to do by writing it as four first order ODE's
V1' = -k * sqrt( V1^2 + V2^2 ) * V1,
V2' = -k * sqrt( V1^2 + V2^2 ) * V2 - g,
S1' = V1,
S2' = V2.
To solve this I've tried to use ODE45. Originally I solved just V1 and V2 which worked fine, by making a function
function vp = Func(t,v)
k = 1
g = 2;
vp = diag( [ - k .* sqrt( v .^ 2 + v(2) .^ 2 ) .* v, - k .* sqrt( v(1) .^ 2 + v .^ 2 ) .* v - g] );
end
t0 = 0;
tfinal = 120;
v0 = [ 1 2 ]'
tfinal = tfinal*(1+eps);
tspan = t0:dt:tfinal;
[t,v] = ode45(@Func,tspan,v0);
which works fine. I thought this would be a simple extension to solving the four differentials at once by changing this to
function vp = Func(t,v)
k = 1
g = 2;
vp = diag( [ - k .* sqrt( v .^ 2 + v(2) .^ 2 ) .* v, - k .* sqrt( v(1) .^ 2 + v .^ 2 ) .* v - g, v(1), v(2)] );
end
t0 = 0;
tfinal = 120;
v0 = [ 1 2 3 4]'
tfinal = tfinal*(1+eps);
tspan = t0:dt:tfinal;
[t,v] = ode45(@Func,tspan,v0);
However, when I try this I get the error Dimensions of matrices being concatenated are not consistent. Any hints in the right direction as to how to extend from solving a system of two differential equations to four would be greatly appreciated.
7 个评论
Star Strider
2015-12-20
I’m not quite sure what you’re doing. You’ve created ‘vp’ as a (6x6) diagonal matrix. It might work better as a column vector.
McTavish Dylan
2015-12-20
Star Strider
2015-12-20
I deleted the code, but when I substituted ‘v’ as an arbitrary (1x2) vector, it created a (6x6) diagonal matrix for ‘vp’.
Actually, the MATLAB ODE functions (at least all of them I’ve worked with thus far) want the derivatives to be a column vector.
Meanwhile, see if the code in my Answer helps.
McTavish Dylan
2015-12-20
McTavish Dylan
2015-12-20
编辑:McTavish Dylan
2015-12-20
Star Strider
2015-12-20
My pleasure!
My Answer gives the full Symbolic Math Toolbox derivation. You would use the ‘Sys’ anonymous function for your ODE function, with appropriate changes to get it to work with your code.
So it would magickally transform to your ‘Func’ function as:
Sys = @(Y,g,k)[Y(2);-g-k.*sqrt(Y(2).^2+Y(4).^2).*Y(2);Y(4);-k.*sqrt(Y(2).^2+Y(4).^2).*Y(4)];
Func = @(t,y) Sys(Y,g,k);
with ‘g’ and ‘k’ previously defined in your workspace.
Ryan Compton
2018-9-25
Hi, I am trying to implement this code as a skeleton to adapt to my own data. However, running it as is (changing the vector from diagonal to column) I get the following error, I am not sure how this is even possible:
Error using odearguments (line 95) FUNC returns a vector of length 10, but the length of initial conditions vector is 4. The vector returned by FUNC and the initial conditions vector must have the same number of elements.
Did you run into this issue? I know you said you were able to get it to work.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!