Tailoring ode output efficiently for a large system

8 ビュー (過去 30 日間)
L Wright
L Wright 2018 年 11 月 9 日
回答済み: L Wright 2018 年 11 月 9 日
I have a large system of odes (1000s of variables). I wish to output the full solution at a small number of times (5 or so), and a weighted average of part of the solution at every time step. I have an OutputFcn for the weighted average, and I can split the time interval such that the times at which I want the full solution are the end points of a series of sub-intervals.
However, I can't get Matlab to give me the full solution only at the ends of each interval and the OutputFcn value at all solution times. It either gives me the full solution throughout the interval or the OutputFcn value only at the times I specify.
  1 件のコメント
Jan
Jan 2018 年 11 月 9 日
Please post the relevant part of the code. Then it is much easier to post a suggestion for modifications.

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

回答 (1 件)

L Wright
L Wright 2018 年 11 月 9 日
OK, this is a hugely simplified example on a much much smaller and simpler system, and this is not the same ODE at all, but the question is generic. My code is too big to post usefully. What I have as a test is given below.
If I set tspan such that it's just the end points, it gives me y(1) and y(2) and yout at every successfully calculated time step within the interval tspan; if I put any intermediate points into tspan, it gives me y(1), y(2), and yout at only those points. What I want is y(1) and y(2) at the ends only, and yout at all intermediately calculated points.
Code:
function [t,y, yout]=opctest
tspan = [0, 10*pi]; y0=[0,1]; opts=odeset('OutputFcn',@opc); county = 0; [t,y] = ode45(@myfun,tspan,y0,opts);
function f = myfun(t,y)
f(1,1) = cos(t) + y(2);
f(2,1) = sin(t) - y(1);
end
function status = opc(t,y,flag)
if(strcmp(flag,'init'))
status=0;
yout(county+1) = y0(1);
county=county+1;
else
if (strcmp(flag,'done'))
status=0;
else
status = 0;
ny = size(y,2);
yout(county+1:county+ny) = y(1, 1:ny);
county = county+ny;
end
end
end
end

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by