Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to evenly sample hyperbola y = 1/x

Subject: How to evenly sample hyperbola y = 1/x

From: Eric Diaz

Date: 9 Dec, 2010 01:08:04

Message: 1 of 4

Dear Matlab community,

I would like to ask for some help developing a function to approximately sample a simple hyperbola, y = 1/x.

So far, I've come up with a crude approximation based on simple triangles, however am having difficulty translating it into matlab code.

#############
if y = 1/x,
then as a crude approximate to length of curve
deltaS = sqrt(deltaY^2 + deltaX^2) % I want this to be constant for y sampling

Then,define
deltaY = dy/dx*deltaX
and,
deltaS = deltaX*sqrt((dy/dx)^2 + 1)
and,
dy/dx = D(1/x) = -x^(-2)
so,
deltaS = deltaX*sqrt((-x^(-2))^2 + 1),
or by simplifying,
deltaS = deltaX*sqrt(x^-4 + 1) ,
and then solving for the deltaX,given a constant deltaS ==C,
deltaX = C/sqrt(x^-4 + 1)

From here, I am having difficulty implementing it. If there is anyone willing to help or offer a better approximation/solution. I would really appreciate it. Thank you in advance.

Eric

Subject: How to evenly sample hyperbola y = 1/x

From: John D'Errico

Date: 9 Dec, 2010 01:53:05

Message: 2 of 4

"Eric Diaz" <eric.diaz@gmail.com> wrote in message <idpa5k$4d6$1@fred.mathworks.com>...
> Dear Matlab community,
>
> I would like to ask for some help developing a function to approximately sample a simple hyperbola, y = 1/x.
>
> So far, I've come up with a crude approximation based on simple triangles, however am having difficulty translating it into matlab code.
>
> #############
> if y = 1/x,
> then as a crude approximate to length of curve
> deltaS = sqrt(deltaY^2 + deltaX^2) % I want this to be constant for y sampling
>
> Then,define
> deltaY = dy/dx*deltaX
> and,
> deltaS = deltaX*sqrt((dy/dx)^2 + 1)
> and,
> dy/dx = D(1/x) = -x^(-2)
> so,
> deltaS = deltaX*sqrt((-x^(-2))^2 + 1),
> or by simplifying,
> deltaS = deltaX*sqrt(x^-4 + 1) ,
> and then solving for the deltaX,given a constant deltaS ==C,
> deltaX = C/sqrt(x^-4 + 1)
>
> From here, I am having difficulty implementing it. If there is anyone willing to help or offer a better approximation/solution. I would really appreciate it. Thank you in advance.
>
> Eric

A simple solution is to use my interparc utility, applied
to a set of arbitrarily sampled points along the hyperbola.
It will do the work for you, to find those equally spaced
points in arc length.

Alternatively, you can use an ode solver to compute
distance along the curve itself. But that will take a wee
bit more work. You can follow the general method in
interparc, as it uses the ode approach itself.

http://www.mathworks.com/matlabcentral/fileexchange/27096-interparc

John

Subject: How to evenly sample hyperbola y = 1/x

From: Eric Diaz

Date: 9 Dec, 2010 04:23:05

Message: 3 of 4

Thank you John.

I ended up implementing my own quick and dirty approach that works for the purposes that I need it for. It's pasted below.

function EvenSamplingOfFunction(minx,delx,maxx,DS)

x = minx:delx:maxx;
x2 = minx-delx/2:delx:maxx+delx/2; %just a shifted version
Nx = size(x,2);
Nx2 = size(x2,2);

y = myfun(x);
y2 = myfun(x2);

%Diffy=gradient(y)/delx;
Diffy=diff(y2)/delx;

% Dy=1./x.^2; %ITS DERIVATIVE
% figure;
% hold on;
% plot(x,Dy,'k','Linewidth',5)
% plot(x,abs(Diffy),'color','r','Linewidth',5)
% pause;

S(1) = 0;
counter = 1;
xsample(counter)=minx;

for nx=2:Nx
    dS=delx*sqrt(1+Diffy(nx)^2);
    S(nx)=S(nx-1)+dS;
    if S(nx)>counter*DS
        counter=counter+1;
        xsample(counter)=x(nx);
    end
end

ysample = myfun(xsample);

figure;
hold on;
plot(x,y,'k','Linewidth',5)
plot(xsample,ysample,'o','Color','r','Linewidth',5)

xsample

function F1 = myfun(xdata)
F1 = 1./xdata; %PUT ANY FUNCTION HERE
%F1 = 5*exp(-xdata); %PUT ANY FUNCTION HERE
%F1 = xdata.^2; %PUT ANY FUNCTION HERE
%F1 = 5*sin(xdata); %PUT ANY FUNCTION HERE

Subject: How to evenly sample hyperbola y = 1/x

From: Roger Stafford

Date: 9 Dec, 2010 05:01:28

Message: 4 of 4

"Eric Diaz" <eric.diaz@gmail.com> wrote in message <idpa5k$4d6$1@fred.mathworks.com>...
> .......
> I would like to ask for some help developing a function to approximately sample a simple hyperbola, y = 1/x.
> ......
- - - - - - - - - -
  You might consider using matlab's 'ode' solvers (as John suggested) but using arc length as the independent variable. This way you could specify uniformly-spaced intervals of arc length in the "tspan" (second) argument along some finite length of the hyperbola and the output would give the corresponding x-values needed for uniform spacing along your curve.

  The differential equation would be:

 dx/ds = 1/sqrt(1+1/x^4)

for your curve. This would give you a series of x-values along the curve for which the points (x(1),1/x(1)),(x(2),1/x(2)), (x(3),1/x(3)), .... are uniformly-spaced arclength-wise.

  This seems to me to be a very straightforward approach to your problem. As you can see, this method could also be applied to a wide selection of such problems with different curves.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us