Why won't this program run?
1 次查看(过去 30 天)
显示 更早的评论
close all;
clc;
clear all;
STEPS = 100;
tau = 10;
t0 = 2*tau;
dt = 1;
t = (0:STEPS-1)*dt;
gE = zeros(1:STEPS);
for i = 1:STEPS
a = dt*STEPS;
gE(i) = exp(-((t-t0-a)/tau).^2);
end
0 个评论
采纳的回答
Kevin Phung
2019-5-8
STEPS = 100;
tau = 10;
t0 = 2*tau;
dt = 1;
t = [0:STEPS-1]*dt;
gE = zeros(1,STEPS);
for i = 1:STEPS
a = dt*STEPS;
gE(i) = exp(-((t(i)-t0-a)/tau).^2);
end
更多回答(1 个)
John D'Errico
2019-5-8
编辑:John D'Errico
2019-5-8
Because you don't understand how to use the function zeros? Go back to go, do not collect $200, and read the getting started tutorials for MATLAB. :)
It fails to run, because that is not how zeros works. (You did make an error I have seen others do many times, so it is not the end of the world.)
STEPS is 100. 1:STEPS is the vector [1 2 3 4 5 6 7 8 9 10 ... 99 100].
Now, what happens when you use zeros? For example, what size are these arrays?
zeros(1:2)
ans =
0 0
>> zeros(1:3)
ans(:,:,1) =
0 0
ans(:,:,2) =
0 0
ans(:,:,3) =
0 0
>> zeros(1:4)
ans(:,:,1,1) =
0 0
ans(:,:,2,1) =
0 0
ans(:,:,3,1) =
0 0
ans(:,:,1,2) =
0 0
ans(:,:,2,2) =
0 0
ans(:,:,3,2) =
0 0
ans(:,:,1,3) =
0 0
ans(:,:,2,3) =
0 0
ans(:,:,3,3) =
0 0
ans(:,:,1,4) =
0 0
ans(:,:,2,4) =
0 0
ans(:,:,3,4) =
0 0
So, zeros(1:2) is an array of size 1x2. It requires 2*8=16 bytes to store.
zeros(1:3) is an array of size 1x2x3. It requires 6*8 = 48 bytes to store.
zeros(1:4) is an array of size 1x2x3x4. It requires 24*8 = 192 bytes to store.
zeros(1:n) is an array of size factorial(n), so requiring factorial(n)*8 bytes to store.
Therfore, zeros(1:100) is an array that requires
factorial(100)*8
ans =
7.4661e+158
7.4e158 bytes is a BIG number. Do you have that much RAM?
vpij2english(vpij('7.4661e+158'))
ans =
'seven hundred forty six unquinquagintillion, six hundred ten quinquagintillion'
I hope you have a large bank balance, if you want to create that array. The code you wrote fails, because it overwhelmingly exceeds the amount of memory available to the entire universe. If every elementary particle in the universe was a memory chip, it would still fail to store that much memory.
Instead, you PROBABLY wanted to create a vector of length STEPS. You could have done that using
gE = zeros(size(t));
Or, perhaps
gE = zeros(1,STEPS);
Either of those would have worked.
By the way, be careful when you create variables with a name like gE, as you might get it confused with the FUNCTION ge, which is the equivalent of the greater than or equal to function, thus >=.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Special Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!