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에서 Linear and Nonlinear Regression에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!