# How can we define piecewise functions along a 2D curve?

13 views (last 30 days)

Show older comments

It's easy to define piecewise functions between specific points (triangular, rectangular, sinusoid functions) on x axis. How can we do it along a 2D curve? (e.g. on a circle)

Thanks,

Dave B.

##### 1 Comment

Image Analyst
on 21 Feb 2014

### Accepted Answer

John D'Errico
on 21 Feb 2014

Edited: John D'Errico
on 21 Feb 2014

The general idea is, you need to parameterize the function.

A circle is easy, since you just work in polar coordinates. Define the piecewise function as a function of theta, the polar angle. (You can always use my piecewise_eval for this purpose, found on the file exchange.)

For more general functions, you probably will find it best to use linear arclength along the curve, or you can use a curvilinear arclength, working with my interparc (as Image Analyst suggested.)

I can give an example or so to show how all of this might work. First, a unit circle. Pick a function that cubically increases from 0 to 1 in the first half, then at 180 degrees, reverses course, dropping down linearly back to zero.

% I've chosen t as the parameter to work in

t = linspace(0,2,200);

% scale t into radians on the interval [0,2*pi].

theta = pi*t;

x = cos(theta);

y = sin(theta);

% use piecewise_eval to do the hard work. Thus...

% 0 <= t < 1 ... z(t) = t.^3

% 1 <= t < 2 ... z(t) = 2 - t

% for t outside of those intervals, the function is zero.

z = piecewise_eval(t,[0 1 2],{0,'t.^3','2 - t',0});

% plot the result

subplot(1,2,1)

plot(t,z,'-')

xlabel 't'

ylabel 'z'

subplot(1,2,2)

plot3(x,y,z,'-')

view(110,8)

grid on

box on

xlabel 'x'

ylabel 'y'

zlabel 'z'

It gets a bit trickier to do this if you have a fully general curve, but it can still be done using my interparc tool. I can add an example if you have something specific in mind. For example, we might have a piecewise function along a polygonal arc, or even a piecewise function along a curvilinear arc, interpolated by splines. Of course, it is probably necessary to think carefully about what you want to happen along that curve.

##### 2 Comments

John D'Errico
on 21 Feb 2014

Given the parametric representation, z(t), this is just a line integral. A nice thing about a circle of radius R is the line integral is really easy to write. It is simply

integral(z(t)*R*dt)

Since R is 1 for a unit circle, as long as you have z(t) in the form of a piecewise_eval call, you can use a tool like integral or one of the quad variations. Make sure you specify waypoints at any singularities, so at any breaks. This will make the integration more efficient.

### More Answers (1)

Image Analyst
on 21 Feb 2014

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!