Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: equally spaced data points over a curve
Date: Mon, 10 Nov 2008 18:02:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 72
Message-ID: <gf9sur$r89$1@fred.mathworks.com>
References: <gf9fl8$4kh$1@fred.mathworks.com> <see-AE3628.11371810112008@news.frontiernet.net>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1226340123 27913 172.30.248.37 (10 Nov 2008 18:02:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 10 Nov 2008 18:02:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1338633
Xref: news.mathworks.com comp.soft-sys.matlab:500033


Thanks for your code Doug, however, it doesn't do what I want. Your code computes points over the curve equally spaced along the x axis. What I want is to compute points over the curve equally spaced along the curve.

My curve has regions of zero gradient and regions of much steeper gradient. I want the distance between each set of two points to be the same over the length of the curve.

Do you know how your code could be edited to enable this? Thanks for your help so far.




Doug Schwarz <see@sig.for.address.edu> wrote in message <see-AE3628.11371810112008@news.frontiernet.net>...
> In article <gf9fl8$4kh$1@fred.mathworks.com>,
>  "Dave Brackett" <davebrackett@hotmail.com> wrote:
> 
> > Hi, 
> > 
> > I have plotted a curve which connects up some data points. The curve is not a 
> > straight line.
> > 
> > I would like to compute a new set of data points from the plotted curve, but 
> > equally spaced over the length of the curve. The number of points would be 
> > specified which would control the resolution. 
> > 
> > Does anyone know how I could do this? If you need any more information please 
> > ask. Thanks.
> 
> Here's something I plan on contributing to the FEX when I get a chance 
> to clean it up and document it.  It's not especially fast, but if you 
> already have x and y in functional form it could be faster.
> 
> 
> ----------------- linspacearc.m ---------------------------
> function [x2,y2] = linspacearc(x,y,n)
> m = length(x);
> t = linspace(0,1,m);
> ppx = spline(t,x);
> ppy = spline(t,y);
>  
> dppx = pp_deriv(ppx);
> dppy = pp_deriv(ppy);
> integrand = @(tt) sqrt(ppval(dppx,tt).^2 + ppval(dppy,tt).^2);
> arc_length = quadgk(integrand,0,1);
> s = linspace(0,arc_length,n);
>  
> inv_arc_len = @(arc,est) fzero(@(u)(quadgk(integrand,0,u)) - arc,est);
>  
> t2 = zeros(1,n);
> t2(1) = inv_arc_len(s(1),0);
> for i = 2:n
>     t2(i) = inv_arc_len(s(i),t2(i-1));
> end
>  
> x2 = ppval(ppx,t2);
> y2 = ppval(ppy,t2);
>  
>  
> function dpp = pp_deriv(pp)
> % pp_deriv: derivative of piecewise polynomial (pp)
>  
> dpp = pp;
> n = pp.order;
> dpp.coefs = bsxfun(@times,n-1:-1:1,pp.coefs(:,1:n-1));
> dpp.order = n - 1;
> -------------------------------------------------------------
> 
> 
> Simply pass in your x and y vectors and the number of desired output 
> points and get back your new x and y vectors.
> 
> -- 
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.