Global variables are inefficient and. make errors difficult to diagnose
37 次查看(过去 30 天)
显示 更早的评论
I'm writing code that builds the Vortex Lattice method, but I'm having trouble calling the variables in my Main code. I'm getting the error in the title on every code and I don't understand why.
0 个评论
采纳的回答
Cris LaPierre
2024-12-9,20:01
编辑:Cris LaPierre
2024-12-9,21:14
Why not pass the variables as inputs and outputs to your functions? I did this by putting all the code into one script, then used the Refactor Code option to break each piece out to its own local function. They could just as easily be function files as well.
%--------------------------------------------------------------------------
% Parametreler
%--------------------------------------------------------------------------
c = 1; % Veter uzunluğu
b = 1; % Kanat açıklığı
NJ = 10; % Panel sayısı Y
NI = 1; % Panel sayısı X
NJ1 = NJ+1;
NI1 = NI + 1;
Gamma = 1;
P = Geometry(NI1, NJ1, b, NJ, c, NI);
[G, C] = Panels(NJ1, P, NJ);
[PC, P1, P2] = DGP(NI, NJ, C, G);
Vor3D(PC, P1, P2, Gamma);
function P = Geometry(NI1, NJ1, b, NJ, c, NI)
P = zeros(NI1, NJ1, 3);
dy = b/NJ;
dx = c/NI;
for j = 1:NJ1
for i = 1:NI1
P(i,j,:) = [(i-1)*dx, (j-1)*dy, 0];
end
end
end
function [G, C] = Panels(NJ1, P, NJ)
%--------------------------------------------------------------------------
% Girdap uç noktalari
%--------------------------------------------------------------------------
B_alpha_deg = 10; % Derece olarak
L_uzunluk = 10; % L'nin uzunluÄŸu
L_vektor = [L_uzunluk * cosd(B_alpha_deg), 0, L_uzunluk * sind(B_alpha_deg)]';
G = zeros(3, NJ1, 3);
for j = 1:NJ1
G(1,j,:) = P(1,j,:) + 0.25*(P(2,j,:) - P(1,j,:));
G(2,j,:) = P(2,j,:);
G(3,j,:) = squeeze(G(2,j,:)) + L_vektor;
end
% disp (G);
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
C = zeros(1, NJ, 3);
for j = 1:NJ
CL = P(1,j ,:) + 0.75*(P(2,j ,:) - P(1,j ,:));
CR = P(1,j+1,:) + 0.75*(P(2,j+1,:) - P(1,j+1,:));
C(1,j,:) = (CL + CR) / 2;
% C(1,j,:) = (P(1,j,:) + 0.75*(P(2,j,:) - P(1,j,:)) + P(1,j+1,:) + 0.75*(P(2,j+1,:) - P(1,j+1,:))) / 2;
end
% disp (C);
%--------------------------------------------------------------------------
% Yuzey birim normal vektorlerini hesaplama
%--------------------------------------------------------------------------
zeta = zeros(1, NJ, 3);
for j = 1:NJ
R1 = squeeze(P(1, j, :) - P(2, j+1, :));
R2 = squeeze(P(2, j , :) - P(1, j+1, :))';
R12 = cross(R1, R2);
normal_buyukluk = norm(R12);
zeta(1, j, :) = R12 / normal_buyukluk;
end
end
function [PC, P1, P2] = DGP(NI, NJ, C, G)
for i = 1:NI
for j = 1:NJ
PC = squeeze(C(i,j,:));
P1 = squeeze(G(i,j ,:));
P2 = squeeze(G(i,j+1,:));
end
end
end
function Vor3D(PC, P1, P2, Gamma)
r1 = PC - P1;
r2 = PC - P2;
r0 = P2 - P1;
r12 = cross(r1, r2);
r1r2 = r12 / (norm(r12))^2;
Term2 = (r1 / norm(r1)) - (r2 / norm(r2));
r0Term2 = dot(r0, Term2);
DeltaV = Gamma / (4*pi) * r1r2 * r0Term2;
end
更多回答(1 个)
Walter Roberson
2024-12-9,21:56
The message is a warning, not an error message.
You are getting the warning message because using global variables is the most expensive form of accessing variables. Literally every other possibility is checked before checking whether a variable is a global. That makes accessing global variables relatively slow.
You are also getting that warning message because historically people have found using global variables to be error-prone, and that it can be quite difficult to track down where global variables are changed. They might not be bad when they are set in a single block of code and are read-only in all other blocks of code, but much more typical is that they end up being modified in several places.
Because the use of global variables is slow and error-prone, the editor gives a warning about them. It is an opportunity to reflect upon whether you really need global variables, and upon whether you have taken precautions to ensure that the global variables are being used correctly.
You can turn off the warning. Right-click on the word "global" and select "Suppress Message" and then either "on this line" or "in this file". But turning off the warning... just turns off the warning, without fixing any of the underlying problems.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!