I edited my post, adding comments to my code; hopefully is easier to understand. Please let me know if further clarification would be helpful :)
How to optimize my code (plotting imported data)
조회 수: 2 (최근 30일)
이전 댓글 표시
I am plotting the mass flow rate Q over the square root of a pressure diference. The result should look as follows
I managed to plot it but my actual code is quite inefficent. I am trying to optimize my code, so that it is more efficient and easily understood by others. I am quite convinced it can be easily improved using vectors. For instance, notice how I have to change values for each Q manually...
Here is the code for just the first two points. For the other 8 I followed the same procedure.
clc
close all
clear all
warning('OFF', 'MATLAB:table:ModifiedAndSavedVarnames')
p_in=9.04;
p_out=[8.06 7.8 7.6 7.4 7.0 6.8 6.6 6.4 6.2 6.0];
p_sat=6.35;
Area = 3.14*(0.015/2).^2;
%How to generalize Q=rho*area*magnitudeOfTheVelocity?
A = readtable("surfaceFieldValue1eNEG06Inlet.dat");
rho = A{:,"Var2"}; %density
rho_last100values = rho(end-100:end); %From this array we'll extract last 100 values
M_rho_last100values=mean(rho_last100values,"all"); %mean value of the last 100 values
%CODE TO GET THE FIRST POINT (Outlet pressure p=8.06 bar)
Ux_A = A{:,"Var3"}; %velocity component
Uy_A = A{:,"Var4"}; %velocity component
Uz_A = A{:,"Var5"}; %velocity component
U_A = sqrt(Ux_A.^2 + Uy_A.^2 + Uz_A.^2); %magnitude of the velocity
UA_last100values = U_A(end-100:end); %las 100 values of the magnitude of the velocity
M_UA_last100values=mean(UA_last100values,"all"); %mean value of the last 100 values
Q_8_06bar = M_rho_last100values.*Area.*M_UA_last100values %we just apply the definition of mass flow rate Q, that is Q=rho*area*magnitudeOfTheVelocity
sqrtDeltaP_8_06bar=sqrt(p_in-p_out(1));%calculate the square root of the pressure difference; the pressure at the inlet is always 9.04
%REPEAT THE ABOVE FOR THE NEXT POINT
%Outlet pressure p=7.8 bar
B = readtable("surfaceFieldValue_0.007001.dat");
Ux_B = B{:,"Var3"};
Uy_B = B{:,"Var4"};
Uz_B = B{:,"Var5"};
U_B = sqrt(Ux_B.^2 + Uy_B.^2 + Uz_B.^2);
UB_last100values = U_B(end-100:end);
M_UB_last100values=mean(UB_last100values,"all");
Q_7_8bar = M_rho_last100values.*Area.*M_UB_last100values
sqrtDeltaP_7_8bar=sqrt(p_in-p_out(:,2));
Q = [Q_8_06bar;Q_7_8bar];
sqrtDeltaP = [sqrtDeltaP_8_06bar;sqrtDeltaP_7_8bar];
%Q plot
figure
plot(sqrtDeltaP,Q,'*','LineWidth',1.5)
legend('Outlet pressure 8 bar','Outlet pressure 7.8 bar','Outlet pressure 7.6 bar','Outlet pressure 7.4 bar','Outlet pressure 7.0 bar','Outlet pressure 6.8 bar','Outlet pressure 6.6 bar','Outlet pressure 6.4 bar','Outlet pressure 6.2 bar','Outlet pressure 6.0 bar','Location','bestoutside')
xlabel('sqrt Delta p')
ylabel('Q [kg/s]')
axis square;
grid on;
The needed files are in the Optimization Data folder here: https://drive.google.com/drive/folders/1CgeDuCahVZvLXXkNkGWlPg8lSGyurmI3
Could you please guide me on how to optimize it? Thank you :)
댓글 수: 2
Jan
2022년 6월 19일
What do you want to improve?
I'd omit the useless clear all because it wastes time with no benefits. It is "cargo cult programming". Use functions instead of scripts to keep the workspace clean.
답변 (1개)
Shushant
2023년 9월 27일
Hi JD_PM,
I understand that you want to make your code more readable and scalable. As mentioned by Jan in the comments you can do so by using "function".
Take the section of code which is being repeated multiple times and convert it into a "function". Refer to the following code snippet where I have made a function which takes the "fileName" and "Area" as input and returns the value of "Q" as output-
p_in=9.04;
p_out=[8.06 7.8 7.6 7.4 7.0 6.8 6.6 6.4 6.2 6.0];
p_sat=6.35;
Area = 3.14*(0.015/2).^2;
%How to generalize Q=rho*area*magnitudeOfTheVelocity?
Q_8_06bar = myfunc("surfaceFieldValue1eNEG06.dat", Area); % Calling our function
sqrtDeltaP_8_06bar=sqrt(p_in-p_out(1));%calculate the square root of the pressure difference; the pressure at the inlet is always 9.04
Q_7_8bar = myfunc("surfaceFieldValue_0.116312.dat", Area); % Calling our function
sqrtDeltaP_7_8bar=sqrt(p_in-p_out(:,2));
Q = [Q_8_06bar; Q_7_8bar];
sqrtDeltaP = [sqrtDeltaP_8_06bar; sqrtDeltaP_7_8bar];
%Q plot
figure
plot(sqrtDeltaP,Q,'*','LineWidth',1.5)
legend('Outlet pressure 8 bar','Outlet pressure 7.8 bar','Outlet pressure 7.6 bar','Outlet pressure 7.4 bar','Outlet pressure 7.0 bar','Outlet pressure 6.8 bar','Outlet pressure 6.6 bar','Outlet pressure 6.4 bar','Outlet pressure 6.2 bar','Outlet pressure 6.0 bar','Location','bestoutside')
xlabel('sqrt Delta p')
ylabel('Q [kg/s]')
axis square;
grid on;
function Q = myfunc(fileName, Area)
A = readtable(fileName);
rho = A{:,2}; % Will always consider the 2nd columns as the value of rho
rho_last100values = rho(end-100:end); %From this array we'll extract last 100 values
M_rho_last100values=mean(rho_last100values,"all"); %mean value of the last 100 values
%CODE TO GET THE FIRST POINT (Outlet pressure p=8.06 bar)
Ux_A = A{:,3}; %velocity component
Uy_A = A{:,4}; %velocity component
Uz_A = A{:,5}; %velocity component
U_A = sqrt(Ux_A.^2 + Uy_A.^2 + Uz_A.^2); %magnitude of the velocity
UA_last100values = U_A(end-100:end); %las 100 values of the magnitude of the velocity
M_UA_last100values=mean(UA_last100values,"all"); %mean value of the last 100 values
Q = M_rho_last100values.*Area.*M_UA_last100values; %we just apply the definition of mass flow rate Q, that is Q=rho*area*magnitudeOfTheVelocity
end
In a similar way, you can define a customized function that meets your specific needs. However, it is important to note that for the "function" to be simplified, all datasets should have the same structure. If the datasets differ significantly from each other, it may introduce complexity during the design of your "function".
Refer to the following documentation for more information on how to create your own "function"-
I hope this information helps in solving the issues you were facing.
Thank you,
Shushant
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Performance Profiling에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!