How to preserve 2-d initial conditions after passing it into ode45?

4 次查看(过去 30 天)
Hi,
I pass 2d initial conditions, but ode45 linearizes it and makes the matrix into a 1-d array. How do I ensure it is handled as a 2-d array?
I have the following:
--------------------------------------
N=60;
x(1:N, 1:2*N) = 0; x(20,20)=1;
options = odeset('RelTol',1e-7);
[t1 y] = ode45('fputest1', [0:L:t], x, options, N);
-----------------------------------------------------
x is a 60x120 matrix. When I step through the code in ode45, x is a 7200 x 1, 1-d array. How do I ensure x remains a 2-d matrix? Also what are the dimensions of y, when the solver has finished? Is it 2-d if x is an array? What is it in the case x is a 2-d matrix?

采纳的回答

Walter Roberson
Walter Roberson 2021-4-19
When I step through the code in ode45, x is a 7200 x 1, 1-d array. How do I ensure x remains a 2-d matrix?
To do that, write your own version of ode45. The Mathworks version will always pass in the boundary conditions as a column vector.
Most people deal with the situation by simply reshape() the input almost immediately.
Also what are the dimensions of y, when the solver has finished?
y will be length(t1) by numel(x).
Your function is required to return a column vector that is numel(x) by 1, and integrated versions of that column will become rows of the output.
What is it in the case x is a 2-d matrix
Then y will be length(t1) by numel(x).
You can reshape y afterwards, such as
yperm = permute(reshape(y, length(t1), size(x,1), size(x,2)),[2 3 1]);
and now yperm(:,:,iteration) will be the 2D results at time t1(iteration)
  5 个评论
Walter Roberson
Walter Roberson 2021-4-20
for i = 1 : size(yperm,3)
surf(yperm(:,:,i), 'edgecolor', 'none');
pause(1);
end
or
volumeViewer(yperm)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by