How to connect point by curve instead of line in MTALB plot

조회 수: 12 (최근 30일)
Ram
Ram 2024년 7월 6일
답변: Image Analyst 2024년 7월 7일
Hello, guys. I have some data points. When I plot these points in Matlab, it connects data points by line. However, I want to connect these points using a curve instead of a line graph for good representation. So please suggest to me which function we should use.
Thank you.

답변 (2개)

Hassaan
Hassaan 2024년 7월 6일
이동: Voss 2024년 7월 6일
% Example data points
x = [1, 2, 3, 4, 5];
y = [1, 4, 9, 16, 25];
% Interpolate to create a smooth curve
xq = linspace(min(x), max(x), 100); % Create 100 points for a smoother curve
yq = interp1(x, y, xq, 'spline'); % 'spline' interpolation for a smooth curve
% Plotting the curve
plot(xq, yq, 'b-', x, y, 'ro'); % 'b-' for blue curve, 'ro' for red original points
xlabel('X data');
ylabel('Y data');
title('Smooth Curve Through Data Points');
legend('Interpolated Curve', 'Original Data Points');
  댓글 수: 4
Ram
Ram 2024년 7월 6일
@Walter Roberson . The below figure show that the exact plot . The red mark point i want to connect by curve. They are connected by lines right now.
Hassaan
Hassaan 2024년 7월 6일
% Define the function to compute residuals for circle fitting
function F = fit_circle(c, x, y)
% c(1) and c(2) are the coordinates of the center of the circle
% c(3) is the radius of the circle
F = sqrt((x - c(1)).^2 + (y - c(2)).^2) - c(3);
end
% Example data points (x, y)
x = [-1.7E-05, -3.2E-05, -4.9E-05, -6.1E-05, -7.6E-05, 0.000883, 0.000847, -0.00505, -0.0137, -0.01811, -0.0189, -0.02273, -0.03432, -0.05114, -0.06628, -0.0758, -0.08215, -0.09042, -0.10216, -0.11353, -0.11952, -0.11911, -0.11585, -0.11349, -0.11196, -0.10784, -0.09861, -0.08553, -0.07239, -0.06166, -0.05247, -0.04241, -0.03072, -0.01951, -0.01168];
y = [-0.00016, 2.27E-05, -0.00018, 7.58E-05, -0.00021, 0.009197, -0.00954, -0.04568, -0.0354, -0.00597, -0.00141, -0.03449, -0.07417, -0.08341, -0.05849, -0.03072, -0.02875, -0.04878, -0.06128, -0.04528, -0.01081, 0.014622, 0.015929, 0.006168, 0.008244, 0.030304, 0.056246, 0.066329, 0.056809, 0.04375, 0.042324, 0.052024, 0.057486, 0.04753, 0.025901];
% Initial guess for circle parameters [x_center, y_center, radius]
initial_guess = [0, 0, 0.1];
% Fit the circle using least squares optimization
options = optimoptions('lsqnonlin', 'Display', 'off');
circle_params = lsqnonlin(@(c) fit_circle(c, x, y), initial_guess, [], [], options);
% Calculate points on the fitted circle for plotting
theta = linspace(0, 2*pi, 100);
x_fit = circle_params(1) + circle_params(3) * cos(theta);
y_fit = circle_params(2) + circle_params(3) * sin(theta);
% Plot the original data and the fitted circle
figure;
plot(x, y, 'ro'); hold on; % original data
plot(x_fit, y_fit, 'b-'); % fitted circle
axis equal; % equal scaling
legend('Original Data', 'Fitted Circle');
title('Least Squares Circle Fitting');
xlabel('X');
ylabel('Y');

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2024년 7월 7일
If you want a smooth envelope going through the convex hull points, like you showed in your example, I suggest first using convhull to find those points, and then use a spline, with a lot more "in between" points/locations to interpolate a smooth curve between them. I'm attaching a spline demo for 1-D and 3-D but it's straight forward to first spline the x data and then spline the y data and plot the new (x,y) and it will go between the convex hull points.

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by