??? Subscript indices must either be real positive integers or logicals.

2 vues (au cours des 30 derniers jours)
Dennis
Dennis le 10 Oct 2012
Here's my code:
N = 10;n=0:N;
f(n) = cumsum(((-1).^n)/(3.^n));
g(n) = 3/4+1/4*(-1/3).^n;
I am new to Matlab. Please send a complete solution. Thank you.
  2 commentaires
Star Strider
Star Strider le 10 Oct 2012
I will not send a complete solution, but I will point out that MATLAB interprets your f(n) and g(n) statements as array references, not functions. To create functions, see the documentation on Anonymous Functions.
Matt Fig
Matt Fig le 10 Oct 2012
How could we send a complete solution when we don't even have a complete question?
Please learn to format your code. Highlight the code, then push the button that looks like {}Code.

Connectez-vous pour commenter.

Réponses (2)

Muruganandham Subramanian
Muruganandham Subramanian le 10 Oct 2012
Hi, I matalab, you cannot find f(0). so you should start with n=1:N and check the below coding, it may help you..
N = 10;
for n=1:N;
f(n) = cumsum(((-1).^n)/(3.^n));
g(n) = 3/4+1/4*(-1/3).^n;
end

Andrei Bobrov
Andrei Bobrov le 10 Oct 2012
f1 = @(n)cumsum((-1).^n./3.^n);
g1 = @(n)3/4+1/4*(-1/3).^n;
>> N = 10;
>> n1 = 0:N;
>> f1(n1)
ans =
1 0.66667 0.77778 0.74074 0.75309 0.74897 0.75034 0.74989 0.75004 0.74999 0.75
0:N
ans =
1 0.66667 0.77778 0.74074 0.75309 0.74897 0.75034 0.74989 0.75004 0.74999 0.75
or
f2 = @(N)cumsum((-1).^(0:N)./3.^(0:N));
g2 = @(N)3/4+1/4*(-1/3).^(0:N);
>> f2(10)
ans =
1 0.66667 0.77778 0.74074 0.75309 0.74897 0.75034 0.74989 0.75004 0.74999 0.75
>> g2(10)
ans =
1 0.66667 0.77778 0.74074 0.75309 0.74897 0.75034 0.74989 0.75004 0.74999 0.75

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by