solving for coefficients in 4th order polynomial

41 visualizaciones (últimos 30 días)
nctt
nctt el 14 de En. de 2024
Editada: Torsten el 18 de En. de 2024
so in this code i get
Empty sym: 0-by-1
for coefficients so it doesnt give the numerical values i tried double() command but i think i used it wrong can you help me with that?
clear all
clc
syms a4 a3 a2 a1 a0
x=[0:5];
y=[15,10,9,6,2,0];
eqn=a0+a1.*x+a2.*x.^2+a3.*x.^3+a4.*x.^4==y;
[a4,a3,a2,a1,a0]= solve(eqn)

Respuesta aceptada

Shivam
Shivam el 14 de En. de 2024
Hi,
From the provided information, I understand that you are trying to get the coefficients of the 4th-degree polynomial using six sets of x and y values.
You can observe that for the 4th-degree polynomial, you have 5 unknown coefficients (a0, a1, a2, a3, a4), and with 6 data points, it is not the correct approach to solve the equations simultaneously for each x and y pair.
You can use the least squares method, which finds the polynomial coefficients that minimize the sum of the squares of the differences between the observed values (y) and those predicted by the polynomial model.
You can follow the below workaround using 'polyfit' to find the coefficients of a 4th-degree polynomial:
x = [0:5];
y = [15, 10, 9, 6, 2, 0];
% Fit a 4th-degree polynomial to the data
p = polyfit(x, y, 4);
% Display the coefficients
a4 = p(1)
a4 = 0.1875
a3 = p(2)
a3 = -1.9398
a2 = p(3)
a2 = 6.2986
a1 = p(4)
a1 = -9.4272
a0 = p(5)
a0 = 14.9802
You can refer to the following documentation to know more about 'polyfit' function:
I hope it helps.
Thanks
  2 comentarios
nctt
nctt el 18 de En. de 2024
can we do it without curve fitting functions?
Torsten
Torsten el 18 de En. de 2024
Editada: Torsten el 18 de En. de 2024
x=[0:5].';
y=[15,10,9,6,2,0].';
A = [ones(6,1),x,x.^2,x.^3,x.^4];
a = A\y;
a0 = a(1)
a0 = 14.9802
a1 = a(2)
a1 = -9.4272
a2 = a(3)
a2 = 6.2986
a3 = a(4)
a3 = -1.9398
a4 = a(5)
a4 = 0.1875

Iniciar sesión para comentar.

Más respuestas (1)

Hassaan
Hassaan el 14 de En. de 2024
Editada: Hassaan el 14 de En. de 2024
  1. Polyfit: Use polyfit to find the coefficients of the polynomial that best fits your data. The polyfit function finds the coefficients of a polynomial of a specified degree that fits the data in a least-squares sense.
  2. Degree of the Polynomial: In your case, you have 6 data points, so you can fit a polynomial of degree 5 (or less).
If you want to visualize this polynomial against your data, you can use the polyval function to evaluate the polynomial at points along the x-axis and plot it
clear all
clc
x = [0:5];
y = [15,10,9,6,2,0];
% Fit a 5th degree polynomial
p = polyfit(x, y, 5);
% The coefficients are in descending powers
a4 = p(1);
a3 = p(2);
a2 = p(3);
a1 = p(4);
a0 = p(5);
fprintf("%f %f %f %f %f",a0,a1,a2,a3,a4)
-11.750000 10.291667 -4.208333 0.708333 -0.041667
% Evaluate polynomial
x_fit = linspace(min(x), max(x), 100);
y_fit = polyval(p, x_fit);
% Plot
plot(x, y, 'o', x_fit, y_fit, '-');
legend('Data Points', 'Fitted Polynomial');
xlabel('x');
ylabel('y');
title('Polynomial Fit');
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Categorías

Más información sobre Polynomials en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by