Practice problem on Linear & Quadratic Fit | MATLAB

Problem Statement:
Consider 6 points in a two-dimensional space:
(1, 2), (2, 3), (1,−1), (−1, 3), (1,−2), (0,−1)
Build a MATLAB figure in which the points are represented with their linear and quadratic regression functions.

Prerequisite: Polynomial Regression in MATLAB

%Code:

clc
clear all
close all
x=[1 2 1 -1 1 0];
y=[2 3 -1 3 -2 -1];
a=1;
coeff=polyfit(x,y,a);
t=min(x):0.01:max(x);
c=0;
temp=a;
z=[];
for i=1:length(t)
t1=t(i);
for j=1:length(coeff)
c=c+coeff(j)*(t1^temp);
temp=temp-1;
end
temp=a;
z=[z c];
c=0;
end
plot(t,z,'--');
hold on;
plot(x,y,'o');
a=2;
coeff=polyfit(x,y,a);
t=min(x):0.01:max(x);
c=0;
temp=a;
z=[];
for i=1:length(t)
t1=t(i);
for j=1:length(coeff)
c=c+coeff(j)*(t1^temp);
temp=temp-1;
end
temp=a;
z=[z c];
c=0;
end
plot(t,z);
hold off;



No comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();