Is it possible to use ode45() once instead of many times for different distinct times?

2 views (last 30 days)
Majid
Majid on 12 Nov 2021
Commented: Majid on 13 Nov 2021
Hi everyone,
I need to solve a system of differential equations and find the value of solutions in certain integer-valued times.
The system is
F(x,y,x', y')=0
G(x,y,x', y')=0
With the initial conditions
x(0) = p1, y(0) = p1
x'(0) = v1, y'(0) = v2.
I firstly used the dsolve() function but it could not find the explicit solution, and so I used the ode45() function then. Here is my code to do it.
function [Sol, Nsol] = SolvingSysEq2D(oDe, V, P, tt)
%oDe is the vector of equations, V is the velocity vector, P is the initial point, tt is the time
syms T Y t x(t) y(t) z(t) v1 v2
EqnS = [oDe(1) == 0, oDe(2) == 0];
[VF, ~] = odeToVectorField(EqnS);
ExEqSyst = matlabFunction(VF, 'Vars',{T, Y});
n = size(V,1);
Sol = zeros(1,2);
for i = 1:n
if rem(i,1)==0, disp(i), end
conD = [P(2), V(i,2), P(1), V(i,1)];
[~, Nsol] = ode45(@(T, Y)ExEqSyst(T, Y), [0, tt], conD);
Sol(i,:)= Nsol(end, [3, 1]);
end
I need to find the values of x(t) and y(t) at t = 1, 2, 3, 4, 5, …, 10. For now, I use my function ten times, each time for a t. I would like to know if there is a way that I use my function only one time and then obtain x(t) and y(t) for all the t =1, 2, …, 10.
I know that by using my function for t=10, I can draw the graphs of x(t) and y(t) on t in [0, 10], that is I can have all the data from t=0 to t=10, but I do not know how to get out the values of x(t) and y(t) at the specific times t = 1, 2, 3, 4, 5, …, 10.
Any help would be appreciated.

Accepted Answer

Walter Roberson
Walter Roberson on 12 Nov 2021
Instead of passing [0, tt] as the time span, pass in 0:10 . It will calculate for the intermediate times necessary to figure out all of 0 to 10, but it will only report at the times you list.
This applies any time the time vector has more than 2 elements. If you give it two elements exactly then it reports back at whatever times it feels like between those two.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!