필터 지우기
필터 지우기

Use ode values without storing

조회 수: 1 (최근 30일)
olabaz
olabaz 2017년 9월 6일
편집: Stephen23 2017년 9월 7일

Hi, I have a system of ODEs that I can solve using ode23. To solve I do:

[t, S] = ode23(@odefunc,tSpan,S0)

and then what I am really interested in is a variable:

D(t) = 2*pi*(S(1)(t) + S(2)(t) + S(3)(t) + ...)

and then I plot D(t) vs t.

Doing it this way, however, I am forced to store all S(1)(t), all S(2)(t), etc. when I really only need their sum for each time. This takes a lot of memory since I have 280k*3 equations. How can I go about just calculating D at each step and saving that in an array to then plot D(t) vs t.

채택된 답변

Stephen23
Stephen23 2017년 9월 7일
편집: Stephen23 2017년 9월 7일
Write your own output function, which is called by ode45 after each successful step:
Inside the function:
  • Store the sum value in a persistent variable.
  • Add a special case that returns the persistent variable (e.g. the third input = 'getval').
To use it:
  • Use the init call to empty the persistent variable.
  • On each call append the sum value onto the persistent variable.
  • After ode45 has finished call the output function with your special third input to return the persistent variable.
Easy! You have stored only the values that you want, and get them in a vector at the end.
  댓글 수: 2
olabaz
olabaz 2017년 9월 7일
Thanks. This is what I was looking for. I wasn't able to get the output working so I just made a global variable and appended to it.
Stephen23
Stephen23 2017년 9월 7일
편집: Stephen23 2017년 9월 7일
@olabaz: I am glad that it works. If you upload your code I will have a look. I would recommend avoiding global if you can.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jacob Ward
Jacob Ward 2017년 9월 6일
You could create a function that calls ode23, finds the sum of S, and then returns that sum. Something like this:
function [t,D] = odeSum()
...
[t, S] = ode23(@odefunc,tSpan,S0)
D(t) = 2*pi*(S(1)(t) + S(2)(t) + S(3)(t) + ...)
end
Since the function throws away any variables that aren't output variables, you wouldn't be saving all the useless S data and would be left with only D when you come out of the function.
  댓글 수: 3
Jacob Ward
Jacob Ward 2017년 9월 6일
I see. Well, I could be wrong, but I think in order to do what you are saying you'd have to go in and change the ode23 code itself so that it only outputs what you want. Maybe someone else will have a better suggestion.
Stephen23
Stephen23 2017년 9월 7일
"...you'd have to go in and change the ode23 code itself..."
Not required at all. See my answer.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by