Parabola plot not displayed correctly

조회 수: 3 (최근 30일)
Sparkle89
Sparkle89 2019년 7월 18일
편집: Sparkle89 2019년 7월 18일
I am trying to have a quadratic fit to my data.
X = randi(100,[40, 1])
Y = (2* (X.^2)) +(3*X) +(5);
X2 = [ones(size(X)) X X.^2]
m3 = X2 \ Y;
y3 = X2*m3;
scatter(X,Y);
hold on
plot(X,y3)
ylabel('Y')
title('y= ax^2+bx+c', 'FontSize',18)
hold off;
untitled1.jpg
However, when I display the plot istead of a single parabolic fir I am getting multiple lines.
Please help. I experimented this while learning linear regression.

채택된 답변

KSSV
KSSV 2019년 7월 18일
You are suing the random numbers for demo.....you have to sort them before you use:
X = randi(100,[40, 1]) ;
X = sort(X) ;
Y = (2* (X.^2)) +(3*X) +(5);
X2 = [ones(size(X)) X X.^2]
m3 = X2 \ Y;
y3 = X2*m3;
scatter(X,Y);
hold on
plot(X,y3)
ylabel('Y')
title('y= ax^2+bx+c', 'FontSize',18)
hold off;
  댓글 수: 1
Sparkle89
Sparkle89 2019년 7월 18일
편집: Sparkle89 2019년 7월 18일
Thank you.It worked. Can you explain the need for sorting because I could fit a line without sorting?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by