Error about preallocating for speed

1 次查看(过去 30 天)
Rohin
Rohin 2022-9-28
回答: dpb 2022-9-28
Hello, I keep getting an error in my code regarding my variable changes size on each iteration and I have to preallocate for speed, how do I fix it?
% Alpha Fe
a = [];
y = 1;
for T = 25:5:1000
D = 0.0062*exp(-80000/(8.31*T));
x(y) = 1/T; %error here
a(y) = D; %error here
y = y + 1;
end
% Gamma Fe
b = [];
y = 1;
for T = 25:5:1000
D = 0.23*exp(-148000/(8.31*T));
b(y) = D; %error here
y = y + 1;
end
% plot graph
subplot(195,195,100)
plot(x,a)
plot(x,b)
xlabel("Inverse Temperature (1/K)")
ylabel("Diffusion")
title("Diffusivity vs 1/T for Alpha Fe and Gamma Fe")

回答(3 个)

KSSV
KSSV 2022-9-28
You need to proceed likt his:
% Alpha Fe
a = [];
T = 25:5:1000 ;
nT = length(T) ;
a = zeros(nT,1) ;
for i = 1:length(T)
D = 0.0062*exp(-80000/(8.31*T(i)));
x = 1/T(i); %error here
a(i) = D; %error here
end
The above can also be achieved without loop.
% Alpha Fe
T = 25:5:1000 ;
D = 0.0062*exp(-80000./(8.31*T));
x = 1./T;

Steven Lord
Steven Lord 2022-9-28
Those lines of code don't error. When Code Analyzer analyzes your code it issues a warning about those lines indicating that there may be room for improvement on those lines of code, but this is neither a run-time warning nor a run-time error.
See the code examples on this documentation page. The second one contains one line of code that is different from the first; that second example preallocates the vector x. Apply the same technique to your code.
That is, of course, if you're required (by the terms of a homework assignment) to use a for loop to construct your a, x, and b vectors. If you're not you can eliminate the loops entirely by using array operators to perform calculations element-wise on all the elements of an array at once.
% a = []; % No longer needed
% y = 1; % No longer needed
T = 25:5:1000; % Make a vector T
D = 0.0062*exp(-80000./(8.31*T)); % Compute all the elements of D at once with ./
x = 1./T; % Operate on all the elements of T at once with ./
a = D; % Overwrite the variable a with the contents of D
% y = y + 1; % No longer needed
% end % No longer needed

dpb
dpb 2022-9-28
a = [];
y = 1;
for T = 25:5:1000
D = 0.0062*exp(-80000/(8.31*T));
x(y) = 1/T; %error here
a(y) = D; %error here
...
You assigned an empty element to a and no reference at all to x so they're having to be reallocated dynamically every pass through the loop...
Do something like
N1=25; dN=5; N2=1000; % use variables, don't bury data inside code unless it truly will NEVER change
N=numel(N1:dn:N2); % compute the number elements going to have...
a=zeros(N,1); % and preallocate...
x=a;
y = 1;
for T = N1:dn:N2
D = 0.0062*exp(-80000/(8.31*T));
x(y) = 1/T;
a(y) = D;
...
However, "the MATLAB way" is to not use a loop in such instances at all, but take advantage of the vector operations -- that's what the "MATrix" part of MATLAB is all about..
N1=25; dN=5; N2=1000;
T = 25:5:1000;
a=0.0062*exp(-80000./(8.31*T));
x=1./T;
and allocation comes along with the assignment of the RHS of the expression "for free".
NB the "dot" operator ./ to operate with the vector T on an element-wise basis instead of the matrix division operation without.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by