how to use different columns of a passed matrix for individual time step in an ode

1 次查看(过去 30 天)
I am trying to pass a matrix (vfx which is a 10 x 10 matrix) as a parameter to a function to solve using ode15s. I understand that at each time step I will obtain a column vector that will be then passed onto the ode15s function. The problem I am facing is to extract a column from the matrix that is passed for each time step and use that in the calculation for that time step. I don't understand how to do that.
% calling ode_research_x from the main script
% vfx is a 10 x 10 matrix
vpx0 = 5.*ones(1,10);
x_range =linspace(1,3,10) ;
[x_val,vx] = ode15s(@(x_val,vx) ode_research_x(x_val,vx,vfx), x_range, vpx0);
% called ode_research_x function
function dvdx = ode_research_x(x,vx,vfx)
dvdx = (vfx(:,1) - vx)./vx; %this works with only the first column of vfx, I wish to use 1st column of vfx for the first time step, 2nd column with 2nd time step value and so on
end

采纳的回答

Walter Roberson
Walter Roberson 2020-5-30
The ode*() routines are variable step solvers that will adjust their timestep as required to meet the integration tolerances. The first timestep might be 1, but the second timestep might be 1.0001 and the third timestep might be 1.7. Furthermore, for each iteration, the solver will evaluate at a series of different times.
The ode*() routines all require that the equations be continuous for the duration of the ode*() call, and that is not going to be the case for you.
I suspect that by "timestep" you are referring to the linspace(1,3,10), and that you are expecting that linspace(1,3,10) means the routine will evaluate at time 1.0 (and you want to use the first column for that), then time 11/9 (and you want to use the second column for that), then time 13/9 (and you want to use the third column for that), and so on. However, that is not the case: the solver will evaluate at a series of times and boundary conditions to mathematically calculate acceptable integrations.
If you want to use one value of a parameter for times [1, 11/9), then a second for [11/9, 13/9), then a third for [13/9, 15/9) and so on, then you will need to stop integration at each of those boundary times and restart with the new parameter, such as
x_range = linspace(1,3,10) ;
Nrange = length(x_range) - 1;
x_val = cell(Nrange,1);
Vx = cell(Nrange,1);
vpx0 = 5.*ones(1,10);
for K = 1 : Nrange
[x_val{K}, Vx{K} = ode15s(@(x_val,vx) ode_research_x(x_val, vx, vfx(:,K)), x_range(K:K+1), vpx0);
vpx0 = Vx{K};
end
And then afterwards, put together all of the x_val and Vx cells
  2 个评论
Sanjana Singh
Sanjana Singh 2020-5-30
It makes sense to me but how do I put together the x_val and vx cells? It would be great if you clear up this small bit too. Thank you so much !
Sanjana Singh
Sanjana Singh 2020-5-30
I tried executing the code but it doesn't work. There is a dimensional mismatch in the ode15 calling statement.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by