For loop time consuming

Is there a way to rewrite this code? If I go to tdata(i) = 100, the iteration loop would have to go through 100/h=100/0.1=1000 loops, or at tdata(i)=200, the loop would have to go through 2000 loops. Is there a way to keep or memorize the previous loop rather than start over again?
tdata is time, and it increases.
function tout = grabvalue(p,tdata,tu)
for i=1:length(tdata)
out(i) = RK4(p,tdata(i),tu);
end
tout = out;
end
function Tissue_single = RK4(p,tfinal,tu)
h=0.1;
F=p(1); fp=p(2); fis=p(3); PS=p(4);
if tfinal == 0
N=1;
else
N=ceil(tfinal/h);
end
t=zeros(1,N);
y=zeros(2,N);
f=@(t,y) [...
(F/fp)*(interpn(tu(:,1),tu(:,2),t)-y(1))-(PS/fp)*(y(1)-y(2));
(PS/fis)*(y(1)-y(2))];
%------------------- RK4 Loop-----------------------------------%
for i=1:N
% Update t
t(i+1)=t(i)+h;
%Update equation
k1 = f(t(i) ,y(:,i) );
k2 = f(t(i)+0.5*h, y(:,i)+ 0.5*k1*h);
k3 = f(t(i)+0.5*h, y(:,i)+ 0.5*k2*h);
k4 = f(t(i)+h , y(:,i)+k3*h);
y(:,i+1) = (y(:,i) + h/6 *(k1 + 2*k2 + 2*k3 + k4));
end
Tissue= y(1,:)*fp+y(2,:)*fis;
Tissue_single=Tissue(end);
end

Answers (1)

Image Analyst
Image Analyst on 17 Jun 2017

0 votes

A for loop of 1000 or 2000 iterations is not time consuming. The computations inside the loop may be time consuming but the for loop itself is not. I just did a for loop with 100 million iterations and it took only 0.2 seconds so don't worry about an extremely miniscule 1000 iterations. We're talking millionths of a second for that few iterations.
Anyway, I don't know what "memorize the previous loop" means so I don't know what to tell you.

5 Comments

Arbol
Arbol on 17 Jun 2017
Let's say tdata(1)=10s and tdata(2) = 20s, at 10s/0.1= 100 iterations. Instead of starting again at iteration=0, can I save the data from 100 iterations and pass it into tdata(2)=20s, so it doesn't have to through that 100 iterations again? Plus, if I do out=grabvalue(p,tdata,tu), where tdata is 225 data points, this takes about 2-3mins to do compute. Just wonder if I can make this faster.
How are you calling the function? What are you passing in for p and tu?
Arbol
Arbol on 17 Jun 2017
tu=load(test.txt); Where test has 225x3. tu(:,1) and tu(:,2) are where I used to interpolation in my RK4 function. P is a set of parameters, ie [0.1 0.2 0.3 0.4], 1x4 vector. I use this to estimate parameters. The reason I don't use ODE45 because it has variable steps, and when I run a fitting (lsqcurvefit,lsqnon,etc), the parameters doesn't fit well with my model (f(t,y) in my RK4 function).
I'm getting it to run in 0.38 seconds. See attached program.
tdata =
10 20
tout =
10.7411530699029 190.22825510961
Elapsed time is 0.381234 seconds.
Arbol
Arbol on 17 Jun 2017
Edited: Arbol on 17 Jun 2017
Can you try tdata=tu(:,1)? This will take a lot longer.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 17 Jun 2017

Edited:

on 17 Jun 2017

Community Treasure Hunt

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

Start Hunting!