How to generate series by percentage difference using loop?

1 view (last 30 days)
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.

Accepted Answer

KSSV
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
  4 Comments
Triveni
Triveni on 23 Jun 2022
Value of .99 is not changing after 5th iteration. Means A(6) = A(5) *0.98; please correct.
Triveni
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),:);

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!