Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Are there any other way that I can optimize my functions or write them differently?

1 visualización (últimos 30 días)
I'm trying to grab a values from a function with an input of time. With my code right now, it takes about 10 minutes to evaluate 100 data points, and 20 minutes for 180 data points. I believe what takes the function long to run is the Arterial function. Is there any other ways I can write this code? I'm new to Matlab.
The following are my functions:
This function grab a value from predictionvalue function.
function grabvalues= grabvalue(tfinal,F,f,z,PS)
tic
grabvalues=zeros(1,length(tfinal));
for i=1:length(tfinal)
grabvalues(i)= predictionvalue(tfinal(i),F,f,z,PS);
end
toc
end
This function has three main parts; Arterial function, RK4 function, and Sampling time. The Arterial function takes in shortdata.txt file and linear interpolate the output. The RK4 then use the Arterial function to get the values for Sample times.
function tis_predicted2 = predictionvalue(tfinal,F,f,z,PS)
%----------------Define Functions-----------%
%Arterial Function
load shortdata.txt
tA=shortdata(:,1);
AA=shortdata(:,2);
function output=AF_function_in(tA_input)
%size(tA);
xq1=0:0.01:tA_input;
vq1=interpn(tA,AA,xq1,'linear');
output=vq1(end);
%plot(xq1,vq1,'-',tA,AA,'o');
%set(gca,'XLim',[0 10])
end
%Two Compartmental Model Functions
fP=@(t,I,P) (F/f)*(AF_function_in(t)- P) - (PS/f)*(P-I); % Hct(mv)=0.45
fI=@(t,I,P) (PS/z)*(P - I);
%Initial Condition
h=0.01;
N=ceil(tfinal/h);
t=zeros(1,N);
P=zeros(1,N);
I=zeros(1,N);
%------------------- RK4 Loop-----------------------------------%
for i=1:N %i=[0, tfinal/h]
% Update t
t(i+1)=t(i)+h;
%Update equation
k1P = fP(t(i) , I(i) , P(i) );
k1I = fI(t(i) , I(i) , P(i) );
k2P = fP(t(i)+0.5*h, I(i)+ 0.5*k1I*h, P(i)+0.5*k1P*h);
k2I = fI(t(i)+0.5*h, I(i)+ 0.5*k1I*h, P(i)+0.5*k1P*h);
k3P = fP(t(i)+0.5*h, I(i)+ 0.5*k2I*h, P(i)+0.5*k2P*h);
k3I = fI(t(i)+0.5*h, I(i)+ 0.5*k2I*h, P(i)+0.5*k2P*h);
k4P = fP(t(i)+h ,I(i)+k3I*h , P(i)+ k3P*h);
k4I = fI(t(i)+h ,I(i)+k3I*h , P(i)+ k3P*h);
I(i+1) = (I(i) + h/6 *(k1I + 2*k2I + 2*k3I + k4I));
P(i+1) = (P(i) + h/6 *(k1P + 2*k2P + 2*k3P + k4P));
end
Tissue_predicted = P*f+I*z;
%-----------------SAMPLING ---------------------------------------------%
tdelta=0.835;
tfinalsub=tfinal+0.000001;
tSamp=zeros(1,ceil(tfinalsub/tdelta));
Samp=zeros(1,ceil(tfinalsub/tdelta));
a(1)=0;
for d = 1:ceil(tfinalsub/tdelta)-1
a(d+1)= a(d)+tdelta/h;
tSamp(d+1)= tSamp(d)+tdelta;
Samp(d+1)= Tissue_predicted(1,ceil(a(d)+1));
end
tis_predicted2 = Samp(end);
end

Respuestas (1)

Steven Lord
Steven Lord el 26 de Mayo de 2017
"I believe what takes the function long to run is the Arterial function."
I recommend that you profile your code and use that data to identify the sections that take the most time, to be certain.
Just at a glance, my educated guess about one of the bottlenecks in your code will be this line.
load shortdata.txt
Don't load the data once per iteration of the for loop in your grabvalue function. Load it once before that loop starts and pass the data into the predictionvalue function as an additional input.
Also, look at the Code Analyzer warnings, if any, in your code. Correcting any issues it identifies may improve the performance and/or robustness of your code.
  2 comentarios
Arbol
Arbol el 26 de Mayo de 2017
Editada: Arbol el 26 de Mayo de 2017
I'm trying to come up with a code that would load the data before the prediction function comes in. However, I couldn't come up with anything. My thought was trying to use a classdef that could call the data values and use method to do it?
Steven Lord
Steven Lord el 27 de Mayo de 2017
You're passing five inputs (tfinal(i), F, f, z, and PS) into predictionvalue when you call it. Why not pass a sixth containing the data you loaded?

La pregunta está cerrada.

Community Treasure Hunt

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

Start Hunting!

Translated by