how to use different columns of a passed matrix for individual time step in an ode
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sanjana Singh
am 30 Mai 2020
Kommentiert: Sanjana Singh
am 30 Mai 2020
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
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 30 Mai 2020
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!