Clear Filters
Clear Filters

Distance between 2 curves from one point

3 views (last 30 days)
David Sicic
David Sicic on 27 Sep 2023
Answered: Star Strider on 27 Sep 2023
Hello,
My aim is to plot the distance between two graphs from point x=150 in x-axis. The distance between the 2 graphs in y-axis is no problem as i just have to subtract the y-values at x=150. But how do i specifically get the distance between graph A from x=150 to the next nearby point of graph B at same y- value. I hope the figure shown below helps you understand my problem.
% Gegebene Daten
x = [29.069, 44.395, 59.528, 74.76, 90.036];
y = [0.07091, 0.076187, 0.081665, 0.087588, 0.093687];
% Quadratische Regression
quadratic_coefficients = polyfit(x, y, 2); % Quadratisches Polynom (ax^2 + bx + c)
% Erzeugen Sie x-Werte für die quadratische Anpassung im erweiterten Bereich
x_values_quadratic = linspace(0, 200, 1000);
% Berechnen Sie die y-Werte für die quadratische Anpassung
y_quadratic = polyval(quadratic_coefficients, x_values_quadratic);
% Lineare Regression für die gemittelte Gerade
x_mean = mean(x); % Durchschnitt der x-Werte
y_mean = mean(y); % Durchschnitt der y-Werte
slope = sum((x - x_mean) .* (y - y_mean)) / sum((x - x_mean).^2); % Steigung
intercept = y_mean - slope * x_mean; % y-Achsenabschnitt
% Erzeugen Sie x-Werte für die gemittelte lineare Anpassung im erweiterten Bereich
x_values_linear = linspace(0, 200, 1000);
% Berechnen Sie die y-Werte für die gemittelte lineare Anpassung
y_linear = slope * x_values_linear + intercept;
% Punkt x = 150
x_point = 150;
y_quadratic_at_150 = polyval(quadratic_coefficients, x_point);
y_linear_at_150 = polyval([slope, intercept], x_point); % Lineare Anpassung bei x = 150
% Abstand in x-Richtung und y-Richtung am Punkt x = 150
distance_x = x_point - x(end); % Abstand in x-Richtung
distance_y = y_quadratic_at_150 - y_linear_at_150; % Abstand in y-Richtung
% Plot der quadratischen und linearen Anpassungen sowie der gegebenen Daten
plot(x, y, 'o', 'MarkerSize', 8, 'MarkerFaceColor', 'b', 'MarkerEdgeColor', 'b');
hold on;
plot(x_values_quadratic, y_quadratic, '-r', 'LineWidth', 2);
plot(x_values_linear, y_linear, '-g', 'LineWidth', 2);
% Vertikale Linie im Abstand in x-Richtung
plot([x_point, x_point], [min([y_linear, y_quadratic]), max([y_linear, y_quadratic])], '--k', 'LineWidth', 1);
% Horizontale Linie im Abstand in y-Richtung
plot([min(x_values_quadratic), max(x_values_quadratic)], [y_linear_at_150, y_linear_at_150], '--b', 'LineWidth', 1);
xlabel('X-Achse');
ylabel('Y-Achse');
title('Quadratische und Gemittelte Lineare Anpassungen der Daten (Bereich von 0 bis 200)');
legend('Datenpunkte', 'Quadratische Anpassung', 'Gemittelte Lineare Anpassung', 'Abstand in x', 'Abstand in y');
grid on;
hold off;
% Quadratische Abweichung (residuale Summe der Quadrate) für die quadratische Anpassung
residuals_quadratic = y - polyval(quadratic_coefficients, x);
quadratic_deviation = sum(residuals_quadratic.^2);
% Lineare Abweichung (residuale Summe der Quadrate) für die gemittelte lineare Anpassung
residuals_linear = y - (slope * x + intercept);
linear_deviation = sum(residuals_linear.^2);
% Geben Sie die quadratische und lineare Abweichung aus
fprintf('Quadratische Abweichung: %.6f\n', quadratic_deviation);
fprintf('Lineare Abweichung (gemittelte Gerade): %.6f\n', linear_deviation);
fprintf('Abstand in x-Richtung bei x = 150: %.6f\n', distance_x);
fprintf('Abstand in y-Richtung bei x = 150: %.6f\n', distance_y);

Answers (2)

Dyuman Joshi
Dyuman Joshi on 27 Sep 2023
% Gegebene Daten
x = [29.069, 44.395, 59.528, 74.76, 90.036];
y = [0.07091, 0.076187, 0.081665, 0.087588, 0.093687];
% Quadratische Regression
quadratic_coefficients = polyfit(x, y, 2);
% Quadratisches Polynom (ax^2 + bx + c)
% Erzeugen Sie x-Werte für die quadratische Anpassung im erweiterten Bereich
x_values_quadratic = linspace(0, 200, 1000);
% Berechnen Sie die y-Werte für die quadratische Anpassung
y_quadratic = polyval(quadratic_coefficients, x_values_quadratic);
% Lineare Regression für die gemittelte Gerade
x_mean = mean(x); % Durchschnitt der x-Werte
y_mean = mean(y); % Durchschnitt der y-Werte
slope = sum((x - x_mean) .* (y - y_mean)) / sum((x - x_mean).^2); % Steigung
intercept = y_mean - slope * x_mean; % y-Achsenabschnitt
% Erzeugen Sie x-Werte für die gemittelte lineare Anpassung im erweiterten Bereich
x_values_linear = linspace(0, 200, 1000);
% Berechnen Sie die y-Werte für die gemittelte lineare Anpassung
y_linear = slope * x_values_linear + intercept;
% Punkt x = 150
x_point = 150;
y_quadratic_at_150 = polyval(quadratic_coefficients, x_point);
y_linear_at_150 = polyval([slope, intercept], x_point); % Lineare Anpassung bei x = 150
%% Modifications
%Equation of the curve as a function handle
f = @(x) polyval(quadratic_coefficients,x);
%Solve for the x value where f(x) is the same value
%as the y value of linear curve at x=150
x0 = fzero(@(x) f(x) - y_linear_at_150, x_point)
x0 = 139.7594
%Get the distance
distance_x = x_point - x0
distance_x = 10.2406
distance_y = y_quadratic_at_150 - y_linear_at_150; % Abstand in y-Richtung
%Draw the x-distance
plot([x0 x_point],y_linear_at_150 + [0 0],'Color','magenta','LineWidth',3.5)
hold on;
% Plot der quadratischen und linearen Anpassungen sowie der gegebenen Daten
plot(x, y, 'o', 'MarkerSize', 8, 'MarkerFaceColor', 'b', 'MarkerEdgeColor', 'b');
plot(x_values_quadratic, y_quadratic, '-r', 'LineWidth', 2);
plot(x_values_linear, y_linear, '-g', 'LineWidth', 2);
% Vertikale Linie im Abstand in x-Richtung
plot([x_point, x_point], [min([y_linear, y_quadratic]), max([y_linear, y_quadratic])], '--k', 'LineWidth', 1);
% Horizontale Linie im Abstand in y-Richtung
plot([min(x_values_quadratic), max(x_values_quadratic)], [y_linear_at_150, y_linear_at_150], '--b', 'LineWidth', 1);
xlabel('X-Achse');
ylabel('Y-Achse');
title('Quadratische und Gemittelte Lineare Anpassungen der Daten (Bereich von 0 bis 200)');
legend('Abstand','Datenpunkte', 'Quadratische Anpassung', 'Gemittelte Lineare Anpassung', 'Abstand in x', 'Abstand in y','FontSize',8);
grid on;
hold off;

Star Strider
Star Strider on 27 Sep 2023
Using interp1
% Gegebene Daten
x = [29.069, 44.395, 59.528, 74.76, 90.036];
y = [0.07091, 0.076187, 0.081665, 0.087588, 0.093687];
% Quadratische Regression
quadratic_coefficients = polyfit(x, y, 2); % Quadratisches Polynom (ax^2 + bx + c)
% Erzeugen Sie x-Werte für die quadratische Anpassung im erweiterten Bereich
x_values_quadratic = linspace(0, 200, 1000);
% Berechnen Sie die y-Werte für die quadratische Anpassung
y_quadratic = polyval(quadratic_coefficients, x_values_quadratic);
% Lineare Regression für die gemittelte Gerade
x_mean = mean(x); % Durchschnitt der x-Werte
y_mean = mean(y); % Durchschnitt der y-Werte
slope = sum((x - x_mean) .* (y - y_mean)) / sum((x - x_mean).^2); % Steigung
intercept = y_mean - slope * x_mean; % y-Achsenabschnitt
% Erzeugen Sie x-Werte für die gemittelte lineare Anpassung im erweiterten Bereich
x_values_linear = linspace(0, 200, 1000);
% Berechnen Sie die y-Werte für die gemittelte lineare Anpassung
y_linear = slope * x_values_linear + intercept;
% Punkt x = 150
x_point = 150;
y_quadratic_at_150 = polyval(quadratic_coefficients, x_point);
y_linear_at_150 = polyval([slope, intercept], x_point); % Lineare Anpassung bei x = 150
% Abstand in x-Richtung und y-Richtung am Punkt x = 150
distance_x = x_point - x(end); % Abstand in x-Richtung
distance_y = y_quadratic_at_150 - y_linear_at_150; % Abstand in y-Richtung
quadratic_x_150 = interp1(y_quadratic, x_values_quadratic, y_linear_at_150); % Desired Quadratic X-Value
Delta_x = 150 - quadratic_x_150; % Delta x
% Plot der quadratischen und linearen Anpassungen sowie der gegebenen Daten
plot(x, y, 'o', 'MarkerSize', 8, 'MarkerFaceColor', 'b', 'MarkerEdgeColor', 'b');
hold on;
plot(x_values_quadratic, y_quadratic, '-r', 'LineWidth', 2);
plot(x_values_linear, y_linear, '-g', 'LineWidth', 2);
% Vertikale Linie im Abstand in x-Richtung
plot([x_point, x_point], [min([y_linear, y_quadratic]), max([y_linear, y_quadratic])], '--k', 'LineWidth', 1);
% Horizontale Linie im Abstand in y-Richtung
plot([min(x_values_quadratic), max(x_values_quadratic)], [y_linear_at_150, y_linear_at_150], '--b', 'LineWidth', 1);
xlabel('X-Achse');
ylabel('Y-Achse');
title('Quadratische und Gemittelte Lineare Anpassungen der Daten (Bereich von 0 bis 200)');
legend('Datenpunkte', 'Quadratische Anpassung', 'Gemittelte Lineare Anpassung', 'Abstand in x', 'Abstand in y');
grid on;
hold off;
% Added 'text' Call To Display 'Delta x' —
text(mean([quadratic_x_150 150]), y_linear_at_150, sprintf('\\leftarrow\\Delta x = %2.2f', Delta_x), 'Vert','middle', 'Horiz','lef', 'Rotation',-45)
ans = 144.8797
% Quadratische Abweichung (residuale Summe der Quadrate) für die quadratische Anpassung
residuals_quadratic = y - polyval(quadratic_coefficients, x);
quadratic_deviation = sum(residuals_quadratic.^2);
% Lineare Abweichung (residuale Summe der Quadrate) für die gemittelte lineare Anpassung
residuals_linear = y - (slope * x + intercept);
linear_deviation = sum(residuals_linear.^2);
% Geben Sie die quadratische und lineare Abweichung aus
fprintf('Quadratische Abweichung: %.6f\n', quadratic_deviation);
Quadratische Abweichung: 0.000000
fprintf('Lineare Abweichung (gemittelte Gerade): %.6f\n', linear_deviation);
Lineare Abweichung (gemittelte Gerade): 0.000000
fprintf('Abstand in x-Richtung bei x = 150: %.6f\n', distance_x);
Abstand in x-Richtung bei x = 150: 59.964000
fprintf('Abstand in y-Richtung bei x = 150: %.6f\n', distance_y);
Abstand in y-Richtung bei x = 150: 0.004951
.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!