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!