How can I change the numbers to negative without altering the positive ones and make a sum of that? This is the serie 1-2+3-5+8-13+21-34+...

3 Ansichten (letzte 30 Tage)
n=8;
F=[1 2];
suma=0;
for i=3:n
F(i)=F(i-1) + F(i-2);
end
disp(F)
this is only for show the positive serie

Akzeptierte Antwort

Voss
Voss am 24 Mai 2022
Bearbeitet: Voss am 24 Mai 2022
n=8;
F=[1 2];
% suma=0;
for i=3:n
F(i)=F(i-1) + F(i-2);
end
disp(F)
1 2 3 5 8 13 21 34
F(2:2:end) = -F(2:2:end)
F = 1×8
1 -2 3 -5 8 -13 21 -34
cumsum(F)
ans = 1×8
1 -1 2 -3 5 -8 13 -21
sum(F)
ans = -21

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by