a) Write a function to fit data to a line of the form y = ax + b. Use the follow
ID: 3802499 • Letter: A
Question
a) Write a function to fit data to a line of the form y = ax + b. Use the following equations to obtain the line properties (the function name is myline): a = (sigma x_i)(sigma y_i) - n sigma x_iy_i)/(sigma x_i)^2 -n (sigma x_i^2) b = (sigma x_i)(sigma x_iy_i) - (sigma x_i^2)(sigma y_i)/(sigma x_i)^2 -n (sigma x_i^2) b) Use these four data points (1, 1), (2, 2), (4, 2), (5, 3) to test your function. c) Verify your function by comparing your result with the one obtained by Matlab function polyfit.Explanation / Answer
Matlab function:
function [a,b] = myline(x,y)
n = size(x,2);
a = (sum(x)*sum(y) - n*(sum(x.*y)) ) /( (sum(x))^2 - n*sum(x.*x) );
b = ( sum(x)*(sum(x.*y)) - (sum(x.*x))*sum(y) ) / ((sum(x))^2 - n*sum(x.*x));
end
Sample Output:
x = [1,2,4,5];
y = [1,2,2,3];
p = polyfit(x,y,1)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.