% segment2_sym.m - symbolic resolution %{ Assigned two points in the real plane, P1, P2; we want to establish if a third point Pj belongs, or not, to the segment between P1 and P2. %} clear; clc; %close all %% line and segment between these two points rng('default') % Set the random number generator to the default seed(0) P1=rand(2,1); P2=2*rand(2,1); %% parametric equation of the line through P1 and P2 (of the segment if lambda in [0,1]) syms lambda real P = P1 + lambda*(P2 - P1); % parametric vector eq of the line %% Other points (irrational numbers) P3=[2 0]'; % P3 not on the line P4=P1 + pi/2*(P2 - P1); % P4 on the line, but outside of the segment P5=P1 + pi/4*(P2 - P1); % P5 on the line, and inside the segment %% Display segment and points figure(2); clf h=ezplot(P(1),P(2),[-2 3]); set(h,'Color','r','LineStyle','--') axis([-1.5 3 -1 3]) axis equal; hold on; grid on; box on h=ezplot(P(1),P(2),[0 1]); set(h,'Color','r','LineWidth',3); plot([P1(1) P2(1)],[P1(2) P2(2)],'Color','r','Marker','.','MarkerSize',24) HT=text(P1(1),P1(2),' P_1','FontSize',14,'Color','k','FontWeight','Bold','VerticalAlignment','top','HorizontalAlignment','right'); HT=text(P2(1),P2(2),' P_2','FontSize',14,'Color','k','FontWeight','Bold','VerticalAlignment','top','HorizontalAlignment','right'); title('Segment between P_1 and P_2','FontSize',14) h=plot(P3(1),P3(2),'om','MarkerSize',10,'MarkerFaceColor','m'); HT=text(P3(1),P3(2),' P_3','FontSize',14,'Color','m','FontWeight','Bold','VerticalAlignment','bottom'); h=plot(P4(1),P4(2),'og','MarkerSize',10,'MarkerFaceColor','g'); HT=text(P4(1),P4(2),' P_4','FontSize',14,'Color','g','FontWeight','Bold','VerticalAlignment','top','HorizontalAlignment','right'); h=plot(P5(1),P5(2),'ob','MarkerSize',10,'MarkerFaceColor','b'); HT=text(P5(1),P5(2),' P_5','FontSize',14,'Color','b','FontWeight','Bold','VerticalAlignment','bottom'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% P3 not on the line: 1) with symbolic solve() S=solve(P-P3 == 0,lambda) if ~isempty(S) double(S) > 0 & double(S) < 1 if double(S) > 0 & double(S) < 1 disp('P3 belongs to the segment between P1 and P2.') else disp('P3 lies on the line passing through P1 and P2,') disp(' but it is outside the segment.') end else disp('P3 is not on the line passing through P1 and P2.') end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% P4 on the line, but outside of the segment: 1) with solve() %{ %% THIS CODE DOES NOT WORK! S=solve(P-P4 == 0,lambda) if ~isempty(S) double(S) > 0 & double(S) < 1 if double(S) > 0 && double(S) < 1 disp('P4 belongs to the segment between P1 and P2.') else disp('P4 lies on the line passing through P1 and P2,') disp(' but it is outside the segment.') end else disp('P4 is not on the line passing through P1 and P2.') end %% solve every single equation S1=solve(P(1)-P4(1) == 0,lambda); S2=solve(P(2)-P4(2) == 0,lambda); fprintf('\nS1=solve(P(1)-P4(1) == 0,lambda) ==> lambda = '); disp(double(S1)) fprintf( 'S2=solve(P(2)-P4(2) == 0,lambda) ==> lambda = '); disp(double(S2)) if double(S1) == double(S2) double(S1) > 0 & double(S1) < 1 if double(S1) > 0 && double(S1) < 1 disp('P4 belongs to the segment between P1 and P2.') else disp('P4 lies on the line passing through P1 and P2,') disp(' but it is outside the segment.') end else disp('P4 is not on the line passing through P1 and P2.') end %} % { %% THIS CODE WORKS! S1=solve(P(1)-P4(1) == 0,lambda); S2=solve(P(2)-P4(2) == 0,lambda); fprintf('\ndouble(S1) = '); disp(double(S1)) fprintf( 'double(S2) = '); disp(double(S2)) fprintf('\ndouble(S1) == double(S2) ?\n'); double(S1) == double(S2) fprintf('\nabs(double(S1) - double(S2)) = '); disp(abs(double(S1) - double(S2))) fprintf('\nabs(double(S1) - double(S2)) < 1e-8\n'); abs(double(S1) - double(S2)) < 1e-8 if abs(double(S1) - double(S2)) < 1e-8 fprintf('\ndouble(S1) = '); disp(double(S1)) fprintf('\ndouble(S1) > 0 & double(S1) < 1 ?\n'); double(S1) > 0 & double(S1) < 1 if double(S1) > 0 & double(S1) < 1 disp(['P4 = (' num2str(P4') ') belongs to the segment.']); disp('') else disp(['P4 = (' num2str(P4') ') does not belong to the segment,']) fprintf('\tbut it lies on the line.\n\n') end else disp(['P4 = (' num2str(P4') ') does not lie on the line.']) end %} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% P5 on the segment: 1) with solve() %{ %% THIS CODE DOES NOT WORK! S=solve(P-P5 == 0,lambda) if ~isempty(S) double(S) > 0 & double(S) < 1 if double(S) > 0 && double(S) < 1 disp('P5 belongs to the segment between P1 and P2.') else disp('P5 lies on the line passing through P1 and P2,') disp(' but it is outside the segment.') end else disp('P5 is not on the line passing through P1 and P2.') end %% solve every single equation S1=solve(P(1)-P5(1) == 0,lambda); S2=solve(P(2)-P5(2) == 0,lambda); fprintf('\nS1=solve(P(1)-P5(1) == 0,lambda) ==> lambda = '); disp(double(S1)) fprintf( 'S2=solve(P(2)-P5(2) == 0,lambda) ==> lambda = '); disp(double(S2)) if double(S1) == double(S2) double(S1) > 0 & double(S1) < 1 if double(S1) > 0 && double(S1) < 1 disp('P5 belongs to the segment between P1 and P2.') else disp('P5 lies on the line passing through P1 and P2,') disp(' but it is outside the segment.') end else disp('P5 is not on the line passing through P1 and P2.') end %} % { %% THIS CODE WORKS! S1=solve(P(1)-P5(1) == 0,lambda); S2=solve(P(2)-P5(2) == 0,lambda); fprintf('\ndouble(S1) = '); disp(double(S1)) fprintf( 'double(S2) = '); disp(double(S2)) fprintf('double(S1) == double(S2) ?\n'); double(S1) == double(S2) fprintf('\nabs(double(S1) - double(S2)) = '); disp(abs(double(S1) - double(S2))) fprintf('abs(double(S1) - double(S2)) < 1e-8\n'); abs(double(S1) - double(S2)) < 1e-8 if abs(double(S1) - double(S2)) < 1e-8 fprintf('\ndouble(S1) = '); disp(double(S1)) fprintf('\ndouble(S1) > 0 & double(S1) < 1 ?\n'); double(S1) > 0 & double(S1) < 1 if double(S1) > 0 & double(S1) < 1 disp(['P5 = (' num2str(P5') ') belongs to the segment.']); disp('') else disp(['P5 = (' num2str(P5') ') does not belong to the segment,']) fprintf('\tbut it lies on the line.\n\n') end else disp(['P5 = (' num2str(P5') ') does not lie on the line.']) end %} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%