How do I create a vector of n exponentially increasing values from a to b?

조회 수: 154(최근 30일)
Tom 2013년 4월 4일
편집: Stephen 2018년 8월 13일
I know how to calculate the growth factor, but not how to turn it all into a vector. Here's the start of my code: -
n = 4
a = 1
b = 10
gf = (b/a)^(1/n - 1)
댓글 수: 1표시숨기기 없음
Tom 2013년 4월 4일
I think I've done it with a for loop: -
n = 4
a = 1
b = 10
gf = (b/a)^(1/(n - 1))
for k = 1:n
y(k) = a*gf^(k-1);
end
Does anyone know how I could do it using a non-for loop method?

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

채택된 답변

the cyclist 2013년 4월 4일
k = 1:n;
y = a*gf.^(k-1);
or, even simpler
y = a*gf.^((1:n)-1);
댓글 수: 3표시숨기기 이전 댓글 수: 2
Stephen 2018년 8월 13일
@Ramm: That error message is telling you what the problem is. It does not mention that you need any "specific module". It shows that your gf is non-scalar and is not the same size as k, thus you will get this error. The code in this answer works perfectly, as long as you define a scalar growth factor (exactly as the question shows):
>> n = 4;
>> a = 1;
>> b = 10;
>> gf = (b/a)^(1/n-1)
gf = 0.17783
>> k = 1:n;
>> y = a*gf.^(k-1)
y =
1.0000000 0.1778279 0.0316228 0.0056234

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

추가 답변(1개)

Carlos 2013년 4월 4일
>> y=zeros(4,1);
>> n=1:1:4;
>> y(1:4)= a*gf.^(n(1:4)-1);
>> y
y =
1.0000
2.1544
4.6416
10.0000
댓글 수: 3표시숨기기 이전 댓글 수: 2
Stephen 2018년 8월 13일
@Ramm: although that n usage is a bit dodgy, it also works.

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

Community Treasure Hunt

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

Start Hunting!