The perpendicular point is not on the proposed line.
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;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; 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; polyfit, line MATLAB Answers — New Questions