Question: code for function: clc;clear all;format short x=[1:10]; y=5.*x; [a b r]=LinearRegression(x,y) function [a b r]=LinearRegression(x,y) %Function which takes x and y & computes the parameters
![code for function: clc;clear all;format short x=[1:10]; y=5.*x; [a b r]=LinearRegression(x,y)](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f4582d02c6c_38866f4582c6d0d0.jpg)
![function [a b r]=LinearRegression(x,y) %Function which takes x and y & computes](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f4582db41f1_38966f4582d561bd.jpg)
code for function:
clc;clear all;format short x=[1:10]; y=5.*x; [a b r]=LinearRegression(x,y) function [a b r]=LinearRegression(x,y) %Function which takes x and y & computes the parameters a,b and r nx=length(x); %Finds length of x ny=length(y); %Finds length of y if(nx~=ny) %If lenght is not same we cannot proceed disp('Error, the number of elements must be same in both'); a='Error'; b='Error'; else %Otherwise we compute each term in the given formula and evaluate the terms n=length(x); Sx=sum(x); Sy=sum(y); Syy=sum(y.^2); Sxy=sum(x.*y); Sxx=sum(x.^2); b=(n.*Sxy-Sx.*Sy)./(n.*Sxx.^2-Sx.^2); a=(Sy.)-(b.*Sx).; r=(n.*Sxy-Sx.*Sy)./sqrt((n.*Sxx.^2-Sx.^2).*((n.*Syy.^2-Sy.^2))); end end
a = 27.4847
b = 0.0028
r = 0.00011144
Write a function (using the function keyword) that: Accepts two vectors (x) and (y) as input. We will later call this function with x representing height and y representing weight. Calculates the the slope (b) and intercept (a) of the regression line (ie., the best fitting line through the points defined by x, y coordinates) according to equations below . Calculates the correlation coefficient (r) for the two variables according to equation below. Do not use the built-in Matlab function for correlation (but note that you could check your code by comparing your results with Matlab's correlation function) Returns the values of a,b, and r to the main program . Complete the calculations with matrix algebra -- not loops. Take special care to use element-wise operations (e.g.. and.) For a regression line, we have: y = a +bx Where the slope (b), intercept (a) of the regression line can be calculated as: 11 12 1-1 b 12 Xx, - Ex, 1! b 12 n Note that n represents the number of observations and the capital sigma represents summation (if you have never seen this notation, read this wiki pagec). The correlation coefficient (r) can be calculated as follows: 17 1-1 1-1 11 To reduce error in converting these equations to Matlab, try to avoid having one long equation on one line. It helps to break the equations (especially the equation for r) into smaller equations and save these to temporary variables. Hint many of the calculations in these formulas imply "element- by-element" multiplication or raising each individual element of a vector to some power. There is specific Matlab notation you have to use to force the element-by-element computation. . . Within the main program: . Use the estimates of b and a to generate a regression line Plot the regression line on a 2-dimensional graph (remember that x is height, y is weight) Add the original data to the graph as points Label the x and y axis Paste the code for your main program below (not the resulting image). You are not allowed to use "Isline" . Edit View Insert Format Tools Table 12pt Paragraph
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
