In: Computer Science
Write a function called HW5_P1 that accepts 1 input argument: an
array, a. The function should output an array, b, that is computed
as: b=3a+5.
Write a MATLAB function called “fit_line” that accepts 2 input
arguments: a column vector of x data and a column vector of y data.
The nth element in the input arguments should correspond to the nth
Cartesian data point i.e. (xn,yn). The function should compute and
return 2 outputs: the slope, m, and the y intercept, b, of a
straight line (y=mx+b) that “best-fits” the data points.
a = 1:1:10; %create random array from 1->10
b = HW5_P1(a) %function call
function b = HW5_P1(a)
b = 3*a + 5;
end
%output:
clc;
x = 1:1:100; %create random array from 1->10
y = 1:2:200;
[m b] = fit_line(x,y) %function call
function [m b] = fit_line(x,y)
P = polyfit(x,y,1);
m = P(1); %slope m
b = P(2); %intercept b
end
%output: