MATLAB Answers

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

14 views (last 30 days)
swathi
swathi on 4 Dec 2019
Answered: swathi on 22 Dec 2019
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 Comment

Adam Danz
Adam Danz on 4 Dec 2019
  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?

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 4 Dec 2019
Edited: Adam Danz on 5 Dec 2019
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 Comments

Show 1 older comment
swathi
swathi on 8 Dec 2019
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 on 9 Dec 2019
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.

Sign in to comment.

More Answers (1)

swathi
swathi on 22 Dec 2019
thank you sir...its working..thank you so much

  0 Comments

Sign in to comment.

Sign in to answer this question.