MATLAB Answers

0

Creating 2x2 matrix is slow

Asked by xtremecheez on 11 Sep 2019
Latest activity Edited by dpb
on 12 Sep 2019
Accepted Answer by dpb
I am attempting to optimize my code, especially by reducing the number of computations that are performed. This is a portion of the Profiler results from a trial run:
In contrast, the following line is also called 4553019 times but only requires 0.905 seconds:
dm = -dt*(A*sig*norm(cf)*0.5*rhoA*v^3);
Why do these matrix operations take up so much more time than the many floating point operations in the script?
Edit: Here is the majority of the function in question.
function [h,t,v,th,gr,m,ch,qmult] = fxn1(cst,drag,visc,...
xloc0,zloc0,cfdb,IXt,IXh,IXgr,IXd,IXvx,IXvz,interact,fnum,state,ch,ablmode,vlim,sig0,sigma)
%state = [h,t,v,th,gr,m,d]
h = state(1);
t = state(2);
v = state(3);
th = state(4);
gr = state(5);
m = state(6);
d = state(7);
g = cst(2)*(cst(4)/(cst(4)+h))^2; %gravity varies with altitude
dt = cst(3)/(v*sin(th)); %computes time step (varies bc velocity changes)
rhoA = 1.225; %density of air (kg/m^3)
sth = sin(th); cth = cos(th);
rotQ3 = [-cth,sth; -sth,-cth];
rotQ1 = rotQ3';
if interact
[cd,cl] = fxn2(cst,drag,visc,xloc0,zloc0,cfdb,IXt,IXh,IXgr,IXd,IXvx,IXvz,fnum,h,t,gr,d,v*[cos(th),sin(th)],rotQ1);
cf = rotQ3*[cd;cl];
else
cf = rotQ3*[drag(1);0];
end
A = pi*(d/2)^2;
fscale = 0.5*rhoA*v^2*A/m;
dv = dt*(fscale*cf-[0;g]);
dth = dt*(v/(cst(4)+h)*cos(th));
dgr = dt*(v*cos(th));
qmult = norm(cf)/drag(1);
dm = -dt*(A*sigma*norm(cf)*0.5*rhoA*v^3);
%% Update variables
vx = v*cos(th) + dv(1);
vz = v*sin(th) + dv(2);
if vz < vlim
vz = vlim;
end
if vx < 0
vx = 0;
end
v = sqrt(vx^2+vz^2);
th = abs(atan(vz/vx)) + dth; %changed from atan2 b/c angle can't go past 90 deg
if th > pi/2 %Cap theta at 90-deg
th=pi/2;
end
m = m + dm;
gr = gr + dgr;
h = h - cst(3);
t = t + dt;
end

  2 Comments

dpb
on 11 Sep 2019
Because they're memory allocation and possibly reallocation.
Would need to see code in context to have any idea about what possibly could be done easily, at least.
@dpb, I've included the code from the relevant function. Hope this helps.

Sign in to comment.

Products


Release

R2018b

1 Answer

dpb
Answer by dpb
on 11 Sep 2019
Edited by dpb
on 12 Sep 2019
 Accepted Answer

The attached profile specific lines constitute only 39% of the total so there's 61% elsewhere but the allocation issue using [] may be reduced some, possibly.
See what happens if you replace
rotQ3=[-cth, sth; -sth, cth];
with
rotQ3=zeros(2);
rotQ3(1,1)=-cth;
rotQ3(1,2)= sth;
rotQ3(2,1)=-sth;
rotQ3(2,2)= cth;
The above was a factor of about 2X reduction here in a braindead kind of timing exercise. What it will show in the actual use you'll have to determine empirically.
In this section you've computed sin(th) and cos(th) but I see the same calculation scattered all over the rest of the function instead of reusing the already computed values. That could possibly help some as well unless th is being updated in between (possible, but it wasn't apparent in relatively quick look).
Another thing I notice is the only place that rotQ1 is used is apparently in the call to fxn2() -- consider recasting it to not need the transpose or to do the transpose in situ instead of creating the secondary variable. I didn't try testing this to see if really matters. Also, you're using the tick ' for transpose which is complex transpose so since it appears rotQ3 is real, see if using the .' non-conjugate transpose operator will make any noticeable difference.

  0 Comments

Sign in to comment.