필터 지우기
필터 지우기

I ask for help please colleagues

조회 수: 3 (최근 30일)
Milagre MANHIQUE
Milagre MANHIQUE 2022년 1월 2일
답변: Milagre MANHIQUE 2022년 1월 6일
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
Image Analyst
Image Analyst 2022년 1월 3일
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.
Milagre MANHIQUE
Milagre MANHIQUE 2022년 1월 5일
I used the polyfit command as you advised me, it gave me some way to solve my problem. With this command it seems to me that there is a light at the bottom of the tunnel.
Thanks for the tip, it's been useful
Milagre MANHIQUE

댓글을 달려면 로그인하십시오.

답변 (1개)

Milagre MANHIQUE
Milagre MANHIQUE 2022년 1월 6일
Using polyfit as I was advised, I ended up finding this solution, it gave what I really wanted, but I found it to be a somewhat rudimentary or primitive way, I believe it is possible to use loops (for, while...) and produce the same solution.I need to improve my skills in using loops, but if anyone can give a tip, I would welcome and appreciate.
Thank you for help
The code is:
load manufacturerdata
load winddata.mat %Reference wind speed, v (in km/h).
% Manufacturer data
x=manufacturerdata.windspeed;
y=manufacturerdata.power;
p1 = polyfit(x,y,1);
p2 = polyfit(x,y,2);
p3 = polyfit(x,y,3);
p4 = polyfit(x,y,4);
p5 = polyfit(x,y,5);
p6 = polyfit(x,y,6);
p7 = polyfit(x,y,7);
p8 = polyfit(x,y,8);
p9 = polyfit(x,y,9);
p10 = polyfit(x,y,10);
f1 = polyval(p1,x);
f2 = polyval(p2,x);
f3 = polyval(p3,x);
f4 = polyval(p4,x);
f5 = polyval(p5,x);
f6 = polyval(p6,x);
f7 = polyval(p7,x);
f8 = polyval(p8,x);
f9 = polyval(p9,x);
f10 = polyval(p10,x);
plot(x,y,'bp',x,f1,'k:',x,f2,'.-',x,f3,x,f4,x,f5,x,f6,...
x,f7,x,f8,x,f9,x,f10)
title('Output power fitting curves for manufacturer data');
xlabel('Wind speed (in m/s)');
ylabel('Output Power (in kW)');
ylim([-50 1000])
legend('data','1^{st} degree','2^{nd} degree','3^{rd} degree',...
'4^{th} degree','5^{th} degree','6^{th} degree','7^{th} degree',...
'8^{th} degree','9^{th} degree','10^{th} degree','location','southeast')

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by