ode23, too slow
3 views (last 30 days)
Show older comments
It takes a long time to solve the code. Is it because the code is reading data from a.dat file? How can I make it go faster? It slows down even more as time increases and the time step gets smaller.
2 Comments
Walter Roberson
on 3 Jun 2022
https://www.mathworks.com/help/matlab/math/parameterizing-functions.html
Accepted Answer
Jan
on 3 Jun 2022
Reading the files in each iteration is a very time-consuming method. Do this once only and store the values in a persistent variable:
function [du] = mart(t,u)
persistent Fyr FyrPi Fzr FzrPi Mxr MxrPi f
if isempty(Fyr)
Fyr = load('fz_F_i_recon.dat');
FyrPi = importdata('fz_Phi.txt');
Fzr = load('fy_F_i_recon.dat');
FzrPi = load('fy_Phi_i_recon.dat');
Mxr = load('mx_F_i_recon.dat');
MxrPi = load('mx_Phi_i_recon.dat');
f = load('frequency.dat');
end
Do not include all import commands in [ and ]. [] is Matlab's operator for a concatenation of arrays. With [load('file')] you concatenate the output of load() with nothing, so this is a waste of time only.
Save time by creating constants as constants: 77e9 is cheap, 77*10^9 is an expensive power operation.
There is no reason to create the same variables in a loop repeatedly:
for j=1:4
...
c(1)=60;c(2)=180;c(3)=150;c(4)=100; % Why?! Move it behind the loop
k(1)=1800;k(2)=2400;k(3)=2000;k(4)=2000;
end
1 Comment
Steven Lord
on 3 Jun 2022
Do this once only and store the values in a persistent variable:
Alternately load them outside mart.m entirely and pass them into the mart function (which is the ODE function for the system, I assume; I have not looked at all the files) as extra parameters.
More Answers (1)
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!