# Why won't this program run?

3 views (last 30 days)
Zahid Saleem on 8 May 2019
Commented: Kevin Phung on 9 May 2019
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

Kevin Phung on 8 May 2019
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

Zahid Saleem on 8 May 2019
You are my saviour, my own persoan Jesus Christ !
Kevin Phung on 9 May 2019
happy to help!

John D'Errico on 8 May 2019
Edited: John D'Errico on 8 May 2019
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 >=.

#### 1 Comment

Zahid Saleem on 9 May 2019
What a dumb mistake :) I should probably go to sleep now :) Thnx for the information.