Help with evaluating a solution of ODE45 at specific times?

7 views (last 30 days)
Hi,
I am trying to find the stability conditions on an equation of motion, which means evaluating the solution at t = 2*pi. I have to specify the initial conditions as well which means I also need to evaluate at time t = 0. I do not care about all the points in between as they get discarded. The whole thing is inside two "for" loops which means it is taking ages to compute. I'm wondering if there is a way to either only compute the solution at times t=0 and t=2*pi... or can I at least drastically reduce the number of times at which the soltuion is evaluated. Hope this is clear, and any help will be much appreciated!
Tom

Answers (1)

Jan
Jan on 1 Apr 2012
If the equation, you want to integrate, has no closed form solution, you have to calculate it to all intermediate time steps to get the final position. Otherwise you would not need an integrator.
You can increase the tolerances for the local discretization error. But this reduces the accuracy of the final position.
There could further methods to increase the processing speed. Without seeing the code it is impossible to give any advice.
  1 Comment
Tom
Tom on 1 Apr 2012
Thank you very much for your reply. Here is a copy of my code, and I welcome any suggestions you have for improvement. I'm sure there must be a faster way of doing this.
%%%%% Stability of Mathieu equation with damping %%%%%
clear all; clc; close all;
global b a q;
b = 0;
tspan = [0,2*pi];
i = 1;
for q = 0:0.01:15
for a = -2:0.01:10
z01 = [1 0];
z02 = [0 1];
[eta1,z1] = ode45('solverz',etaspan,z01);
[eta2,z2] = ode45('solverz',etaspan,z02);
U = [z1(end,1) z2(end,1);z1(end,2) z2(end,2)];
lambda = max(eig(U));
if lambda < 1
avec(i) = a;
qvec(i) = q;
i = i+1;
end
end
end
figure(1)
plot(qvec,avec,'x')
xlabel('q parameter');ylabel('a parameter')
%%%% and a copy of the function with the equations: %%%%
function dz1deta = solverz(eta,z1)
global b a q
dz1deta = zeros(size(z1));
dz1deta(1) = z1(2);
dz1deta(2) = -(a - 2*q*cos(2*eta))*z1(1) - b*z1(2);
If there's anything you can immediately spot to speed it up thta would be great.
Again, many thanks,
Tom

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!