polyfit in a for loop
12 visualizzazioni (ultimi 30 giorni)
Greig il 22 Feb 2015
Firstly, this is a poorly conditioned problem. You are trying to fit a straight line through a single data point, so there is no unique solution.
I guess you are wanting the slope to pass through the origin? If so, and since you are then only fitting to two points, you want to use
polyfit([0, cos(n^3)], [0, 3*n], 1)
So following the loop approach, you want to use something like..
Data_Fits(n,:) = polyfit([0, cos(n^3)], [0, 3*n], 1);
This can be done without the loop, but this only works for the simple case of passing through the origin...
As a point of note, n ranges up to 270, which looks to me to be an angle in degrees? If so, you should note that cos() assumes radians, so you should use cosd() instead, or convert n to radians. Of course, if this is not what you're doing, you can completely ignore this last point.