How can i write recursive function using midpoint equation 3d coordinate system?

10 次查看(过去 30 天)
A,B are 2 inputs.with this 2 inputs i want to generate n number of midpoints
A = [2 2 2];
B = [13 13 13];
c = mid of (A,B);
D = mid of (A,C);
E = mid(C,B);
how can i write this program in recursive function to calculate n number of midpoints...
and each midpoint distance is>1
  1 个评论
Adam Danz
Adam Danz 2019-12-4
编辑:Adam Danz 2019-12-6
  1. Define "midpoint". Is this a median? mean? center of mass?
  2. In your quesiton, I'm assuming C is a scalar value (not a vector but a single data point). So how is that combined with A in D= mid of (A,C)?
Your examples of inputs A and B are helpful but please provide an example of expected outputs so we have a complete picture.
----[update after your question-edit]----
By 'midpoint' do you mean you want the (x,y,z) coordinate of the midpoint between the line whose endpoints are A and B?

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2019-12-4
编辑:Adam Danz 2019-12-5
This solution creates an anonymous function midPointFcn() that receives two inputs A and B which are both (x,y,z) coordinates of a line. The output are the 3 midpoints from your question: C, D, & E.
Then I plot the results for confirmation.
See inline comments for more detail
% INPUTS: (x,y,z) endpoints of a line AB
A = [2 2 2];
B = [13 13 13];
% Create midpoint function
% Inputs A and B are 1x3 (x,y,z) coordinates.
% Output is 3x3 where columns are (x,y,z) coordinates.
% Row 1 is the mid point of AB
% Row 2 is the mid points between A and midpoint(AB)
% Row 3 is the mid points between B and midpoint(AB)
midPointFcn = @(A,B)[mean([A;B]); mean([A;mean([A;B])]); mean([B;mean([A;B])])];
% Compute midpoints C,D,&E
CDE = midPointFcn(A,B);
% Plot results
clf()
plot3([A(1),B(1)],[A(2),B(2)],[A(3),B(3)],'k-o','DisplayName', 'Line AB')
grid on
hold on
plot3(CDE(1,1),CDE(1,2),CDE(1,3),'m*','DisplayName','Midpoint C')
plot3(CDE(2,1),CDE(2,2),CDE(2,3),'b*','DisplayName','Midpoint D')
plot3(CDE(3,1),CDE(3,2),CDE(3,3),'r*','DisplayName','Midpoint E')
legend()
  4 个评论
swathi
swathi 2019-12-8
编辑:swathi 2019-12-8
in that my starting point is (x,y,z) = (0 0 0)& my ending points are (x,y,z) = (17 17 17)
in that i want to generate all midpoints in between starting and ending points...and distance between two points is not greater than 1..the midpoint generate untill >1
this program i want to write using " recursive function"
Adam Danz
Adam Danz 2019-12-9
A recursive function is just a function that calls itself. Check out tue midPointFcn function in my answer. it just receives two coordinates A and B and determines the midpoint between A and B. If you want that to be recursive, you could create a while-loop that does the following
  1. It starts with the two end points A and B as the initial inputs and produces C which is the midpoint of AB.
  2. Then the new inputs are A and C which finds the midpoint D of AC.
  3. Then the new inputs are A and D etc.....
  4. After each midpoint calculation, you calculate the distance and when it falls below threshold. you stop.
  5. But then you have to do the same thing from the other end where the inputs are (B,C) etc...
If you get stuck, show us how far you got and we can help you get unstuck.

请先登录,再进行评论。

更多回答(1 个)

swathi
swathi 2019-12-22
thank you sir...its working..thank you so much

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by