How to find best fit regression line for each of these data sets
3 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Hiro Yoshino
2021-4-29
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!