MATLAB Answers

Why won't this program run?

1 ビュー (過去 30 日間)
Zahid Saleem
Zahid Saleem 2019 年 5 月 8 日
コメント済み: Kevin Phung 2019 年 5 月 9 日
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
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
  2 件のコメント
Kevin Phung
Kevin Phung 2019 年 5 月 9 日
happy to help!

サインインしてコメントする。

その他の回答 (1 件)

John D'Errico
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 >=.
  1 件のコメント
Zahid Saleem
Zahid Saleem 2019 年 5 月 9 日
What a dumb mistake :) I should probably go to sleep now :) Thnx for the information.

サインインしてコメントする。

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by