Sum inside a "bouncing" domain

3 次查看(过去 30 天)
chicken vector
chicken vector 2024-1-23
I have a set of positions given by x and y.
Considering a point yf, I want to know what's the value of yi, when the difference along y, is given by dy.
The difference dy is applied considering this difference to bounce within its domain.
To explain myself better, below an example where the outcome has been "hardcoded".
Starting from the final black point, I'd like to find the initial red point where the difference between these two is computed in terms of absolute value along its path.
% Vertical difference:
dy = 3/4*pi;
% Options:
a = pi/3;
N = 361;
f = @(x) asin(sin(a)*sin(x));
% Data:
x = linspace(0,2*pi,N);
y = f(x);
% Final point:
xf = 5/3*pi;
yf = f(xf);
% Find initial point:
% - a+yf = deltaY between black point and -a
% - 2*a = deltaY between -a and a
% - dy-(a+yf+2*a) = deltaY to be removed from a to obtain final point
yi = a-(dy-(a+yf+2*a));
g = @(x) f(x) - yi;
xi = fzero(g,0);
% Figure:
figure;
hold on;
plot(x,y);
scatter(xf,yf,50,'k','filled');
scatter(xi,yi,50,'r','filled');
hold off;
grid on;
In other terms, the condition to be satisfied is:
tol = 1e-4;
xCheck = linspace(xi,xf,1/tol);
dy - sum(abs(diff(f(xCheck)))) < tol
ans = logical
1
Because I'm intereseted only in the value of y, my approach is do to the following:
% My approach:
direction = -sign(gradient(y)+eps);
YI = direction.*(mod(a + y + direction*dy, 2*a) - a);
% Figure:
figure;
hold on;
plot(y);
plot(YI);
hold off;
grid on;
I can see I'm almost there except for the middle part which is flipped.
The proper outcome should be:
% Find indeces of wrong values and flip them:
swapIdx = find(diff(YI(1,:)) > a);
wrongIdx = swapIdx(1)+1:swapIdx(2);
YI(wrongIdx) = -YI(wrongIdx);
% Figure:
figure;
hold on;
plot(y);
plot(YI);
hold off;
grid on;
The variable swapIdx identifies two intervals, one of which is correct, while the other has to be flipped.
At the moment I hardcoded a way to find what interval is wrong and flip it, but I'm almost sure there should be an easier way to sovle the problem.
My limitation is that I don't know the real name of what I'm calling "bouncing domains" and I fail to gather proper knowledge.
Thanks in advance.

回答(1 个)

Steven Lord
Steven Lord 2024-1-23
So you want to identify a point at a certain distance in arc length along the curve?
Since you have point coordinates you have an approximation to your continuous curve as a series of line segments. See the "General approach" section of that Wikipedia page. When you find the line segment on which your desired point is located (where the arc length to one endpoint is smaller than your desired distance and the arc length to the other endpoint is greater) determine where along that line segment the point needs to be located.
  1 个评论
chicken vector
chicken vector 2024-1-25
编辑:chicken vector 2024-1-25
The arc length is almost the same concept of what I am looking for, thank you for the hint.
Nonetheless my problem is simpler than computing the arc length, because I'm only interested in one dimension (y), regardless of x. I used the x in my example because I needed to have something to plot, but if you look at my approach at the end I don't use it at all.
I feel like using the arc length approach, of course solves the proble, but in an overcomplicated way. I'd like to have things as smooth as possible since this procedure has to be performed on 1E07 / 1E09 values.
I'm pretty sure my approach outperformes it, but I would like to improve it.
To explain myself better consider the following example:
% Input data:
a = 1;
yf = .5;
dy = 16/5*a;
% My approach to compute y0:
y0 = mod(a + yf + dy, 2*a) - a;
figure;
hold on;
plot([0 0],[a -a],'k','LineWidth',2);
yline(0,'--','y=0')
yline(a,'--','y=a')
yline(-a,'--','y=-a')
scatter(0,yf,100,'r','filled');
text(-.06,yf,'yf','Color','r');
quiver(.25,yf,0,a/2,'b','AutoScale','off');
text(.2,(a+yf)/2,'a/2','Color','b');
quiver(.35,a,0,-2*a,'b','AutoScale','off');
text(.3,-yf,'2a','Color','b');
quiver(.45,-a,0,7/10*a,'b','AutoScale','off');
text(.46,-(a+yf)/2,'7a/10','Color','b');
scatter(0,y0,100,'g','filled');
plot([0 .25],[yf yf],'r');
plot([0 .45],[y0 y0],'g');
text(-.06,y0,'y0','Color','g');
hold off;
axis off;
ylim(1.1*a*[-1 1]);
xlim([0 1]);
Where:
  • a/2 + 2a + 7a/10 = dy = 16a/5
  • y0 = -3a/10

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Two y-axis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by