How to generate series by percentage difference using loop?

1 Ansicht (letzte 30 Tage)
Triveni
Triveni am 22 Jun. 2022
Kommentiert: Triveni am 24 Jun. 2022
I have a value
A(1) = 60;
A(2) = A(1) *0.99;
A(3) = A(2) *0.99;
A(4) = A(3) *0.99;
A(5) = A(4) *0.99;
A(6) = A(5) *0.98; % Values changing after 5 iteration
.
.
.
A(11) = A(10) *0.97; % Values changing after 5 iteration
.
.
.
.till the differences reached to 0.01.
Please help me.

Akzeptierte Antwort

KSSV
KSSV am 22 Jun. 2022
Bearbeitet: KSSV am 22 Jun. 2022
A = zeros([],1) ;
A(1) = 60 ;
dA = 1 ;
i = 1 ;
while dA > 0.01
i = i+1 ;
A(i) = A(i-1)*0.99 ;
dA = abs(A(i)-A(i-1)) ;
end
  4 Kommentare
Triveni
Triveni am 23 Jun. 2022
Value of .99 is not changing after 5th iteration. Means A(6) = A(5) *0.98; please correct.
Triveni
Triveni am 24 Jun. 2022
%----------------------------------------
PP = 28;
sdf = 0.995;
%----------------------------------------
%Generate Series
A = zeros([],1) ;
A(1) = PP;
dA = 1 ;
i = 1 ;
while dA > 0.0001
i = i+1 ;
if length(A)>4 && rem(length(A),5)==0
sdf = sdf-.01;
A(i) = A(i-1)*sdf ;
else
A(i) = A(i-1)*sdf ;
end
dA = abs(A(i)-A(i-1)) ;
end
A = round(unique(A'),2);
A = A(fliplr(1:end),:);
N = 0.05; % Round value
B = unique(nonzeros(round(A/N)*N));
A = B(fliplr(1:end),:);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements 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