inquiry about a command

4 vues (au cours des 30 derniers jours)
mohamed
mohamed le 4 Déc 2013
Modifié(e) : Andrei Bobrov le 5 Déc 2013
im trying to answer this question
Write a program to calculate: X = 1/a – 1/2a + 1/3a – 1/4a + ………… – 1/100a For values of a = -5, -3, -1, 1, 3, …50
so far ive tried
a=[-5:2:50];
>> n=[1:100];
>> x=0;
>> for i=1:100
term= (-1.^n +1)/a*n;
x=x+term;
end
but i keeps giving me error (Error using /)
the next question is
Without using loops, generate the vector x = [1 1/2 1/3 1/4 1/5 1/6 … 1/100].
and ive tried putting
n=[2:99]
x=[1/1 :1/n :1/100]
is that right
thx

Réponses (2)

Walter Roberson
Walter Roberson le 4 Déc 2013
See the ./ (rdivide) operator and the .* (times) operator
  2 commentaires
mohamed
mohamed le 4 Déc 2013
what about it
Walter Roberson
Walter Roberson le 4 Déc 2013
You used the / operator. That is the wrong operator for your purpose. Use ./ instead.

Connectez-vous pour commenter.


Andrei Bobrov
Andrei Bobrov le 4 Déc 2013
Modifié(e) : Andrei Bobrov le 5 Déc 2013
a=-5:2:50;
n=1:100;
X = -sum((-1).^n./n)*a; % EDIT
  1 commentaire
Andrei Bobrov
Andrei Bobrov le 5 Déc 2013
corrected

Connectez-vous pour commenter.

Catégories

En savoir plus sur Time Series Events 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