Question:
Find Linear Interpolation and Linear Regression for given \(X\) and \(Y\) data points in MATLAB?
Answer:
Linear Interpolation for given \(X\) and \(Y\) data points can be evalued in MATLAB by using built-in command "interp1" and Linear Regression can be obtainbed by solving system of Linear Equations.
x = [0.023 0.05 0.072 0.055 0.013 0.096 0.011]
y = [0.050623 0.0925 0.112608 0.098175 0.030303 0.120192 0.025927]
xi = linspace(min(x),max(x));
yi = interp1(x,y,xi);
A = [numel(x) sum(x);
sum(x) sum(x.^2)];
b = [sum(y);
sum(x.*y)];
var = fliplr((A\b)')
poly2sym(var)
xr = xi;
yr = polyval(var,xi);
yu = polyval(var,x);
err = immse(y,yu)
plot(x,y,'o',xi,yi,':.',xr,yr,'b--',x,yu,'go');
legend('Data Points','Interpolation','Linear Regression','$y = ax + b$','Interpreter','latex')
OUTPUT
x =
0.0230 0.0500 0.0720 0.0550 0.0130 0.0960 0.0110
y =
0.0506 0.0925 0.1126 0.0982 0.0303 0.1202 0.0259
var =
1.1889 0.0214
ans =
(5354402693154885*x)/4503599627370496 + 771401281570945/36028797018963968
err =
9.3126e-05
0 Comments