How to generate series by percentage difference using loop?

3 views (last 30 days)
Triveni on 22 Jun 2022
Commented: Triveni on 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.

KSSV on 22 Jun 2022
Edited: KSSV on 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
Triveni on 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),:);

Categories

Find more on Matrix Indexing in Help Center and File Exchange

R2022a

Community Treasure Hunt

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

Start Hunting!