Creating 2x2 matrix is slow
古いコメントを表示
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 件のコメント
dpb
2019 年 9 月 11 日
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.
xtremecheez
2019 年 9 月 11 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Mathematics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!