# How to save ode45 outputs in a loop?

조회 수: 2 (최근 30일)
EB 2019년 10월 9일
편집: EB 2019년 10월 9일
I am trying to save my T and Y values in a loop, but it gives me the error "Subscripted assignment dimension mismatch."
I originally had [T,Y] = ode45(dydt, t, [y0, dy0], options); but it would only save when j = 7.
for j = 1:7
[T(j),Y(j)] = ode45(dydt, t, [y0, dy0]);
end

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

### 채택된 답변

Star Strider 2019년 10월 9일
The easiest way would be to save them as cella rrays, then sort those out later:
for j = 1:7
[T{j},Y{j}] = ode45(dydt, t, [y0, dy0]);
end
Note the curly brackets {} indicating cell array indexing.
It will be easier to work with them if you define ‘tspan’ as a vector with more than two elements:
t = linspace(0, 5, 50); % time span
This will result in all the arrays having equal numbers of rows.

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

### 추가 답변 (1개)

James Tursa 2019년 10월 9일
편집: James Tursa 2019년 10월 9일
Don't use a loop. Just call ode45 once and it will give you the entire results in T and Y.
[T,Y] = ode45(dydt, t, [y0, dy0]);
If you want outputs at specific times, set t accordingly. E.g.,
t = linspace(0,5,100);

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

### 카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

R2016a

### Community Treasure Hunt

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

Start Hunting!

Translated by