Find x and y cordinates and t for z=0

6 views (last 30 days)
Bek
Bek on 5 Nov 2022
Edited: John D'Errico on 5 Nov 2022
I have plotted a 3D trajectory t=0-10sec
How can i find when z=0 what the co-ordinates of x and y are and also what value of t is at z=0?
  3 Comments
Torsten
Torsten on 5 Nov 2022
Edited: Torsten on 5 Nov 2022
And how is the trajectory given ? As a matrix where first column is time and columns 2-4 are x,y and z coordinates, respectively ? Or as expllicit functions x(t), y(t) and z(t) ? Or ...

Sign in to comment.

Answers (4)

KSSV
KSSV on 5 Nov 2022
Let x,y,z,t be yout required arrays. GEt the index of z where it is z = 0. Using this index, you can get what you want.
tol = 10^-5 ;
idx = abs(z)<=tol ;
iwant = [t(idx) x(idx) y(idx) z(idx)]
  1 Comment
Bek
Bek on 5 Nov 2022
xvel=20
yvel=20
zvel=40
g=-9.80665
t = [0:0.1:10]
xacc = 0
yacc = 0
zacc = g
sx = xvel*t + (0.5 * xacc*t.^2)
sy = yvel*t + (0.5 * yacc*t.^2)
sz = zvel*t + (0.5 * zacc*t.^2)
i want to calculate when z=0 what t is and also what the values for x and y are at this point too. thanks

Sign in to comment.


Star Strider
Star Strider on 5 Nov 2022
Edited: Star Strider on 5 Nov 2022
Without knowing more, try something like this —
x = x(:);
y = y(:);
z = z(:);
zidx = find(diff(sign(z)));
for k = 1:numel(zidx)
idxrng = max(1,zidx(k)-1) : min(numel(z),zidx(k)+1);
z0(k,:) = interp1(z(idxrng), [x(idxrng) y(idxrng) t(idxrng)], 0);
end
Using the provided code —
xvel=20;
yvel=20;
zvel=40;
g=-9.80665;
t = [0:0.1:10];
xacc = 0;
yacc = 0;
zacc = g;
sx = xvel*t + (0.5 * xacc*t.^2);
sy = yvel*t + (0.5 * yacc*t.^2);
sz = zvel*t + (0.5 * zacc*t.^2);
x = sx(:);
y = sy(:);
z = sz(:);
t = t(:);
zidx = find(diff(sign(z)));
for k = 1:numel(zidx)
idxrng = max(1,zidx(k)-1) : min(numel(z),zidx(k)+1);
z0(k,:) = interp1(z(idxrng), [x(idxrng) y(idxrng) t(idxrng)], 0);
end
Result = array2table(z0, 'VariableNames',{'x','y','t'})
Result = 2×3 table
x y t ______ ______ ______ 0 0 0 163.15 163.15 8.1574
The rows of ‘z0’ should have all the necessary information for every instance of z=0.
EDIT — (5 Nov 2022 at 13:48)
Added provided code to my original code to demonstrate the result.
.

John D'Errico
John D'Errico on 5 Nov 2022
Edited: John D'Errico on 5 Nov 2022
It seems that from your comments, you want to find where z == 0, so the (x,y) coordinates where that happens, and t at that point.
If all you have are a list of points, (x,y,z,t) that follow some general trajectory in R^3, then in general you cannot compute an analytical solution. You CAN use interpolation to try to discover where that happens. However, you will need to choose an interpolation method, and the solution will be only as good as your choice of interpolant, and as accurate as your data. If there is noise in the data, so these are measured in some way, then again, no analytical solution exists.
The two obvious choices are of interpolant are a linear interpolation, and a spline, but even with a spline, you could still consider various choices of spline, as that would impact the result. And of course, we are not given any datta as an example, so I'll need to make something up to show how to solve it. My example will be a simple spiral helix. with a little noise added to {x(t),y(t),z(t)}.
t = 0:20;
r = t/3;
x = r.*cos(t) + 1 + randn(size(t))/20;
y = r.* sin(t) + 2 + randn(size(t))/20;
z = t.^2 - 73.5 + randn(size(t))/20;
plot3(x,y,z,'o-')
grid on
box on
xlabel x(t)
ylabel y(t)
zlabel z(t)
Of course it is trivially simple to compute where z crosses zero (at least if I exclude the noise in z), since I have the analytical form of z. We can simply plot z(t) to understand the answer.
plot(t,z,'-o')
yline(0)
And now we can use interpolation to decide where that curve crosses z==0. As long as z is a monotonic function of t, this is again easy, but if z is somewhat noisy, it might not even be monotonic, or if the curve is more complex, again that may be a probem. But we can do everything we need using one of the tools from my SLM toolbox, slmsolve. First, find a set of three splines that interpolate the curve, each as a function of t.
splx = spline(t,x);
sply = spline(t,y);
splz = spline(t,z);
I used spline here, but I might have used pchip, or makima, for example, as alternatives to create the splines.
First, find the point (or points) where z crosses zero.
tloc = slmsolve(splz,0)
tloc = 8.5728
tloc is the point where z(t) crosses the indicated level at z==0. So we have
fnval(splz,tloc)
ans = -3.5527e-15
which is as close to zero as we can have in double precision arithmetic. And now it is simple to find x(tloc) and y(tloc)
xy0 = [fnval(splx,tloc),fnval(sply,tloc)]
xy0 = 1×2
-0.9226 4.1387
But suppose the curve was a more complex one, perhaps where z(t) crosses zero. In this case, I can change the problem around, locating where x(t) crosses some fized value (it does not need to be zero either, as slmsolve is fully general in that respect.)
tlocx0 = slmsolve(splx,0)
tlocx0 = 1×6
2.8189 3.8567 8.2105 10.7176 14.3537 17.1040
As you can see, that happened at many locations, but slmsolve found them all. As you can see, it worked as designed. Here they are listed in a table with the columns as {t,x,y,z}.
table(tlocx0',fnval(splx,tlocx0)',fnval(sply,tlocx0)',fnval(splz,tlocx0)')
ans = 6×4 table
Var1 Var2 Var3 Var4 ______ ___________ _______ _______ 2.8189 0 2.2604 -65.568 3.8567 -5.4123e-16 1.1012 -58.532 8.2105 1.1102e-16 4.5432 -6.0661 10.718 1.1102e-15 -1.4352 41.368 14.354 0 6.7266 132.51 17.104 -9.5479e-15 -3.6098 219.05
Again 6 points where the curve crossed x==0, based on a spline interpolant.
All you need is my slmsolve, again, found in the SLM toolbox, available for free download here:

Torsten
Torsten on 5 Nov 2022
xvel=20;
yvel=20;
zvel=40;
g=-9.80665;
t = [0:0.1:10];
xacc = 0;
yacc = 0;
zacc = g;
sx = xvel*t + (0.5 * xacc*t.^2);
sy = yvel*t + (0.5 * yacc*t.^2);
sz = zvel*t + (0.5 * zacc*t.^2);
t_bump = -zvel/(0.5*zacc)
t_bump = 8.1577
sx_bump = xvel*t_bump + (0.5 * xacc*t_bump^2)
sx_bump = 163.1546
sy_bump = yvel*t_bump + (0.5 * yacc*t_bump^2)
sy_bump = 163.1546

Tags

Community Treasure Hunt

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

Start Hunting!