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

1 visualización (últimos 30 días)
Nimisha
Nimisha el 22 de Nov. de 2014
Respondida: Jan el 23 de Nov. de 2014
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..?

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 23 de Nov. de 2014
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!

Más respuestas (1)

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

Categorías

Más información sobre Mathematics en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by