Create a vector x with the elements

30 visualizaciones (últimos 30 días)
Noorhafizah
Noorhafizah el 3 de Feb. de 2011
Editada: SIBUNABIRWA el 30 de Nov. de 2023
Create a vector x with the elements x = (-1^n + 1/2*n -1 ) Add up the elements of the version of this vector that has 100 elements.
Can someone tell me how to do it?
  2 comentarios
Walter Roberson
Walter Roberson el 3 de Feb. de 2011
Is that (-1)^n ? Does the 1/2 apply to n or to (n-1) ? Is n a constant, or does it represent the index of the element? If it represents the index, should n start at 0 or at 1 ?
the cyclist
the cyclist el 3 de Feb. de 2011
If this is class homework, please say so and describe what you have tried on your own.

Iniciar sesión para comentar.

Respuestas (2)

Davide Ferraro
Davide Ferraro el 3 de Feb. de 2011
You can use vector operations on the vector n with elements from 1 to 100:
x = (-1.^n + 1/2*n -1);
to create the x vector.
If you need to sum every element you can then use the SUM command:
sum(x)

Barnali
Barnali el 21 de Ag. de 2023
Editada: DGM el 16 de Oct. de 2023
n = 1:100;
x = ((-1).^(n+1))./(2*n-1);
sum(x)
ans = 0.7829
  1 comentario
SIBUNABIRWA
SIBUNABIRWA el 30 de Nov. de 2023
Editada: SIBUNABIRWA el 30 de Nov. de 2023
It is working out. Thank you very much

Iniciar sesión para comentar.

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by