Borrar filtros
Borrar filtros

The perpendicular point is not on the proposed line.

1 visualización (últimos 30 días)
KUN CHENG
KUN CHENG el 12 de Jun. de 2024
Comentada: Voss el 13 de Jun. de 2024
I would like to ask what is wrong with my code that the green line is not on the fit line?
Now I have to fit two lines to the given coordinates and calculate their perpendicular distance from the center of the circle. Then their slopes will be based on the data_line1.
% 基準綫坐標
data_line1 = readtable("基準綫.csv");
data_line2 = readtable("基準綫2.csv");
x_line1 = data_line1{7:end, 1};
y_line1 = data_line1{7:end, 2};
x_line2 = data_line2{7:end, 1};
y_line2 = data_line2{7:end, 2};
% 圓心坐標
center_x = 37.5;
center_y = 37.5;
% 擬合第一條綫
x1 = x_line1;
y1 = y_line1;
p_line = polyfit(x1, y1, 1);
slope = p_line(1);
intercept = p_line(2);
A = slope;
B = -1;
C = intercept;
x_foot = (center_x - slope * (center_y - intercept)) / (1 + slope^2);
y_foot = slope * x_foot + intercept;
distance = abs(A * center_x + B * center_y + C) / sqrt(A^2 + B^2);
disp('垂直距離1:');
disp(distance);
% 繪製結果
figure;
hold on;
plot(x1, y1, 'bo', 'DisplayName', '原始數據1'); % 原始數據點
plot(center_x, center_y, 'rx', 'MarkerSize', 10, 'DisplayName', '圓心'); % 圓心
% 繪製擬合直線
fitted_x = linspace(min(x_line1), max(x_line1), 100);
fitted_y = polyval(p_line, fitted_x);
plot(fitted_x, fitted_y, 'k--', 'DisplayName', '擬合直線1');
% 繪製垂直距離線
plot([center_x, x_foot], [center_y, y_foot], 'r-', 'DisplayName', '垂直距離1');
% 擬合第二條綫
x2 = x_line2;
y2 = y_line2;
p2_line = polyfit(x2, y2, 1);
intercept_2 = p2_line(2);
C2 = intercept_2;
x_foot_2 = (center_x - slope * (center_y - intercept_2)) / (1 + slope^2);
y_foot_2 = slope * x_foot_2 + intercept_2;
distance_2 = abs(A * center_x + B * center_y + C2) / sqrt(A^2 + B^2);
disp('垂直距離2:');
disp(distance_2);
% 繪製第二條線和垂直距離線
plot(x2, y2, 'go', 'DisplayName', '原始數據2'); % 原始數據點2
fitted_y2 = polyval(p2_line, fitted_x);
plot(fitted_x, fitted_y2, 'b--', 'DisplayName', '擬合直線2');
plot([center_x, x_foot_2], [center_y, y_foot_2], 'g-', 'DisplayName', '垂直距離2');
legend;
xlabel('X軸');
ylabel('Y軸');
title('從圓心到擬合直綫的垂直距離');
grid on;
hold off;

Respuestas (1)

Voss
Voss el 12 de Jun. de 2024
The second calculation (which is for the green line) is using the slope from the first calculation (which is for the blue line) instead of its own slope p2_line(1).
Here I've removed slope, intercept, and intercept_2 and used A and C throughout instead for clarity.
% 基準綫坐標
data_line1 = readtable("基準綫.csv");
data_line2 = readtable("基準綫2.csv");
x_line1 = data_line1{7:end, 1};
y_line1 = data_line1{7:end, 2};
x_line2 = data_line2{7:end, 1};
y_line2 = data_line2{7:end, 2};
% 圓心坐標
center_x = 37.5;
center_y = 37.5;
% 擬合第一條綫
x1 = x_line1;
y1 = y_line1;
p_line = polyfit(x1, y1, 1);
A = p_line(1);
C = p_line(2);
B = -1;
x_foot = (center_x - A * (center_y - C)) / (1 + A^2);
y_foot = A * x_foot + C;
distance = abs(A * center_x + B * center_y + C) / sqrt(A^2 + B^2);
figure;
hold on;
plot(x1, y1, 'bo', 'DisplayName', '原始數據1');
plot(center_x, center_y, 'rx', 'MarkerSize', 10, 'DisplayName', '圓心');
fitted_x = linspace(min(x_line1), max(x_line1), 100);
fitted_y = polyval(p_line, fitted_x);
plot(fitted_x, fitted_y, 'k--', 'DisplayName', '擬合直線1');
plot([center_x, x_foot], [center_y, y_foot], 'r-', 'DisplayName', '垂直距離1');
% 擬合第二條綫
x2 = x_line2;
y2 = y_line2;
p2_line = polyfit(x2, y2, 1);
A = p2_line(1); % this is the critical line that was missing
C = p2_line(2);
x_foot_2 = (center_x - A * (center_y - C)) / (1 + A^2);
y_foot_2 = A * x_foot_2 + C;
distance_2 = abs(A * center_x + B * center_y + C) / sqrt(A^2 + B^2);
plot(x2, y2, 'go', 'DisplayName', '原始數據2');
fitted_y2 = polyval(p2_line, fitted_x);
plot(fitted_x, fitted_y2, 'b--', 'DisplayName', '擬合直線2');
plot([center_x, x_foot_2], [center_y, y_foot_2], 'g-', 'DisplayName', '垂直距離2');
legend;
xlabel('X軸');
ylabel('Y軸');
title('從圓心到擬合直綫的垂直距離');
grid on;
hold off;
  4 comentarios
KUN CHENG
KUN CHENG el 13 de Jun. de 2024
Thank you I have gotten the results I wanted. I realized that I gave the wrong command when I was drawing the graph.
Voss
Voss el 13 de Jun. de 2024
I'm not sure whether my answer helped, but if it did please "Accept" it. Thanks!

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by