Second order polynomial curve of best fit
57 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
I'm trying to fit a second order polynomial curve into a scatter plot but all I'm getting is a straight line. Why is this? And how can I fix this?
Thanks
close all
clear all
clc
data = xlsread('Book1.xlsx'); %experimental data
data_B = data(:,2); %column 2 is angle [degrees]
data_D = data(:,4); %column 2 is radius [degrees]
x = data_D;
y = data_B;
constant = lsqcurvefit(@f3,[0;0;0],x,y); %curve of best fit for second order polynomial
%equation of the line is y=a(x-b)^2+c
a = constant (1); %constant a in the function file
b = constant (2); %constant b in the function file
c = constant (3); %constant c in the function file
xfit = 0:0.0001:1.2e-3; %initialRadius:stepRadius:finalRadius
yfit = f3(constant,xfit); %f3 is the function file
figure
plot(x,y,'b*')
hold on
plot(xfit,yfit,'r','linewidth',2)
grid on
xlabel('Radius(m)')
ylabel('Angle (^\circ\theta)')
title ('Angle vs Radius')
legend('Experimental', 'Polynomial Best Fit', 'Location', 'Northwest');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%the function is a seperate file. I just pasted it as one code on the forum for the ease
function y = f3(constant,x)
y = constant(1)*(x-constant(2)).^2+constant(3); %y =a(x-b)^2+c
end
0 个评论
采纳的回答
Matt J
2020-7-23
编辑:Matt J
2020-7-24
It probably happens because you've chosen too arbitrary an initial guess constant=[0;0;0]. It's clear from your data that these are nowhere near the correct coefficients. Regardless, for such a simple polynomial fit, it makes more sense to use polyfit, which does not require any iterative search.
p= polyfit(x,y,2);
xfit = 0:0.0001:1.2e-3; %initialRadius:stepRadius:finalRadius
yfit = polyval(p,xfit);
However, your data does not appear to fit a quadratic model very well...
更多回答(1 个)
John D'Errico
2020-7-23
You need to understand, this data is NOT something a quadratic polynomial will ever fit reasonably.
plot(x,y,'o')
Don't forget that isolated data point at (0,0).
I'm sorry, but that is simply not the shape of a polynomial. For ANY polynomial. Certainly not a quadratic polynomial. Just wanting to fit a quadratic to it won't help.
If anything, this curve appears to be vaguely hyperbolic, that is, asymptotic to straight lines along each wing of the curve.
Worse, that isolated data point at (0,0) will cause problems. It seems to be inconsistent with the rest of the data.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!