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

2 ビュー (過去 30 日間)
Abhishek Tyagi
Abhishek Tyagi 2021 年 4 月 29 日
回答済み: Hiro Yoshino 2021 年 4 月 29 日
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.

回答 (1 件)

Hiro Yoshino
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.

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by