Dont know what Polyval is here doing :-/

1 visualización (últimos 30 días)
Daniel Wurster
Daniel Wurster el 31 de Mayo de 2019
Comentada: dpb el 1 de Jun. de 2019
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
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

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by