Line of best fit
2 views (last 30 days)
Show older comments
So I have an assignment where we need to plot data points as a scatterplot and then plot the line of best fit on top of the figure.
I ran into a problem for the last question of the assignment, where we need to do this except with stock closing data that is 252 days long.
Example:
A = [10 1;20 1;30 1;40 1;50 1;]
b = [8;7;4;6;3]
coeff = A\b
new = A*coeff
As shown in the example, we are supposed to solve for the coefficients with left division. However, I do not know how to do this without manually typing out the array with a one at the end of each element.
load fordstock
day = linspace(0,252,252);
scatter(day,ford)
axis([0 300 38 50])
one = ones(1,252,1)
XX = [day one;]
Note: This XX array is where I am struggling since it just prints out all of the days first and then all of the ones.
0 Comments
Answers (1)
Star Strider
on 21 Oct 2017
Use the ones function:
A = [(10:10:50)' ones(5,1)];
b = [8;7;4;6;3];
coeff = A\b
new = A*coeff
so ‘XX’ should be:
XX = [day(:) ones(252,1)];
You have too many arguments in your ‘one’ vector. (The ‘(:)’ subscript forces a column vector.)
0 Comments
See Also
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!