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

7 views (last 30 days)

Show older comments

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.

##### 0 Comments

### Answers (2)

Hassaan
on 6 Jul 2024

Moved: Voss
on 6 Jul 2024

% 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 Comments

Hassaan
on 6 Jul 2024

% 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
on 7 Jul 2024

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!