Reverse geometric progression algorithm

2 次查看(过去 30 天)
I would like to have a set of points from -b to b using a geometric progression from dpB / 2 to b and mirroring the points with respect to 0. However, I want the grid to be "denser" closer to b, meaning that the gap between 2 points going from dpB/2 to b should decrease not increase. How can I tweek my code to achieve that?
b = 5;
N = 25;
dpB = 2 * b / N;
kk = (N+1)/2;
r = ((dpB/2)/b)^(1 / (kk-1));
pBy = zeros(1,N+1);
mid = (N+1) / 2;
pBy(N+1) = b;
pBy(1) = -b;
for i=N:-1:(mid+1)
pBy(i) = pBy(i+1) * r;
end
pBy(1:mid) = -flip(pBy((mid+1):end));

采纳的回答

John D'Errico
John D'Errico 2021-2-20
编辑:John D'Errico 2021-2-20
Since you want a symmetric set of points around 0, first generate a set on the positive side of the origin, then just flip them around.
b = 5;
N = 25;
a = b / N % This is just dpB/2, since you multiply then divide by 2.
a = 0.2000
So I'll generate a set of points that runs effectively down from b to a, with increasing spacing as it moves towards a. What is the geometric increment?
geoinc = nthroot(b/a,N-1)
geoinc = 1.1435
t = geoinc.^(0:N-1);
V = a + b - a*flip(t)
V = 1×25
0.2000 0.8276 1.3764 1.8563 2.2760 2.6430 2.9639 3.2446 3.4900 3.7047 3.8923 4.0565 4.2000 4.3255 4.4353 4.5313 4.6152 4.6886 4.7528 4.8089 4.8580 4.9009 4.9385 4.9713 5.0000
So the spacing increases as you move towards a, away from b, and it does so in geometric fashion. Now just duplicate the set, negating them.
V = [flip(-V),V]
V = 1×50
-5.0000 -4.9713 -4.9385 -4.9009 -4.8580 -4.8089 -4.7528 -4.6886 -4.6152 -4.5313 -4.4353 -4.3255 -4.2000 -4.0565 -3.8923 -3.7047 -3.4900 -3.2446 -2.9639 -2.6430 -2.2760 -1.8563 -1.3764 -0.8276 -0.2000 0.2000 0.8276 1.3764 1.8563 2.2760
plot(V,'o')
So easy enough. But no, I won't even try to tweak your code. Far better to learn to use MATLAB as it is designed to be used, manipulating vectors and arrays.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by