我有一個單位圓,上面標有n個單位根。我希望能夠旋轉、平移和縮放一條位于x軸(在 -1 和 1 之間)上的線,以連接任何一對標記的根。目前,我的代碼在某些情況下可以做到這一點,但通常不起作用。我想避免硬編碼每對可能的線應該如何移動。這是我到目前為止所擁有的:
clear
%% Roots of unity
n = 10;
roots = zeros(1, n);
for k = 1 : n
roots(k) = exp(2 * k* pi * 1i / n);
end
%% Move line
% Pair of roots the line should connect
point_1 = roots(2);
point_2 = roots(6);
% Coordinates of pair of roots
x1 = real(point_1);
x2 = real(point_2);
y1 = imag(point_1);
y2 = imag(point_2);
d = sqrt((x1-x2)^2 (y1-y2)^2); % Euclidean distance between pair of roots
m = (y1 - y2) / (x1 - x2); % Gradient of line connecting pair of roots
c = y1 - m * x1; % y-intercept of line
int = -c / m; % x-coordinate that the rotation should occur at
shift = [int; 0];
x = linspace(-1, 1, 10); % Initial line lying purely on x-axis
y = 0 * x;
v = [x; y];
theta = atan((y2-shift(2))/(x2-shift(1))); % Angle by which to rotate
rot = [cos(theta), -sin(theta); sin(theta), cos(theta)]; % Rotation matrix
u = v * (d / 2); % Scale initial line
if m < 1e-3 % Horizontal case
shift = [0; 0];
end
w = (rot * (u - shift)) shift; % Apply rotation
% Another shift that seems necessary
% This is definitely a problematic section
shift_x = w(1, 1) - x2;
shift_y = w(2, 1) - y2;
shift_2 = [shift_x; shift_y];
w = w - shift_2;
%% Plot
fig = figure;
fig.Units = 'inches';
fig.Position = [1, 1, 9, 9];
ax = gca;
tt = title(ax, 'Title');
tt.FontWeight = 'bold';
tt.FontSize = 20;
st = subtitle(ax, sprintf('More text here'));
st.FontAngle = 'italic';
st.FontSize = 15;
hold on
hCircle = viscircles([0, 0], 1, 'Color', 'k');
for i = 1 : n
x_point = real(roots(i));
y_point = imag(roots(i));
hPin = plot(x_point, y_point, 'Marker', 'o', 'MarkerSize', 20, 'MarkerfaceColor', 'red', ...
'MarkerEdgeColor', 'black');
end
% Plot original and shifted line, split into colours so direction is easier to see
plot(v(1,1:4), v(2,1:4), 'b');
plot(v(1,4:7), v(2,4:7), 'r');
plot(v(1,7:end), v(2,7:end), 'g');
plot(w(1,1:4), w(2,1:4), 'b');
plot(w(1,4:7), w(2,4:7), 'r');
plot(w(1,7:end), w(2,7:end), 'g');
例如,保持point_1 = roots(2);和更改僅point_2 = roots(p);在p = 3、4、6、7、8 時按預期作業。
任何有關如何使這項作業的指導將不勝感激,謝謝!
編輯:
為了提供更多細節,基本上我有一個介于 0 和 1(而不僅僅是一條線)之間的數字陣列,我想在連接兩個根的線上繪制它們。例如,如果我的陣列是x=[0.2, 0.5, 0.9],那么我想要point_1和之間的三個點point_2,第一個點是距離 0.2d 的連接線point_1,第二個 0.5d(即中途),最后一個是 0.9d 距離。
uj5u.com熱心網友回復:
首先,由于您要連接的點是復數,因此直接使用復坐標更容易。
統一的根源
我已經稍微簡化了您的代碼。由于是內置的 matlab 函式,因此
有些人可能會在命名變數時引發標志。我很好,只要用法不會引起任何混淆,即不要在同一背景關系中用作變數和函式。
由于 matlab 提供了如此多的內置函式,因此無法避免名稱沖突,除非在命名每個變數之前都熟記或搜索。rootsrootsroots
n = 10;
k = 1:n;
roots = exp(2 * k * pi * 1i / n);
縮放、旋轉和平移
% Pair of roots the line should connect
point_1 = roots(2);
point_2 = roots(6);
d = abs(point_2 - point_1); % distance between pair of roots
theta = angle(point_2 - point_1); % rotation angle
p_on_line = linspace(-1, 1, 10); % Initial line lying on x-axis
p_on_line = p_on_line * d/2; % scale
p_on_line = p_on_line * exp(1i*theta); % rotate
p_on_line = p_on_line (point_1 - p_on_line(1)); % translate
情節
我添加了一些散點并洗掉了不相關的部分(例如標題、字體)。
fig = figure;
fig.Units = 'inches';
fig.Position = [1, 1, 9, 9];
hold on
hCircle = viscircles([0, 0], 1, 'Color', 'k');
hPin = plot(roots, 'o', 'MarkerSize', 20, 'MarkerfaceColor', 'red', ...
'MarkerEdgeColor', 'black');
% Plot line connecting roots
plot(p_on_line(1:4), 'b.-', 'MarkerSize', 20);
plot(p_on_line(4:7), 'r.-', 'MarkerSize', 20);
plot(p_on_line(7:end), 'g.-', 'MarkerSize', 20);
% Plot original line
original_x = linspace(-1, 1, 10);
original_y = zeros(1, 10);
plot(original_x(1:4), original_y(1:4), 'b.-', 'MarkerSize', 20);
plot(original_x(4:7), original_y(4:7), 'r.-', 'MarkerSize', 20);
plot(original_x(7:end), original_y(7:end), 'g.-', 'MarkerSize', 20);
hold off
這應該適用于根對的所有組合。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/363401.html
