How to sum over indices?

86 visualizaciones (últimos 30 días)
Omar
Omar el 28 de Nov. de 2019
Comentada: Omar el 28 de Nov. de 2019
Given for some , how can one writes the sum using the Matlab Code. When I try, I get confused. Please Help me out.
I started by fixing my m=5 say, and write an=[1,-1,0,1,1] say, and with r=0.5, I generate r^m=3.125e-07. Then I don't know how to fit this into the for environment.
This is as follows:
an=[1,-1,0,1,1];
b0=an(1); rm=3.125e-07;
for k=1:5;
b(k)=b0+rm^k*a(k)
end
what is wrong here please? I cannot proceed from here. I appreciate any assistance how to sort this out.

Respuesta aceptada

Jan
Jan el 28 de Nov. de 2019
Editada: Jan el 28 de Nov. de 2019
If you assume, that something is wrong, mention, what you observe. This is better than letting the readers guess, what the problem is.
You have defined an, but use a later. For me this code is running, when I change "an" to "a":
a = [1,-1,0,1,1];
b0 = a(1);
rm = 3.125e-07;
for k = 1:5
b(k) = b0 + rm^k * a(k);
end
This creates the elements of the sum, but not the sum itself. Perhaps you want:
S = 0;
for k = 1:5
S = S + rm^k * a(k);
end
or simpler:
k = 1:5;
S = sum(rm.^k .* a);
  1 comentario
Omar
Omar el 28 de Nov. de 2019
Thanks a lot for your response. This is really helpful. I got solution to the problem.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by