Hi, and happy new year.
I need a 3rd order polynomiial approximation for the file Data001 in attached.
I can use polyfit but I need a polinomium without the term x^2.
My polinomium must be:
x^3+x^1+x^0.
How can I do that?

 Respuesta aceptada

Star Strider
Star Strider el 1 de En. de 2020

1 voto

Use the mldivide,\ function with a specific design matrix (since this is a linear problem):
D = load('Data001.mat');
x = D.Data001(:,1);
y = D.Data001(:,2);
DesignMatrix = @(x) [x.^3 x ones(size(x))];
B = DesignMatrix(x) \ y;
xv = linspace(min(x), max(x), 1000);
yv = DesignMatrix(xv(:)) * B;
figure
plot(x, y, '.')
hold on
plot(xv(:), yv(:), '-r')
hold off
grid
xlabel('x')
ylabel('y')
legend('Data001', 'Regression Fit')
producing:
The ‘B’ vector are the respective regression coefficients, so that y(x) = B(1)*x^3 + B(2)*x + B(3).

3 comentarios

Paul Rogers
Paul Rogers el 1 de En. de 2020
oh my god, thank you. One thing I asked wrong,
I need x^3+x^2+x^0. The one I don't need is the therm x^1
Star Strider
Star Strider el 1 de En. de 2020
My pleasure!
I was confused by your original Question, since you only left out ‘x^2’.
To leave out ‘x^1’, change ‘DesignMatrix’ to:
DesignMatrix = @(x) [x.^3 x.^2 ones(size(x))];
Also, your vectors are column vectors here, and this will only work with column vectors. To enforce that if you have row vectors, the ‘B’ calculation becomes:
B = DesignMatrix(x(:)) \ y(:);
since the ‘(:)’ will force ‘x’ and ‘y’ to become column vectors.
With that change, the ‘B’ vector are the respective regression coefficients, so that y(x) = B(1)*x^3 + B(2)*x^2 + B(3).
Star Strider
Star Strider el 1 de En. de 2020
If my Answer helped you solve your problem, please Accept it!

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 1 de En. de 2020

0 votos

Without toolbox, use backslash operator, \
X=[x.^[3 2 1 0]]; X(:,2)=0; % design matrix straight calculation
b=X\y; % solve for coefficients
If have Stat or Curve Fitting TB, use one of the linear model fitting routines with custom model.

Categorías

Más información sobre Linear and Nonlinear Regression en Centro de ayuda y File Exchange.

Productos

Versión

R2014b

Etiquetas

Preguntada:

el 1 de En. de 2020

Comentada:

el 1 de En. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by