ode23, too slow

22 views (last 30 days)
Gloria
Gloria on 3 Jun 2022
Commented: Steven Lord on 3 Jun 2022
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
Walter Roberson on 3 Jun 2022
https://www.mathworks.com/help/matlab/math/parameterizing-functions.html
Walter Roberson
Walter Roberson on 3 Jun 2022
I also suspect that you have a stiff system and should try ode23s

Sign in to comment.

Accepted Answer

Jan
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
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.

Sign in to comment.

More Answers (1)

Gloria
Gloria on 3 Jun 2022
Thank you so much! It got much more better !

Community Treasure Hunt

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

Start Hunting!