spline? interp?
2 views (last 30 days)
Show older comments
hi guys, obviously I have a problem... I have two vectors (x,y) of 30 points each one and I want to plot(x,y). THey rapresent an histeresis graph (so it's NOT a function). Doing PLOT I obtain a squared graph (with linear segments that connect these points), how can I link the points with a smooth curve? PS=remember it's not a function, so the SPLINE command doesn't work!
0 Comments
Answers (5)
Kelly Kearney
on 9 Sep 2011
You can try interpolating both x and y parametrically. This example puts an equal number of new points between each of your old points; you can possibly get a better result if you calculate actual distance along the polygon edge for your original points.
x = [1.1781; 1.1695; 1.1665; 1.1651; 1.1667; 1.1685; 1.1700; 1.1698; 1.1672; 1.1840; 1.2166; 1.2492; 1.2839; 1.3054; 1.3202; 1.3397; 1.3563; 1.3680; 1.3685; 1.3543; 1.3363; 1.3195; 1.3017; 1.2852; 1.2637; 1.2469; 1.2358; 1.2179; 1.1995; 1.1860; 1.1744];
y = [0.6709; 0.6637; 0.6518; 0.6448; 0.6455; 0.6362; 0.6440; 0.6575; 0.6723; 0.7485; 0.7883; 0.7969; 0.7928; 0.7425; 0.7376; 0.7426; 0.7435; 0.7493; 0.7474; 0.7328; 0.7304; 0.7146; 0.6944; 0.6686; 0.6336; 0.6478; 0.6586; 0.6461; 0.6539; 0.6722; 0.6684];
x2 = interp1(0:30, x, linspace(0,30,150), 'spline');
y2 = interp1(0:30, y, linspace(0,30,150), 'spline');
x3 = interp1(0:30, x, linspace(0,30,150), 'pchip');
y3 = interp1(0:30, y, linspace(0,30,150), 'pchip');
plot(x2,y2,'-c', ...
x3,y3,'-g', ...
x,y,'bo');
0 Comments
John D'Errico
on 9 Sep 2011
The common solution is to use a parameteric interpolant.
x=[1.1781; 1.1695; 1.1665; 1.1651; 1.1667; 1.1685; 1.1700; 1.1698; 1.1672; 1.1840; 1.2166; 1.2492; 1.2839; 1.3054; 1.3202; 1.3397; 1.3563; 1.3680; 1.3685; 1.3543; 1.3363; 1.3195; 1.3017; 1.2852; 1.2637; 1.2469; 1.2358; 1.2179; 1.1995; 1.1860; 1.1744];
y=[0.6709; 0.6637; 0.6518; 0.6448; 0.6455; 0.6362; 0.6440; 0.6575; 0.6723; 0.7485; 0.7883; 0.7969; 0.7928; 0.7425; 0.7376; 0.7426; 0.7435; 0.7493; 0.7474; 0.7328; 0.7304; 0.7146; 0.6944; 0.6686; 0.6336; 0.6478; 0.6586; 0.6461; 0.6539; 0.6722; 0.6684];
Compute a cumulative chordal arclength. This should do it:
t = cumsum(sqrt([0;diff(x)].^2 + [0;diff(y).^2]));
Now interpolate x(t) and y(t) using spline, interp1, etc.
A simple solution is to use my interparc code from the FEX, which does it all in one call, then generating equally spaced points along that smooth curve.
newxy = interparc(1000,x,y);
plot(x,y,'o')
hold on
plot(newxy(:,1),newxy(:,2),'-')
0 Comments
Lucas García
on 8 Sep 2011
Do you have the curve fitting toolbox?
If so, you can try using the functions cscvn for interpolating the spline and fnplt for plotting it.
As an example, you can try the following code:
x = -15:15;
y = x.*sin(x);
plot(x,y,'o-');
hold on
c = cscvn([x;y]);
fnplt(c,'r')
0 Comments
DMGM Mazzoleni
on 9 Sep 2011
1 Comment
John D'Errico
on 9 Sep 2011
Using eps to name a variable is a terribly bad choice. Try to avoid naming a variable with an existing (and useful) matlab function.
See Also
Categories
Find more on Splines in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!