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

88 views (last 30 days)
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 Comment
Tom
Tom on 4 Apr 2013
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?

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 4 Apr 2013
k = 1:n;
y = a*gf.^(k-1);
or, even simpler
y = a*gf.^((1:n)-1);
  3 Comments
Ramm
Ramm on 13 Aug 2018
Edited: Ramm on 13 Aug 2018
Is this a specific module that I need to add? In my case it doesn't work:
n = 4;
a = 1;
b = 10;
K>>
K>> k = 1:n;
K>> y = a*gf.^(k-1);
Error using .^ (line 1000)
Sizes of x, y don't match.
K>> y = a*gf.^((1:n)-1);
Error using .^ (line 1000)
Sizes of x, y don't match.
Stephen23
Stephen23 on 13 Aug 2018
Edited: Stephen23 on 13 Aug 2018
@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

Sign in to comment.

More Answers (1)

Carlos
Carlos on 4 Apr 2013
>> 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

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!