Efficiently Repetitive Solving of ODE
Mostrar comentarios más antiguos
Hi all,
I'm working on a problem where I have to solve a nonlinear 2nd order ODE with initial conditions. I have to solve this ode for a set of constants (a, b, c) that coorespond to unique initial values and all are passed into my function as 1xN arrays. The constants don't change all that much, nor to the initial conditions, but will have slightly different solutions to the ode. The ode has no known symbolic solution so I feel I am stuck with numerically solving it N times.
At the moment my code works, but it is slow. Is there a more efficient way to approach this problem? Somehow leverage that my 1xN arrays do not change much and the solutions will be similar?
EDIT 11/22: The overall intent is to vary the parameters/condititions and compare the solutions. Physically, the ODE is a function of space only, but the parameters vary with time.
function value=some_function1(a,b,c,slope,x0)
%a, b, c, slope, x0 -> size of 1xN%
value=nan(size(a));
ODEFUN=@(x,Y,a,b,c) [Y(2);(a-b./(1-x).^3-c./(1-x).^2-Y(2)./x./sqrt(1+Y(2).^2)).*-(1+Y(2).^2).^(3/2)];
for ii=1:length(a)
sol=ode113(@(x,y) ODEFUN(x,y,a(ii),b(ii),c(ii)),[x0(ii) 0],[0 slope(ii)]);
%some important output value is calculated%
value(ii)=some_function2(sol.x,sol.y(1,:))
end
end
Thank you!
5 comentarios
Star Strider
el 22 de Nov. de 2021
I’m not certain that I competely understand the problem.
Is the intent to fit parameters in the ODE such that the integrated ODE is the objective function for the parameter estimation optimisation, or simply to vary the intital conditions (or other paramerers) in the ODE integration to do a number of different simulations and then plot (or otherwise compare) ther results?
.
Brandon Murray
el 22 de Nov. de 2021
James Tursa
el 22 de Nov. de 2021
Editada: James Tursa
el 23 de Nov. de 2021
Do you have a small time span where linearization might give you a faster means of finding approximate neighboring solutions?
Or maybe just feed all your ode problems to a parfor loop.
Star Strider
el 23 de Nov. de 2021
If this is a system of partial differential equations (functions of time and space), there are functions such as pdepe to integrate them and the Partial Differential Equation Toolbox devoted to solving them.
.
Brandon Murray
el 23 de Nov. de 2021
Respuestas (1)
Walter Roberson
el 23 de Nov. de 2021
If you have two odes with the same time span, then
[t1, y1] = ode(first_ode, tspan, first_conditions);
[t2, y2] = ode(second_ode, tspan, second_conditions);
is, at least in theory, equivalent to
[t3, y3] = ode(@(t,y) [first_ode(t,y(1:length(first_conditions))); second_ode(t,y(length(first_conditions)+1:end))], tspan, [first_conditions, second_conditions])
And this implies that when first_ode and second_ode are the same code, that you can slightly rewrite the ode:
function dy = joint_ode(t, yM)
y = reshape(yM, number_of_parameters, []);
dy = zeros(size(y));
%now calculate using ROW indexing, such as
dy(1,:) = y(2,:).^2 - y(3,:);
dy(2:end-1,:) = y(3:end,:);
dy(end,:) = something;
%now reshape to vector
dy = dy(:);
end
1 comentario
Brandon Murray
el 23 de Nov. de 2021
Categorías
Más información sobre Ordinary Differential Equations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!