Dont know what Polyval is here doing :-/

Hello Together, i dont know what polyval is doing here with the blue lines:
What i want is a line through all points (like the black one).
untitled.jpg
Code for it is at end.
  • As you can see, the black line with polyval is plotted correct.
  • The points yd1 for xd1 and yd2 for xd2 are plotted correct
  • What is not plotted correct, is the polyval for xd1 and xd2 -> What have i done wrong here?
Thanks a lot!
%Black line
xml1 = [ -0.3, 0.4];
yml1 = [-0.15, -0.15];
xml2 = 0.4 + 0.7 * cos(0:pi/50:pi/2) ;
yml2 = -0.85 +0.7 * sin(0:pi/50:pi/2);
xml3 = [1.1, 1.1];
yml3 = [-0.85, -1.15];
load ('xdata310519.mat')
%xd1 = 0 0.3906 0.7430 1.0227 1.2022 1.2641 1.2022
%yd1 = 0 -0.0619 -0.2414 -0.5211 -0.8735 -1.2641 -1.6547
%xd2 = 0 0.1943 0.3696 0.5087 0.5980 0.6288 0.5980
%yd2 = 0 -0.0308 -0.1201 -0.2592 -0.4345 -0.6288 -0.8231
grad = 5;
p1 = polyfit( xd1, yd1, grad);
p2 = polyfit( xd1, yd2, grad);
f1 = polyval( p1, xd1);
f2 = polyval( p2, xd2);
hold on
%Black line
plot(xml1, yml1, '--k');
plot(xml2, yml2,'--k');
plot(xml3, yml3, '--k');
%points
plot(xd1, yd1,'s', xd2, yd2,'s');
plot(xd1, f1,'b', xd2, f2,'b');

 Respuesta aceptada

dpb
dpb el 31 de Mayo de 2019
Editada: dpb el 31 de Mayo de 2019
You fit yd2 vs xd1 but then plotted against xd2. One presumes the fit should be against xd2 as well.
BUT: A perfect illustration why higher order polynomials are rarely the right fitting function--what your fit really is is shown below--
untitled.jpg
where this was generated from
p2 = polyfit( xd2, yd2, grad); % fit against intended variable
figure
plot(xd2,yd2,'bx') % plot the data points
hold on
plot(xd2,f2,'b-') % and the fit at only those points (linear between)
x2=linspace(min(xd2),max(xd2)); % fill in 100 points between first, last
plot(x2,polyval(p2,x2),'r-') % and plot those as well...
legend('Data','5th Order at X','5th Order at 100X')
A polynomial is an exceedingly bad choice for such data; use an interpolating spline instead or something similar.

5 comentarios

John D'Errico
John D'Errico el 31 de Mayo de 2019
I would add only that the data is highly non-polynomial, although it looks like the relation where x is viewed as a function of y DOES seem to be polynomial in nature.
Daniel Wurster
Daniel Wurster el 1 de Jun. de 2019
Editada: Daniel Wurster el 1 de Jun. de 2019
You fit yd2 vs xd1 but then plotted against xd2. One presumes the fit should be against xd2 as well.
Yes thats right. My bad. I fixed that and it looks now like:
I would add only that the data is highly non-polynomial, although it looks like the relation where x is viewed as a function of y DOES seem to be polynomial in nature.
The data xd1, yd1 and xd2, yd2 is based on sinus and cosinus. So its polynomial.
My overall plan is:
  1. Create datapoints to draw a function through them
  2. Calculate the arc length of the function to compare it.
For 1. i did the following code:
box on; grid on; hold on;
axis ([ -0.5 ,2 , -2 ,2]);
n1 = -pi/2:pi/180:pi/8;
%To get the data points i wrote
%n1 = -pi/2:pi/10:pi/8;
%but to draw it nice i wrote pi/180
i=1:9;
xr1 = 1.2641 * cos(-n1);
yr1 = 1.2641 * sin(-n1) -1.2641;
xr2 = 0.6288 * cos(-n1);
yr2 = 0.6288 * sin(-n1) -0.6288;
h1 = plot(xr1, yr1);
h2 = plot(xr2, yr2);
xd1 = (get(h1, 'XData'))
yd1 = (get(h1, 'YData'))
%save('xdata1.mat','xdata1', 'ydata1');
xd2 = (get(h2, 'XData'))
yd2 = (get(h2, 'YData'))
save('xdata3105.mat','xd1', 'yd1','xd2', 'yd2');
clear all
Which gives me:
For 2. i need to have a polynomial function through the data points to calculte the arc length.
dpb
dpb el 1 de Jun. de 2019
"...is based on sinus and cosinus. So its polynomial"
How do you draw that conclusion??? The illustrated behavior is clearly not a polynomial in x altho as John notes, reversing the dependent/independent variables would leave to something more nearly approaching a quadratic.
I still think splines will be your best approach here for whatever it is you're trying to accomplish.
dpb
dpb el 1 de Jun. de 2019
If it is the length, then John's FEX submission should be just the ticket <fileexchange/34871-arclength>
dpb
dpb el 1 de Jun. de 2019
One last fling with respect to modelling the given shape as polynomial -- after your code above then:
xlim([-0.1 1]); ylim([-1 0.5]) % blow up the region of interest to see more detail
b2=polyfit(xd2,yd2,2); % fit a quadratic in xd2 and add to plot
yhat2=polyval(b2,xd2);
hLY2=plot(xd2,yhat2,'g-');
b2H=polyfit(yd2,xd2,2); % As J d E observed far more nearly polynomic in Y
xhat2=polyval(b2H,yd2); % now we're predicting x instead from y...
hLX2=plot(xhat2,yd2,'k-'); % add this possible approximation as well...
legend('YD1','YD2','Quadratic in XD2','Quadratic in YD2')
As can be seen, then open to left parabola comes at least with the right shape and can produce the symmetry axis in the proper direction but doesn't fit the actual data all that closely.
You could, of course, continue to add terms to try to improve, but still the spline will fit directly and is, overall, the far better option here.
untitled.jpg

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 31 de Mayo de 2019

Comentada:

dpb
el 1 de Jun. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by