Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Linear regression! In this lab we will start by learning to use the function provided by matlab to find the root. In this homework, we

Linear regression!
In this lab we will start by learning to use the function provided by matlab to find the root. In this homework, we are going to use linear regression to fit a line to a set of data.
m=ni=1nxiyi-i=1nxii=1nyini=1n(xi)2-(i=1nxi)2
b=i=1nyi-mi=1nxin
Task:
First, you should download the data file from Canvas. You should save the file to the same folder on your computer as your code. The name of the file should be ME208_week6_hwkdata.txt (if Canvas changes it, you should change it back).
We will create a main script and a function. I recommend putting the function in the separate file in the same folder on your computer.
The function will calculate the linear regressions formulas for m and b. Start your function passing in x and y(two arrays of data). You should pass out m and b. The first line of your function should be: function [m,b]= mylinregression (x,y)
The first part of your function should be to create the 4 sums:
i=1nxi
i=1nyi
i=1nxiyi
i=1n(xi)2
Start with 4 variables, sumx, sumy, sumxy, sumxx and set them to zero. Use a for loop that goes from i=1 to i=n to add to the sums.
5. The last part of the function should be to put together m and b using the completed sums and the formula above.
6. Once you have completed the function, you should work on the main script. Start by loading the data from the file and assigning the second column to a variable exam1 and the third to exam2: load ME208_week6_hwkdata.txt
exam1= ME208_week6_hwkdata(:,2);
exam2= ME208_week6_hwkdata(:,3);
7. Send your data to your function to get m and b:[m,b]= mylinregression(exam1,exam2);
8. We want to compare your function with Matlab's built in function polyfit to see if it is giving the right answer. If you send the data to polyfit and fit it to a first order nolvnomial the coefficients of that polynomial should be equal to m and b: p=polyfit(exam1,exam2,1);
9. Using your new equations fitting a line to your data, create some new y data
yfit1=p(1)*exam1+p(2)
yfit2=m*exam1+b;
10. Plot your data using * symbol (exam1 vs exam2) and this fit data (yfit1 and yfit2) using a red line to create two subplots (one for the fit from your function and one for the fit from polyfit).
11. Display your coefficients to the command window:
disp(['The slope (m) was calculated as 'num2str(m)' compared to 'num2str(p(1)) 'using the matlab function'])
disp(['The intercept (b) was calculated as 'num2str(b)' compared to 'num2str(p(2)) 'using the matlab function'])
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions