Math Lab Application Program (24 pts) If 2 data streams fxl, x2, ..xN), and tyl,
ID: 3168439 • Letter: M
Question
Math Lab Application Program (24 pts) If 2 data streams fxl, x2, ..xN), and tyl, y,2,..N) have been given, the best fit least squares algorithm can be used to find the slope "m" and y intercept "b" as follows, x,y, IM where = A"B 2 x 2 Assume the data streams are input from the screen written in a matlab script file called main.m defined by the following four lines, >>clear >>x-input("Input the N data points of the data stream x ‘); >>y-input('Input the N data points of the data stream y ‘); sIm b] - mainfcn (x, y)Explanation / Answer
function [m b]=mainfcn(x,y)
N=length(y);
for i=1:N
a1(i)=x(i)^2;
a2(i)=x(i);
a3=N;
b1(i)=y(i)*x(i);
b2(i)=y(i);
end
% matrix coefficeints
A=[sum(a1) sum(a2); sum(a2) (a3)];
% matrix B
B=[sum(b1) sum(b2)];
a=AB';
m=a(1);
b=a(2);
end
clc;
clear all
x=input('input N data points of the stream x')
y=input('input N data points of the stream y')
[m b]=mainfcn(x,y)
input N data points of the stream x[1 2 3 4 5]
x =
1 2 3 4 5
input N data points of the stream y[1.5 2 3 4 5]
y =
Columns 1 through 4
1.500000000000000 2.000000000000000 3.000000000000000 4.000000000000000
Column 5
5.000000000000000
m =
0.899999999999999
b =
0.400000000000002