2 coupled second order equations

1 回表示 (過去 30 日間)
Eliraz Nahum
Eliraz Nahum 2018 年 9 月 28 日
編集済み: Bruno Luong 2018 年 10 月 25 日
Hello everyone, I have a problem of 2 coupled second order equations of the form that is described in the photo I attached. By the way – W(t) and T(t) are quite simple functions, and I plan calling another external function when using it. The reason I don't include them above is the fact that they have a "steps" form and hence time-interval dependent. As I see the only option is a numeric solution by using the ode45 function. I want to make sure that using the above function is possible at all by doing the manipulation that is also described in the attached photo. I will be happy to hear what is your advice – is there another good way to solve this problem? If not, is the methodic way described above should work? How should I write the command when using the ode45 function? [t,q]=ode45(@(t,q) F(t,q),t_span,q0) While F is a 2x1 vector that consists each of the above eauations.
  6 件のコメント
Walter Roberson
Walter Roberson 2018 年 9 月 29 日
Runge–Kutta methods to estimate slopes are only valid if the slopes have continuous derivatives, which is not the case for step functions.
ode45() does not always detect the discontinuity, which just means that the answers produced are not always valid.
Bruno Luong
Bruno Luong 2018 年 9 月 29 日
編集済み: Bruno Luong 2018 年 9 月 29 日
The non-stiff method such as Runge-Kutta is less accurate at the breakpoints, but it is a valid solution.
I don't know if you see the graph of the example I gives, but yes it detect the discontinuity indirectly by estimate the error term and it reduces the step when it cross the break point. The solution looks OK (zoom around t=1, you'll see it negotiates the break point very very well). The theoretical solution at final time t=2 is y=1 theorically, and ode45 has an error of 3.148e-5. Tell me if that accuracy does not satisfy you.
The ODE solver are more sophisticated than you might think.
Nothing telling me (beside you of course) that the solution is not valid.

サインインしてコメントする。

採用された回答

Bruno Luong
Bruno Luong 2018 年 9 月 30 日
編集済み: Bruno Luong 2018 年 9 月 30 日
Bellow is the code with 2 methods.
Observation: piecewise RUNGE-KUTTA method is more accurate whenever the trajectory is orbital around the attractor (0,0)
% S(t) := T(t) / W(t),
% S(t) = s(i) for tbin(i) <= t < tbin(i+1);
%
n = 10; % number of interval where S is constant
tbin = [0 cumsum(0.1+rand(1,n))];
s = rand(1,n);
s(end+1) = s(end);
q0 = rand(4,1);
% Cauchy condition
tspan = tbin([1 end]);
[t,q] = ode15s(@(t,q) odefun(t,q,tbin,s), tspan, q0);
x = q(:,1);
y = q(:,3);
plotsol(1,t,x,y,tbin,'single run ode15s');
%
t = [];
q = [];
for k=1:n
subts = tbin([k k+1]);
[tk,qk] = ode45(@(t,q) odefun(t,q,tbin,s), subts, q0);
t = [t; tk];
q = [q; qk];
q0 = q(end,:);
end
x = q(:,1);
y = q(:,3);
plotsol(2,t,x,y,tbin,'piecewise ode45');
function plotsol(fignum,t,x,y,tbin,str)
% plot the solution
fig = figure(fignum);
clf(fig);
% make figure wider
p = get(fig,'position');
p(3) = 800;
set(fig,'position',p);
subplot(1,2,1),
h = plot(t,[x,y]);
xlabel('t');
hold on
n = length(tbin);
for k=2:n
plot(tbin(k)+[0 0],ylim(gca),'-.k');
end
legend(h,'x','y');
title(str);
subplot(1,2,2),
plot(x,y);
xlabel('x');
ylabel('y');
title(str);
end
function qp = odefun(t,q,tbin,s)
x = q(1);
xp = q(2);
y = q(3);
yp = q(4);
[~,i] = histc(t,tbin);
S = s(i);
a = sqrt(x.^2+y.^2).^3;
b = sqrt(xp.^2+yp.^2);
c = S./b;
xpp = -x./a + xp.*c;
ypp = -y./a + yp.*c;
qp = [xp; xpp; yp; ypp];
end
  4 件のコメント
Jan
Jan 2018 年 10 月 25 日
@Bruno: [~,i] = histc(t,tbin); S=s(i) can cause discontinuities in the function to be integrated. Matlab's ODE integrators cannot handle this reliably, see http://www.mathworks.com/matlabcentral/answers/59582#answer_72047. ODE45 might find a trajectory, stop due to a too small stepsize or in the worst case calculate a "result" which is far beyond the actual bounds defined by the error tolerances.
In your case you limit the time span to one specific tbin([k k+1]), such that histc find the same bin during a run of the integration. So wouldn't it be cheaper to determine the contstant outside the integration instead of calling histc for each call of the function?
Bruno Luong
Bruno Luong 2018 年 10 月 25 日
編集済み: Bruno Luong 2018 年 10 月 25 日
Depend on type of discontinuity, as long as it does not "stiffness", affects only low-order terms, the error estimate and adaptive time step work just well, as in the case of this rocket eqt we discuss here.
I showed in my code it works just fine numerically by splitting intervals or integrate straight through in a single shot. ODE45 handles it just fine, and I don't even need to use ODE15s.
As for use TSPAN, yes, it can sync the break points, but I can't see how one can remove HISTC (which is not expansive either) since the ode function never knows before hand which interval ODE45 is working on. However one can loop in the intervals as I showed in my code for-loop that call sequentially ODE45, that's probably the cleanest way to handle discontinuity.
I and Walter went over this discussion already, see the comments part of the question.

サインインしてコメントする。

その他の回答 (2 件)

Eliraz Nahum
Eliraz Nahum 2018 年 9 月 29 日
Sorry guys but I still can't understand if the code I wrote os feasible...
  1 件のコメント
Bruno Luong
Bruno Luong 2018 年 9 月 29 日
It seems perfectly OK to me.

サインインしてコメントする。


Eliraz Nahum
Eliraz Nahum 2018 年 10 月 1 日
Thank you all for your answers. I read each one and learned a lot.

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by