Plotting section works for one case and not for the other similar case

1 view (last 30 days)
Any idea why the plotting section of the code is working for the whole "Type == 1" case and not for the whole "Type == 2" case? For the subcases in "Type == 2" that the triangle becomes a line and a point (last two elseif statements) no graphing window appears. It works only if I write plotting sections for each subcase independently "inside" the statements but I want this only once for the whole case.
clc % Clears the command window
clear all % Clears the values that are assigned to the variable
close all % Closes all the graphs
Type = input("Type 1 for hypotenuse or 2 for side : ", 's'); % Asks user if he/she wants to calculate the hypotenuse or the vertical side
Type = str2double(Type);
if Type == 1 % User wants to find hypotenuse
sides = input("Give [side1 side2] = ", 's'); % Asking user to enter the vertical sides' values
sides = str2num(sides); % Use str2num for allowing non-scalars
if numel(sides) == 2 % Make sure that the user enters two numbers for the sides
sides = sides(:).'; % Make sides a row vector
% Assiging easier symbols for the sides
a = sides(1,1);
b = sides(1,2);
if any(imag(sides)) ~= true && a >= 0 && b >= 0 || a == 0 && b == 0
figure(1)
% x- and y-coordinates of the triangle that will be graphed
x = [0 a 0 0];
y = [0 0 b 0];
plot(x,y)
title('Pythagorean theorem')
xlabel('Side 1')
ylabel('Side 2')
grid on
end
if any(imag(sides)) == true || a < 0 || b < 0
disp('No negative neither complex values for lengths are allowed. Please try again.') % Displays to the user that the lengths cannot be complex neither negative numbers
elseif a > 0 && b > 0
disp('The hypotenuse is :')
Hyp = sqrt(a^2 + b^2) % Calculation of the hypotenuse if both sides are given positive
elseif a == 0 && b > 0 || a > 0 && b == 0
Hyp = sqrt(a^2 + b^2)
disp('The triangle has become a single straight line') %Displays what happens when only one vertical side is zero
else
Hyp = sqrt(a^2 + b^2)
disp('The triangle has become a single point') % Displays what happens when both vertical sides are zero
plot(x,y,'*')
grid on
end
end
elseif Type == 2
sides = input("Give [Hyp side] = ", 's');
sides = str2num(sides); % use str2num for allowing non-scalars
if numel(sides) == 2 % make sure they entered two numbers
sides = sides(:).'; % make sides a row vector
% Assiging easier symbols for the hypotenuse and the sides
c = sides(1,1);
b = sides(1,2);
if any(imag(sides)) ~= true && c > 0 && b > 0 && c >= b
figure(1)
% x- and y-coordinates of the triangle that will be graphed
x = [0 sqrt(c^2 - b^2) 0 0];
y = [0 0 b 0];
plot(x,y)
xlabel('Side 1')
ylabel('Side 2')
grid on
end
if any(imag(sides)) == true || c < 0 || b < 0
disp('No negative neither complex values for lengths are allowed. Please try again') %Displays that the sides cannot be negative neither complex numbers
elseif c > 0 && b > 0 && c > b
Side = sqrt(c^2 - b^2) %Calculation of the vertical side when the sides are both positive and the hypotenuse is greater than the vertical
elseif c > 0 && b == 0 || c == b && c ~= 0
Side = sqrt(c^2 - b^2)
disp('The triangle has become a single straight line') % Displays what happens if the hypotenuse is positive but the side is equal to zero, or if the hypotenuse is equal to the side.
elseif c == 0 && b == 0
Side = sqrt(c^2 - b^2)
disp('The triangle has become a single point') % Displays what happens when both the hypotenuse and the side are zero
else
disp('Hyp must be greater than the side. Please try again.') %Displays what happens when Hyp is less than the vertical side
end
end
end
  4 Comments
Voss
Voss on 23 Dec 2021
Notice the plot() function only gets called under this condition:
if any(imag(sides)) ~= true && c > 0 && b > 0 && c >= b
You might change it to be more like the condition for the Type == 1 case, if you want to get plots for when b and/or c are 0.
Left Terry
Left Terry on 23 Dec 2021
Yes. I think I fixed it a liitle:
clc % Clears the command window
clear all % Clears the values that are assigned to the variable
close all % Closes all the graphs
Type = input("Type 1 for hypotenuse or 2 for side : ", 's'); % Asks user if he/she wants to calculate the hypotenuse or one side
Type = str2double(Type);
if Type == 1 % User wants to calculate hypotenuse
sides = input("Give [side1 side2] = ", 's'); % Asking user to enter the perpendicular sides' values
sides = str2num(sides); % Use str2num for allowing non-scalars
if numel(sides) == 2 % Make sure that the user enters two numbers for the sides
sides = sides(:).'; % Make sides a row vector
% Assigning easier symbols for the sides
a = sides(1,1);
b = sides(1,2);
if any(imag(sides)) == false && a >= 0 || b >= 0 && a == 0 && b == 0 % Plotting section. Graph appears only for real and non-negative numbers (lengths' values)
figure(1)
% x- and y-coordinates of the triangle that will be graphed
x = [0 a 0 0];
y = [0 0 b 0];
plot(x,y)
title('Pythagorean theorem')
xlabel('Side 1')
ylabel('Side 2')
grid on
if a > b
axis([0 a 0 a])
end
if a < b
axis([0 b 0 b])
end
end
if any(imag(sides)) == true || a < 0 || b < 0
disp('No negative neither complex values for lengths are allowed. Please try again.') % Displays to the user that the lengths cannot be complex neither negative numbers
elseif a > 0 && b > 0
disp('The hypotenuse is :')
Hyp = sqrt(a^2 + b^2) % Calculation of the hypotenuse if both sides are given positive
elseif a == 0 && b == 0
Hyp = sqrt(a^2 + b^2)
disp('The triangle has become a single point') % Displays what happens when both vertical sides are zero
plot(x,y,'*') % Shows the point's location in the graph
title('Pythagorean theorem')
xlabel('Side 1')
ylabel('Side 2')
grid on
else
Hyp = sqrt(a^2 + b^2)
disp('The triangle has become a single straight line') %Displays what happens when only one vertical side is zero
end
else
disp('Please try again and enter two numbers for the sides.');
end
elseif Type == 2 % User wants to find
sides = input("Give [Hyp side] = ", 's');
sides = str2num(sides); % Use str2num for allowing non-scalars
if numel(sides) == 2 % Make sure the user entered two numbers
sides = sides(:).'; % Make sides a row vector
% Assigning easier symbols for the hypotenuse and the sides
c = sides(1,1);
b = sides(1,2);
if any(imag(sides)) == false && c >= 0 && b >= 0 && c >= b
figure(1)
Side = sqrt(c^2-b^2);
% x- and y-coordinates of the triangle that will be graphed
x = [0 Side 0 0];
y = [0 0 b 0];
plot(x,y)
title('Pythagorean theorem')
xlabel('Side 1')
ylabel('Side 2')
grid on
if b > Side
axis([0 b 0 b])
elseif b < Side
axis([0 Side 0 Side])
else
axis([-1 1 -1 1])
end
end
if any(imag(sides)) == true || c < 0 || b < 0
disp('No negative neither complex values for lengths are allowed. Please try again') %Displays that the sides cannot be negative neither complex numbers
elseif c == 0 && b == 0
Side = sqrt(c^2 - b^2)
disp('The triangle has become a single point') %Displays what happens when both Hyp and vertical are zero
plot(x,y,'*') % Shows the point's location in the graph
title('Pythagorean theorem')
xlabel('Side 1')
ylabel('Side 2')
grid on
elseif c > 0 && b > 0 && c > b
Side = sqrt(c^2 - b^2) %Calculation of the vertical side when the sides are both positive and the hypotenuse is greater than the vertical
elseif c > 0 && b == 0 || c == b
Side = sqrt(c^2 - b^2)
disp('The triangle has become a single straight line') % Displays what happens if the hypotenuse is positive but the side is equal to zero, or if the hypotenuse is equal to the side.
else
disp('Hyp must be greater than the side. Please try again.') %Displays what happens when Hyp is less than the side
end
else
disp('Please try again and enter two numbers for the hypotenuse and the side respectively.');
end
else
disp('Wrong Input. Please try again and type 1 if you want to calculate the hypotenuse or 2 if you want one of the other sides.') % Displays what happens when the user enters a number different than 1 or 2
end

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 23 Dec 2021
Edited: Cris LaPierre on 23 Dec 2021
The last two else-if statements are working as designed. Perhaps you could more clearly state what "not working" means to you?
The plotting is actually done in the second if-statement for the Type == 2 case. When one or both of your sides are zero, this code is skipped, so no plot command is called. This is because, when, say b=0, then the condition b > 0 is false.
sides = [5 0];
c = sides(1,1);
b = sides(1,2);
any(imag(sides)) ~= true && c > 0 && b > 0 && c >= b
ans = logical
0
  3 Comments
Cris LaPierre
Cris LaPierre on 23 Dec 2021
There are ways to code this so that you don't have to plot twice, but if this is working for your needs, that's fine, too.

Sign in to comment.

More Answers (0)

Categories

Find more on Labels and Annotations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!