How to generate series by percentage difference using loop?

조회 수: 1 (최근 30일)
Triveni
Triveni 2022년 6월 22일
댓글: Triveni 2022년 6월 24일
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.

채택된 답변

KSSV
KSSV 2022년 6월 22일
편집: KSSV 2022년 6월 22일
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
Triveni
Triveni 2022년 6월 23일
Value of .99 is not changing after 5th iteration. Means A(6) = A(5) *0.98; please correct.
Triveni
Triveni 2022년 6월 24일
%----------------------------------------
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),:);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by