Matlab学习手记——椭圆拟合

椭圆拟合方程:

结果图:

程序:

clear; clc;
F = @(p, x) p(1) * x(:, 1) .^ 2 + p(2) * x(:, 1) .* x(:, 2) + p(3) * x(:, 2) .^2 + p(4);
% 离散数据点
x = [6.3246, 6.9379, 7.0875, 7.9242, 7.8075, 7.4144,...
7.0113, 6.9972, 5.4084, 3.6725, 2.0887, 0.77134,...
-0.30409, -1.1955, -1.9562, -2.6275, -3.2407,...
-3.82, -4.3847, -4.9512, -5.5328, -6.1370,...
-6.7579, -7.3536, -7.8009, -7.8436, -7.1336,...
-6.9397, -5.8234, -4.5284, -3.2401, -2.0724,...
-1.0584, -0.18885, 0.56017, 1.2139, 1.7943,...
2.3196, 2.8045, 3.2607, 3.6981, 4.1253, 4.5494,...
4.977, 5.4134, 5.8625, 6.3246;...
0, 1.1372, 1.4598, 3.3259, 4.454, 6.6544, 7.1475,...
7.1608, 7.8789, 7.817, 7.3585, 6.2585, 6.1371,...
5.534, 4.9541, 4.1894, 3.8261, 3.2479, 2.6352,...
1.9635, 1.2011, 0.30584, -0.77674, -2.106, -3.7068,...
-5.4597, -6.2233, -7.2127, -7.7935, -7.9183, -7.7237,...
-7.3522, -6.5047, -6.4363, -5.9721, -5.5207, -5.0827,...
-4.6548, -4.2316, -3.307, -3.3735, -2.9232, -2.4463,...
-1.9315, -1.3645, -0.72786,0];
x = x';
p0 = [1, 1, 1, 1];
% 拟合系数,最小二乘法;
p=nlinfit(x, zeros(size(x, 1), 1), F, p0);  % 和 0 比较
plot(x(:, 1), x(:, 2), 'ro');
hold on
xmin = min(x(:, 1));
xmax = max(x(:, 1));
ymin = min(x(:, 2));
ymax = max(x(:, 2));
%作图
ezplot(@(x, y) F(p, [x, y]), [-1 + xmin, 1 + xmax, -1  +ymin, 1 + ymax]);
hold off
title('椭圆曲线拟合');
legend('样本点', '拟合曲线');

猜你喜欢

转载自blog.csdn.net/u012366767/article/details/83092536
今日推荐