ode23, too slow

10 次查看(过去 30 天)
Gloria
Gloria 2022-6-3
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 个评论
Walter Roberson
Walter Roberson 2022-6-3
https://www.mathworks.com/help/matlab/math/parameterizing-functions.html
Walter Roberson
Walter Roberson 2022-6-3
I also suspect that you have a stiff system and should try ode23s

请先登录,再进行评论。

采纳的回答

Jan
Jan 2022-6-3
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 个评论
Steven Lord
Steven Lord 2022-6-3
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.

请先登录,再进行评论。

更多回答(1 个)

Gloria
Gloria 2022-6-3
Thank you so much! It got much more better !

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by