Why won't this program run?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Zahid Saleem
am 8 Mai 2019
Kommentiert: Kevin Phung
am 9 Mai 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
0 Kommentare
Akzeptierte Antwort
Kevin Phung
am 8 Mai 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
2 Kommentare
Weitere Antworten (1)
John D'Errico
am 8 Mai 2019
Bearbeitet: John D'Errico
am 8 Mai 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 >=.
Siehe auch
Kategorien
Mehr zu Special Functions finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!