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

조회 수: 15 (최근 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에서 Contour Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by