Efficiently Repetitive Solving of ODE
5 views (last 30 days)
Show older comments
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 Comments
Star Strider
on 23 Nov 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.
.
Answers (1)
Walter Roberson
on 23 Nov 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
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!