Second order polynomial curve of best fit

56 ビュー (過去 30 日間)
asd ad
asd ad 2020 年 7 月 23 日
コメント済み: asd ad 2020 年 7 月 24 日
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

採用された回答

Matt J
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 件のコメント
asd ad
asd ad 2020 年 7 月 24 日
Thanks a lot for your answer

サインインしてコメントする。

その他の回答 (1 件)

John D'Errico
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.
  1 件のコメント
asd ad
asd ad 2020 年 7 月 24 日
Thanks a lot for your input

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by