Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

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

Popular Posts

Followers