# How to find best fit regression line for each of these data sets

3 views (last 30 days)
Abhishek Tyagi on 29 Apr 2021
Answered: Hiro on 29 Apr 2021
You are given four sets of data x and y (eg, x1 and y1, x2 and y2, etc.). You are asked to find a best fit regression line for each of these data sets, and you are told that the relationship between x and y could be linear (y = ao + a1^x), exponential (y = aoe^a1x), a power equation (y = aox^a1.), or a modified Fourier analysis equation (y = ao + a1 cos(x) + a2sin(x2)).
Write a function m-file that:
• receives the vectors x and y as inputs
• uses least squares regression to fit the four models described above. Your code for least squares regression may not use any of MATLAB's built-in regression functions (eg, polyfit), but you may use built-in functions to do common, basic calculations (eg, mean, sum) or to solve linear systems (eg, backslash). Your code must be written to work with any size input data.
• returns as an output the coefficient of determination (R2 ) for all four of the models. Again, you may not use any of MATLAB's built-in regression functions, but you may use built-in functions to do common, basic calculations.
• makes a figure with four plots (ie, using the subplot command), one for each of the four models. The input data points should be shown using a symbol (eg, open circles) and the regression should be plotted as a smooth line.

Hiro on 29 Apr 2021
You should take a look at this first:
You could write everything from scratch but it takes time. MATLAB is with you for this reason.
I would say you can write code for linear problems relatively easily if you have a good understanding on basic linear algebra but non-linear problems often require optimization approaches and it would be another hassle for you.