??? Subscript indices must either be real positive integers or logicals.
显示 更早的评论
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 个评论
Star Strider
2012-10-10
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
2012-10-10
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.
回答(2 个)
Muruganandham Subramanian
2012-10-10
0 个投票
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
2012-10-10
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
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!