How can I fit a curve to x, y points and obtain the regression?

조회 수: 6 (최근 30일)
Sarah
Sarah 2018년 11월 19일
댓글: Sarah 2018년 11월 20일
I have plotted x vs y and obtained a plot of points, now I'm trying to fit a curve to my data using a nonlinear polynomial of order 4, the coeficiants are unknown.
I aim to obtain the regression coefficient as well.
Any idea how it is possible to do this? what are the suitable matlab functions to plot the fitting curve and to obtain the regression coeficient?
I already tried polyfit, but is it correct? if yes then how to proceed?

답변 (3개)

KSSV
KSSV 2018년 11월 19일
편집: KSSV 2018년 11월 19일
Yes polyfit is the function you need.
x = linspace(0,4*pi,50);
y = sin(x);
% Use polyfit to fit a 4th-degree polynomial to the points.
p = polyfit(x,y,7);
% Evaluate the polynomial on a finer grid and plot the results.
x1 = linspace(0,4*pi);
y1 = polyval(p,x1);
figure
plot(x,y,'o')
hold on
plot(x1,y1)
hold off
In the above p has your coefficients. YOu can use poly2sym to see the polynomial obtained.
  댓글 수: 11
KSSV
KSSV 2018년 11월 20일
Then using polyfit is apt.
Sarah
Sarah 2018년 11월 20일
so i tried this as below, but the resulting fitting does not apear same as it is when done on excel.
x =[6 values input by the user ]
y = [6 values calculated ]
so x and y are the same size
then:
p = polyfit(x, y, 4);
val = polyval(p, x);
plot(x, y, 'o');
hold on;
plot(x, val);
hold off

댓글을 달려면 로그인하십시오.


madhan ravi
madhan ravi 2018년 11월 19일
Read interp1 and use appropriate method you want
x = linspace(0,4*pi,50);
y = sin(x);
xx = linspace(x(1),x(end),1000);
yy = interp1(x,y,xx,'spline')
plot(x,y,'o',xx,yy)
  댓글 수: 3
madhan ravi
madhan ravi 2018년 11월 19일
Anytime :) , polyfit is for polynomials
Sarah
Sarah 2018년 11월 19일
then how can i get the regression coeficient if I am sing the interp1 method?

댓글을 달려면 로그인하십시오.


Luna
Luna 2018년 11월 19일
편집: Luna 2018년 11월 19일
Hello Sarah,
you can use polyfit. It uses least squares, here is the link you can read about it:

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by