How can I speed up my Code?

1 次查看(过去 30 天)
Gerrit
Gerrit 2020-1-9
编辑: Gerrit 2020-1-11
Good evening,
I have a problem with the performance of my Code, as Im new to matlab.
I hope the comments help you to understand everything.
The main problem is, that most of the time I have no points at the x-coordinate. Thats why I interpolate the closest and the secound closest. But that I have to do for every AeroDataId, so every Mach/CL combination in my database. To speed it up a little bit, I only use the points in a certin mach and Cl delta.
load('new_upper_airfoil');
%% searched point
Ma=0.7;
C_l=0.65;
%% delta of cl and Mach
cl_delta=0.015;
Ma_delta=0.015;
%% delta of the points in the original pressure distribution
Delta=2/height(originalDV);
DV=zeros(height(originalDV),2);
Temp=array2table(zeros(0,4),'VariableNames',{'X_C','CP','Ma','CL_res'});
Temp_update=array2table(zeros(0,4),'VariableNames',{'X_C','CP','Ma','CL_res'});
i=1;
%% load only the points within a Mach Number and Lift coefficient delta
new_upper_airfoil=new_upper_airfoil(new_upper_airfoil.Ma < (Ma+Ma_delta) & new_upper_airfoil.Ma > (Ma-Ma_delta) & new_upper_airfoil.CL_res < (C_l+cl_delta) & new_upper_airfoil.CL_res > (C_l-cl_delta),:);
new_lower_airfoil=new_lower_airfoil(new_lower_airfoil.Ma < (Ma+Ma_delta) & new_lower_airfoil.Ma > (Ma-Ma_delta) & new_lower_airfoil.CL_res < (C_l+cl_delta) & new_lower_airfoil.CL_res > (C_l-cl_delta),:);
%% Interpolation of upper_airfoil
for x_c=0:Delta:1
%% Vector with all unique Ids (muss nicht nochmal für Unterseite erstellt werden)
DataId_unique=unique(new_upper_airfoil.AeroDataId); %könnte gespeichert werden für alle Daten
for a=1:1:size(DataId_unique)
%% temp table with all the information for one ID (AeroData ID is no longer needed)
new_upper_airfoil_unique=new_upper_airfoil(new_upper_airfoil.AeroDataId==DataId_unique(a,:),2:5); % könnte später gespeichert werden
%% try to find x/c
FIND=new_upper_airfoil_unique(new_upper_airfoil_unique.X_C==x_c,:);
if(isempty(FIND))
%% find the closest and secound closest x-coordinate and interpolate between them
[d,sortIndex]=sort(abs(new_upper_airfoil_unique.X_C-x_c));
closest=new_upper_airfoil_unique(sortIndex(1),:);
secound_closest=new_upper_airfoil_unique(sortIndex(2),:);
average=interp1([closest.X_C,secound_closest.X_C], [closest.CP,secound_closest.CP], x_c, 'linear','extrap');
%% saved with closest Ma and Cl as they are the same as in secound closest
Temp=[Temp;{x_c, average, closest.Ma, closest.CL_res}];
else
Temp=[Temp;FIND];
end
end
%% Interpolation
cp_inter=IDW(Temp.Ma, Temp.CL_res, Temp.CP, Ma, C_l, -2, 'ng', 4);
Temp(:,:)=[];
%% Result
DV(i,:)=[x_c,cp_inter];
i=i+1;
end
  9 个评论
Stephen23
Stephen23 2020-1-11
"Preallocation is slower in this case."
Please upload the code that you tried.
Gerrit
Gerrit 2020-1-11
编辑:Gerrit 2020-1-11
The problem here is, that Temp is getting filled up again with every loop step.

请先登录,再进行评论。

回答(0 个)

类别

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

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by