Question 3 - Comparison of Q1 and Q2 solution
Contents
Logistic Equation
figure [t,x]=ode23(@logistic,[0,3], 2); plot(t,x(:,1), 'DisplayName', 'ode23'); xlabel('t'); ylabel('x'); hold on; disp('Logistic Equation:'); tic; [t,x]=q1_solution(@logistic,0,3, 2); duration = toc; plot(t,x(:,1),'ro','DisplayName','Q1'); fprintf(' Q1 Computation Time: %f\n', duration); tic; [t,x]=q2_solution(@logistic,0,3, 2); duration = toc; plot(t,x(:,1),'mx','DisplayName','Q2'); fprintf(' Q2 Computation Time: %f\n', duration); legend('Location', 'NorthEast');
Logistic Equation: Q1 Computation Time: 0.001434 Q2 Computation Time: 0.001275
Stiff Linear System
figure [t,x]=ode23(@linsystem,[0,0.1], [2; 1]); plot3(t,x(:,1),x(:,2), 'DisplayName', 'ode23'); xlabel('t'); ylabel('x'); hold on; disp('Stiff Linear System:'); tic; [t,x]=q1_solution(@linsystem,0,0.1, [2; 1]); duration = toc; plot3(t,x(:,1),x(:,2),'ro','DisplayName','Q1'); fprintf(' Q1 Computation Time: %f\n', duration); tic; [t,x]=q2_solution(@linsystem,0,0.1, [2; 1]); duration = toc; plot3(t,x(:,1),x(:,2),'mx','DisplayName','Q2'); fprintf(' Q2 Computation Time: %f\n', duration); legend('Location', 'NorthEast');
Stiff Linear System: Q1 Computation Time: 0.002954 Q2 Computation Time: 0.002302
Oscillator
figure [t,x]=ode23(@oscillator,[0, 20], [1, 0]); plot3(t,x(:,1),x(:,2), 'DisplayName', 'ode23'); xlabel('t'); ylabel('x'); hold on; disp('Oscillator:'); tic; [t,x]=q1_solution(@oscillator,0,20, [1, 0]); duration = toc; plot3(t,x(:,1),x(:,2),'ro','DisplayName','Q1'); fprintf(' Q1 Computation Time: %f\n', duration); tic; [t,x]=q2_solution(@oscillator,0,20, [1, 0]); duration = toc; plot3(t,x(:,1),x(:,2),'mx','DisplayName','Q2'); fprintf(' Q2 Computation Time: %f\n', duration); legend('Location', 'NorthEast');
Oscillator: Q1 Computation Time: 0.003978 Q2 Computation Time: 0.003030
We note that for Q1 that for the low order method
and for the high order method
Therefore, four evaluations of
are required.
For Q2, as it is an embedded method, we only have three evaluations of
.
We expected that the Q1 method is slower than the Q2 method, which appears to be confirmed by the computation time.