Clear Filters
Clear Filters

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

4 views (last 30 days)
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
Image Analyst on 21 Feb 2014
What do you want as an output? For every "in between" point, do you want the coefficients of the polynomial or whatever function? Or do you just want the values of a fitting function at those points, like a Savitzky-Golay filter gives you?

Sign in to comment.

Accepted Answer

John D'Errico
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
Dave
Dave on 21 Feb 2014
Thank you John for the answer. Your way is very good to illustrate piecewise functions. However, I'm trying to define those functions mathematically first and then multiply it with some other functions and integrate it between two points.
It's the example based on your code:
t = linspace(0,2,200);
theta = pi*t;
x = cos(theta);
y = sin(theta);
z = piecewise_eval(t,[0 0.1 0.2],{0,'(t)/(1)','(0.2-t)/(1)',0});
z2 = piecewise_eval(t,[0.1 0.2 0.3],{0,'(t-0.1)/(1)','(0.3-t)/(1)',0});
subplot(1,2,1)
plot(t,z,'-')
xlabel 't'
ylabel 'z'
hold on
plot(t,z2,'r-')
subplot(1,2,2)
plot3(x,y,z,'-')
grid on
box on
xlabel 'x'
ylabel 'y'
zlabel 'z'
hold on
plot3(x,y,z2,'r-')
Now, I want to use those sinusoid functions inside an integral equation and integrate it along x^2+y^2=1 circle.
Best regards,
Dave
John D'Errico
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.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 21 Feb 2014
  1 Comment
Dave
Dave on 21 Feb 2014
Hi, thanks for your answer. My aim is not just interpolating specific points on a circle with piecewise functions. After defining piecewise functions, I should be able to add it into an integral equation, multiply it with some other functions and then integrate it between those specific points.
if true
xn=[0 1 2 3 4 5 6];
del=1;
x=0:0.01:12;
y=sin((del-abs(x-3)))/sin(del);
plot(x,y)
xlabel 'x'
ylabel 'y'
grid
end
On the code above, I define a piecewise sinusoid between points (2,0) and (4,0). The normal vector between those 2 points (e.g. (3,0) is directed to the top of the sinusoid. Outside of the interval doesn't concern me because after adding it into an integral equation, I'm going to integrate it between 2 and 4 with respect to x. However I don't want to do it along y=0 axis but along a circle.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!