How many iterations does a ODE solver like ode45 do? What do the dimension of outputs mean?
18 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
DB
am 28 Aug. 2022
Kommentiert: Sam Chak
am 30 Aug. 2022
Say I have a state space function
function dx = sys(t, x)
A = [0 1; -2 3]; B = [0;1]; K = [-1 -1];
u = K*x;
dx = A*x + B*u;
end
Which I will call with the following script:
tspan = [0 10];
iniCon = [1;-1]
[t, x] = ode45(@sys, tspan, iniCon);
plot(t,x)
This way, I have a two-variable function of x with an initial condition of [1;-1] which I want plotted over 0 to 10 time units.
When running this program, I get t and x with dimension 85x1 and 85x2, respectively.
My questions which are made explicit in its subquestions related to this example are:
1) Why do I get 85 rows in x?
1a)- Where is the number 85 coming from?
1b)- Why are rows and columns reverted? x started with 2 columns, now it is 2 rows.
2) How many iterations does ode45 do and why this amount?
2a) Similarly to (1a): From a timespan of [1 10] I get 85 timestamps, what is the relation and why?
Thanks in advance. See a plot below of plot(t,x).
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 28 Aug. 2022
Bearbeitet: Walter Roberson
am 28 Aug. 2022
When you pass in tspan of length 2, ode45() decides for itself when to output.
ode45() takes variable step lengths. At each step it evaluates the function at carefully chosen locations and use the results to make a prediction about where the function is going. It then takes the prediction and compares to the actual results. If the two are close enough then matlab "accepts" the point and records it internally, and increases the step size and goes back to make another prediction. If the two were not close enough then ode45 rejects the step and reduces the step size and tries again. It might fail several times in a row in places that the function is changing rapidly.
When the tspan is a vector of length 2 then each time it accepts a point, then by default it uses the predictions to generate 3 intermediate points between the previous and new points, and predicts the values there and emits those predictions and the accepted point.
So in your case about 28-ish points were accepted, 3 additional predictions for each one, plus the start point, gives 85 outputs.
The reason that the rows become columns on output is that plot() plots each column as a separate line, so it makes more sense to have each the different variables be different columns to make plot easier.
ode45 does not do iterations as such. It just keeps evolving the function and checking if the prediction is reasonable. It rejects if the function changes more rapidly than the prediction function.
One way of thinking of it is that it is making a series of quintic (order 5) polynomial approximations of the function. It uses as many tries as is needed to do that.
6 Kommentare
Walter Roberson
am 28 Aug. 2022
I suggest you switch to ode23s instead of ode45. You probably have a stiff system.
Sam Chak
am 30 Aug. 2022
Hi @DB
If you choose a different set of values for K, then the system won't blow up. For example, .
tspan = [0 10];
iniCon = [1;-1];
[t, x] = ode45(@sys, tspan, iniCon);
plot(t, x)
A = [0 1; -2 3];
B = [0; 1];
K = [1 -5];
AA = A + B*K % A*x + B*(K*x) = (A + B*K)*x
eig(AA)
function dx = sys(t, x)
A = [0 1; -2 3];
B = [0; 1];
K = [1 -5];
u = K*x;
dx = A*x + B*u;
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations 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!