I ask for help please colleagues
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I have a pair of input and output data. My goal is to find a polynomial function that best fits the data. For that I need polynomials of various orders (linear, quadratic, cubic,... up to degree 10). In the matlab program I have, I can get the polynomials one at a time and plot them separately. My problem is: How can I run my program, get the curves of different orders (linear, quadratic, cubic,...), plot them on the same graph and label each curve. I know I should use the "for-loop" but I'm facing difficulties for several days.Can someone help please?
Thanks in advance.
Milagre MANHIQUE
My matlab code is:
clear;
clc;
load manufacturerdata
x=manufacturerdata.windspeed; % Wind speed data in m/s
y=manufacturerdata.power; % Wind turbine output power in Watt.
F=input('Enter the order of the desired polynomial: ');
n=length(x);
N=F+1; % For dimension of matrix A, for Ax=B.
A=zeros(N,N);
for i=1:N
for j=1:N
A(i,j)=sum(x.^(i+j-2));
end
end
B=zeros(N,1);
for k=1:N
B(k)=sum((x.^(k-1)).*y);
end
U=A\B;
Coefficient=[U(2,1) U(1,1)]
% Displaying the desired polynomial
disp('The desired polynomial function fitted to the manufacturer data is: P(x)= ')
for k=N:-1:2
fprintf('+(%.4fx^%d)', U(k), k-1)
end
fprintf('+(%.4f)\n', U(1))
% Plotting the curve fitted to the data
p=flip(U);
x=linspace(x(1),x(n),100);
y=polyval(p,x);
c1=plot(x,y,'-r');
hold on
c2=plot(x,y,'-.');
ylim([0 1000])
title('Output power fitting curve for manufacturer data');
xlabel('Wind speed (in m/s)');
ylabel('Output Power (in kW)');
legend([c1,c2], 'Manufacturer P(v)','Fitted P(v) curve','Location','NorthWest')
2 comentarios
Image Analyst
el 3 de En. de 2022
You forgot to attach manufacturerdata.mat. Why not use polyfit()?
Obviously the highest order polynomial will fit your training data best (lowest residuals) but may not be best for data not used to train it.
Respuestas (1)
Ver también
Categorías
Más información sobre Polynomials en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!