How to solve Attempted to access (0) error.?

1 次查看(过去 30 天)
In attached figure, one equation is there which i need to implement.
where,
x[n] = a* r^n
a = 11; r = 0.9; L = 19; 0<=m<=L; 0<=n<=L
for this i have written a script.
clear all
a = 11;
r = 0.9;
L = 19;
for m = 0:L
for n = 0:L
S(m) = a*(r^n);
end
end
plot(S,m)
xlabel('Value of "S"');
ylabel('Value of "L"');
title('a = 11; r = 0.9; L = 19')
When i run it, it gives following error
Attempted to access (0); index must be a positive integer or logical.
Error in Question_6 (line 8)
S(m) = a*(r^n);
How to solve it..?

采纳的回答

Geoff Hayes
Geoff Hayes 2014-11-23
Nimisha - unlike C/C++, indexing into MATLAB arrays begins at one instead of zero. Since your code is iterating (over m) from 0 to L, then the error message makes sense for
S(m) = a*(r^n);
I don't think that you have quite gotten this equation correct. From your attached image, it looks like
S[m] = sum(n=0...m)x[n];
which to me means that your S could be initialized as
a = 11;
r = 0.9;
L = 19;
S = zeros(L+1,1); % size S
S(1) = a*(r^0); % this corresponds to S[0] in your equation
for m = 2:L+1
S(m) = S(m-1) + a*(r^m);
end
Note how the above assumes that S is a recurrence relation. We can then plot it as
plot(S,0:L)
xlabel('Value of "S"');
ylabel('Value of "L"');
title('a = 11; r = 0.9; L = 19')
Try it and see what happens!

更多回答(1 个)

Jan
Jan 2014-11-23
When you omit the clear all you can use the debugger to find out the reason of your problem.

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by