Plotting section works for one case and not for the other similar case
1 view (last 30 days)
Show older comments
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
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.
Accepted Answer
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
3 Comments
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.
More Answers (0)
See Also
Categories
Find more on Labels and Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!