MATLAB: Curve fitting and scatter plots

scatter plots polynomials splines

Hi all,
This may not be possible but is there a function or a way you can fit a curve to a select number of points within a scatterplot? Looking specifically to fit a curve around the 'lower boundary' of a scatterplot. For example say I had a scatterplot of hundreds of points that made up a circle. Is there a way to fit a curve around the bottom perimeter?
If you need any more info please ask.
Best

Best Answer

Craig, try this to see if it's what you want. I create a bunch of noisy points around a circle. Then I select the bottom half of those. Then I fit a circle through the points, using only those points in the bottom half of the circle to determine the fitted equation. I think that's what you've been saying you want. Save the following in test.m and run it.
function test
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
xCenter = 12;
yCenter = 10;
theta = 0 : 0.01 : 2*pi;
radius = 5 + 2*rand(1, length(theta));
x = radius .* cos(theta) + xCenter;
y = radius .* sin(theta) + yCenter;
plot(x, y, 'b*');
axis square;
xlim([0 20]);
ylim([0 20]);
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Get the bottom half of them
selector = y < yCenter;
bottomHalfY = y(selector);
bottomHalfX = x(selector);
hold on;
plot(bottomHalfX, bottomHalfY, 'ro');
% Fit a circle to the bottom half noisy data:
[xc,yc,R,a] = circfit(x,y)
xFit = R .* cos(theta) + xc;
yFit = R .* sin(theta) + yc;
plot(xFit, yFit, 'r-', 'LineWidth', 3);
function [xc,yc,R,a] = circfit(x,y)
%CIRCFIT Fits a circle in x,y plane
% http://matlab.wikia.com/wiki/FAQ#How_can_I_fit_a_circle_to_a_set_of_XY_data.3F
% [XC, YC, R, A] = CIRCFIT(X,Y)
% Result is center point (yc,xc) and radius R. A is an optional
% output describing the circle's equation:
%
% x^2+y^2+a(1)*x+a(2)*y+a(3)=0
% by Bucher izhak 25/oct/1991
n=length(x); xx=x.*x; yy=y.*y; xy=x.*y;
A=[sum(x) sum(y) n;sum(xy) sum(yy) sum(y);sum(xx) sum(xy) sum(x)];
B=[-sum(xx+yy) ; -sum(xx.*y+yy.*y) ; -sum(xx.*x+xy.*y)];
a=A\B;
xc = -.5*a(1);
yc = -.5*a(2);
R = sqrt((a(1)^2+a(2)^2)/4-a(3));