Least Squares method and Octave/Matlab

2.3k Views Asked by At

I'll try to be as clear as possible so that you understand what I'm trying to do and can help me

I have twelve pairs of data $(x_1,y_1),....,(x_{12},y_{12})$ and from this data we established a model of polynomial regression trough Least Squares,a model like this $$y=a_0+a_ix+a_2x^2$$

until then I had no problems, but to make the graphs of that model I need more points, and my question is how do I get these points and how do I plot these points with the curve established by my model with the octave / matlab

I have set the coefficients $a_0,a_1,a_2$. The range of points is $x_1=1,x_2=2,..,x_{13}=13$

3

There are 3 best solutions below

3
On BEST ANSWER
% Data
x_data = ...
y_data = ...

% Polynomial fit
p = polyfit(x_data, y_data, 2);

% Plot
N = 42;
x = linspace(x_data(1), x_data(end), N);
y = polyval(p, x);
plot(x,y); legend('Nice plot');
0
On

You can simply create the additional points that you need.

The fitting algorithm provides you with the values $a_0$, $a_1$ and $a_2$. Let's say that $x_m$ is the minimum value across your $x$-points and that $x_M$ is the maximum value. You can get a chart of your fitted curve by doing something like

x = linspace(x_m, x_M); y = a_0 + a_1 * x + a_2 * x .* x; plot(x, y)

0
On

Ok, first choose the range, of your plot. Since your data points go from $1$ to $13$, a reasonable range might be the interval $[0,15]$. Then do the following:

xs = 0;
xe = 15;
x = xs:0.01:xe;
y = a_0 + a_1 * x + a_2 * x.^2;
plot(x,y)