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

조회 수: 1 (최근 30일)
Nimisha
Nimisha 2014년 11월 22일
답변: Jan 2014년 11월 23일
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