Approximating unit circle with a cubic spline
Mostrar comentarios más antiguos
Hey all. We're covering cubic splines in class and I've been messing around in Matlab a little bit and I've run into a question I can't seem to answer. I'm trying to approximate the unit circle with cubic splines. I've written the following function to do this. It consumes two row vectors, x and y. Using the built-in spline function, I've parameterized x and y with respect to a variable t that I let range from 0 to 1. x and y have two extra elements compared to t to allow for the use of endslopes. My problem is that I'm not getting a symmetrical curve and I'm wondering why? Does the error stem from the improper use of the spline function or is there a mathematical reason for this that I'm currently missing? Here's the relevant code:
function parametric_spline(x, y)
% Draws a parametric curve (x(t), y(t)
t = linspace(0,1,length(x)-2);
tplot = linspace(0,1,100);
csx = spline(t, x, tplot);
csy = spline(t, y, tplot);
figure(1)
plot(csx,csy)
hold on
plot(cos(2*pi*tplot),sin(2*pi*tplot), 'color', 'red')
xlim([-2 2]);
ylim([-2 2]);
axis equal
figure(2)
plot(tplot, csx);
hold on
plot(tplot, cos(tplot*2*pi), 'color', 'red');
xlim([0 1]);
ylim([-2 2]);
figure(3)
plot(tplot, csy);
hold on
plot(tplot, sin(tplot*2*pi), 'color', 'red');
xlim([0 1]);
ylim([-2 2]);
end
As input, I've used:
x = [0, 1, 0, -1, 0, 1, 0]; y = [1, 0, 1, 0, -1, 0, 1];
I've included the figures produced. Is there a reason the fit is so much better to cos(t) than to sin(t)?



EDIT:
As a followup, I tried setting the first and last values in y as 5 and 5 - it seems to work much better now. I can't see why, however. The slope of y(t) at the beginning and end should be 1, should it not?
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Spline Postprocessing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!