(n start with 0) An=A0+A1+ A2+A3+…+ An-1+An process??

sorry, actually the question seems easy but I am new user, ı don't know much things about MatLab functions,
how can i type that An=A0+A1+ A2+A3+…+ An-1+An in matlab mey be there is a function? n start with 0 to 100
I tried to do with A(0), A(1), .. but A(0) can not determined!!
thank you

6 comentarios

Baris
Baris el 17 de En. de 2013
thank you for helping but actually I am looking for example following formula;
so, I could not type a0 for this question, the rest is Ok.
thank you!
Azzi Abdelmalek
Azzi Abdelmalek el 17 de En. de 2013
Your link is not working, which formula?
Image Analyst
Image Analyst el 17 de En. de 2013
The link works now, but there is no sum at all in that formula!!
Matt Kindig
Matt Kindig el 17 de En. de 2013
Editada: Matt Kindig el 17 de En. de 2013
The actual formula in the equation can be implemented as:
n= 0:1:100
a = ((-1).^n)*pi.^(2*n)./gamma(2*n+1); %gamma implements factorial function
%note that a(n+1) is the term for a given value of n, because of the lack of zero-indexing in Matlab
That said, as Image Analyst pointed out above, there is no sum in the formula.
Jan
Jan el 17 de En. de 2013
@Baris: You cannot write "a(0)", because Matlab starts indices at 1. Simply write "a(1)", which has no influence to the result.
Baris
Baris el 21 de En. de 2013
thank you for helping. actually it is easy to implement if I use a(1) in stead of a(0).I just wanted to learn that. So,ıf there is no way to do, I will do using a(1).
thank you so much again!!

Iniciar sesión para comentar.

Respuestas (4)

Ryan Livingston
Ryan Livingston el 16 de En. de 2013

2 votos

In MATLAB, array indices begin at 1 rather than zero so A(0) generally won't succeed.
The SUM function could be of assistance:
Matt J
Matt J el 16 de En. de 2013
Use the CUMSUM command, e.g.
>> cumsum([4 6 9 3])
ans =
4 10 19 22
Azzi Abdelmalek
Azzi Abdelmalek el 16 de En. de 2013
An=@(n) cos(n) % example for cos(n)
out=sum(arrayfun(@(x) An(x),0:n))

1 comentario

An=@(n) (-1)^n*pi^(2*n)/factorial(2*n)
out=sum(arrayfun(@(x) An(x),0:100))

Iniciar sesión para comentar.

Image Analyst
Image Analyst el 16 de En. de 2013
Not sure if this is what you meant, but the code below will do EXACTLY what you said. This code will replace the nth element only, with the sum of the elements from 1 to n, and leave the other elements of A unchanged. n is 100. If that's not what you meant, then please be specific.
% Define sample data of length 120.
A = randi(9, 1, 120)
n = 100; % or whatever element you want to stop at.
fprintf('A(%d) initially equals %d\n', n, A(n));
% Replace the nth element only, with the sum of the elements from 1 to n.
% Leave the other elements of A unchanged!
A(n) = sum(A(1:n))
fprintf('but now A(%d) equals %d\n', n, A(n));
Note: because MATLAB starts at 1 but you started at 0, you may have to adjust n or else adjust the sum:
A(n) = sum(A(1:n+1)); % n=8 is the 9th element if you consider A starting at 0

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 16 de En. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by