Plotting trapezoids in one graph
38 views (last 30 days)
Show older comments
I want to plot 2 or more trapezoids in one coordinate plane as in plotted in the picture I attached. Also I need to plot their inversions in one coordinate plane.
7 Comments
Sam Chak
on 8 Sep 2023
Hi @davut
You may continue the discussion here. Four methods were presented. Which method below did you try but fail to produce the desired complementary trapezoid? You can insert (copy/paste) the code in the grey field after clicking this icon , so that I can investigate and you will learn where the issue is.
Accepted Answer
Sam Chak
on 9 Aug 2023
Hi @davut
Mathematically, these plots represent piecewise linear functions. The easiest way is probably to use the trapezoidal function called trapmf() directly. This requires you to have the Fuzzy Logic Toolbox installed.
x = linspace(-8, -1, 70001);
a = 1/2; % amplitude
T1 = a*trapmf(x, [-8 -7 -6 -5]); % trapezoid 1
T2 = a*trapmf(x, [-6 -5 -4 -3]); % trapezoid 2
T3 = a*trapmf(x, [-4 -3 -2 -1]); % trapezoid 3
U1 = a - T1;
U2 = a - T2;
U3 = a - T3;
subplot(2, 1, 1)
plot(x, [T1; T2; T3]), xlim([-9 0]), ylim([-0.5 1.0]), grid on
subplot(2, 1, 2)
plot(x, [U1; U2; U3]), xlim([-9 0]), ylim([-0.5 1.0]), grid on
2 Comments
Sam Chak
on 9 Aug 2023
Hi @davut
You can add 'k' (which signifies blacK) in the plot() argument to specify the line color. Also take this opportunity to learn other approaches (by @Star Strider) to plotting lines. Additionally, please read this documentation:
x = linspace(-8, -1, 70001);
a = 1/2; % amplitude
T1 = a*trapmf(x, [-8 -7 -6 -5]); % trapezoid 1
T2 = a*trapmf(x, [-6 -5 -4 -3]); % trapezoid 2
T3 = a*trapmf(x, [-4 -3 -2 -1]); % trapezoid 3
U1 = a - T1;
U2 = a - T2;
U3 = a - T3;
subplot(2, 1, 1)
plot(x, [T1; T2; T3], 'k'), xlim([-9 0]), ylim([-0.5 1.0]), grid on
subplot(2, 1, 2)
plot(x, [U1; U2; U3], 'k'), xlim([-9 0]), ylim([-0.5 1.0]), grid on
More Answers (4)
Sam Chak
on 12 Aug 2023
Hi @davut
I'm revisiting your plotting problem. If you simply want to display the trapezoids and their flipped counterparts, you can use the plot() trick to connect the vertices (dots) of each trapezoid.
However, please note that the trapezoid described by the vertices is unsuitable for mathematical analysis, such as finding the y-value at or taking the derivative . Although you can use the interpolation method interp1() to obtain interpolated values between the vertices, if interpolation is an option, it's preferable to use the formulated trapezoid function directly.
The flipped trapezoids are obtained by flipping the original trapezoids about the horizontal line . The following script should be easier to understand if you are new to MATLAB or if you have not yet learned how to use the for-loop technique.
% Trapezoid A (described by the vertices using a 2-by-4 matrix)
trapzA = [1.0 2.0 3.0 4.0; % x-coordinate (Row 1)
0.0 0.5 0.5 0.0]; % y-coordinate (Row 2)
% Trapezoid B
trapzB = [3.0 4.0 5.0 6.0; % x-coordinate
0.0 0.5 0.5 0.0]; % y-coordinate
% Trapezoid C
trapzC = [5.0 6.0 7.0 8.0; % x-coordinate
0.0 0.5 0.5 0.0]; % y-coordinate
subplot(2, 1, 1)
hold on
plot(trapzA(1,:), trapzA(2,:), '.', 'markersize', 10) % <-- plot the dots based on (x, y)
plot(trapzA(1,:), trapzA(2,:)) % connect the dots with a line
text(2.4, 0.6, 'A')
% Remarks:
% trapzA(1,:) specifies all the columns in the 1st row of trapzA matrix (x-coordinate vector).
% trapzA(1,:) specifies all the columns in the 2nd row of trapzA matrix (y-coordinate vector).
plot(trapzB(1,:), trapzB(2,:), '.', 'markersize', 10)
plot(trapzB(1,:), trapzB(2,:))
text(4.4, 0.6, 'B')
plot(trapzC(1,:), trapzC(2,:), '.', 'markersize', 10)
plot(trapzC(1,:), trapzC(2,:))
text(6.4, 0.6, 'C')
grid on
yline(0.25, '--', '0.25', 'color', '#7F7F7F')
xlim([ 0.0 9.0])
ylim([-0.5 1.0])
title('Trapezoids')
xlabel('x'), ylabel('y')
hold off
subplot(2, 1, 2)
hold on
plot(trapzA(1,:), 0.5-trapzA(2,:), '.', 'markersize', 10) % <-- plot the dots based on (x, y)
plot(trapzA(1,:), 0.5-trapzA(2,:)) % connect the dots with a line
text(2.4, 0.6, 'D')
% Remarks:
% 0.5-trapzA(2,:) flips trapzA about the horizontal line y = 0.25,
% and it gives the vertices of the flipped trapezoid.
plot(trapzB(1,:), 0.5-trapzB(2,:), '.', 'markersize', 10)
plot(trapzB(1,:), 0.5-trapzB(2,:))
text(4.4, 0.6, 'E')
plot(trapzC(1,:), 0.5-trapzC(2,:), '.', 'markersize', 10)
plot(trapzC(1,:), 0.5-trapzC(2,:))
text(6.4, 0.6, 'F')
grid on
yline(0.25, '--', '0.25', 'color', '#7F7F7F')
xlim([ 0.0 9.0])
ylim([-0.5 1.0])
title('Flipped Trapezoids')
xlabel('x'), ylabel('y')
hold off
0 Comments
Sam Chak
on 13 Aug 2023
Hi @davut
In the future, if the trapezoids are used for performing mathematical analysis later, then the formulated trapezoid function should be utilized. An example is provided in the following MATLAB script.
% Formulated Trapezoid-like Function
oT = @(x, c) 1/4*(2.5 - abs(x - c) - abs(abs(x - c) - 0.5)); % original
fT = @(x, c) 0.5 - 1/4*(2.5 - abs(x - c) - abs(abs(x - c) - 0.5)); % flipped
c = [2.5, 4.5, 6.5]; % center of trapezoid
x = @(v) linspace(v, v+3, 30001);
v = [1.0, 3.0, 5.0]; % starting point of trapezoid
% Original Trapezoids
subplot(2, 1, 1)
for j = 1:length(v)
plot(x(v(j)), oT(x(v(j)), c(j)));
hold on
end
hold off
grid on
yline(0.25, '--', '0.25', 'color', '#7F7F7F')
xlim([ 0.0 9.0])
ylim([-0.5 1.0])
title('Original Trapezoids')
xlabel('x'), ylabel('y')
% Flipped Trapezoids
subplot(2, 1, 2)
for j = 1:length(v)
plot(x(v(j)), fT(x(v(j)), c(j)));
hold on
end
hold off
grid on
yline(0.25, '--', '0.25', 'color', '#7F7F7F')
xlim([ 0.0 9.0])
ylim([-0.5 1.0])
title('Flipped Trapezoids')
xlabel('x'), ylabel('y')
% Find the y value at x = 1.3579
y1 = oT(1.3579, c(1))
y2 = fT(1.3579, c(1))
0 Comments
Sam Chak
on 14 Aug 2023
Hi @davut
This will be the final update. As shown in the previous approach, finding the one-line formulated trapezoid-like function isn't entirely mathematically intuitive, at least not without some calculations. Thus, if the Fuzzy Logic Toolbox is available, I recommend using the built-in trapezoidal membership function, trapmf(), along with a for-loop to iteratively plot the three trapezoids:
% Using the trapezoidal membership function, trapMF(); requires Fuzzy Logic Toolbox
oT = @(x, v) 1/2*trapmf(x, [v v+1 v+2 v+3]); % original
fT = @(x, v) 0.5 - 1/2*trapmf(x, [v v+1 v+2 v+3]); % flipped
x = @(v) linspace(v, v+3, 30001);
v = [1.0, 3.0, 5.0]; % starting point of trapezoid
% Original Trapezoids
subplot(2, 1, 1)
for j = 1:length(v)
plot(x(v(j)), oT(x(v(j)), v(j)));
hold on
end
hold off
grid on
yline(0.25, '--', '0.25', 'color', '#7F7F7F')
xlim([ 0.0 9.0])
ylim([-0.5 1.0])
title('Construction of Trapezoids using trapmf()')
xlabel('x'), ylabel('y')
% Flipped Trapezoids
subplot(2, 1, 2)
for j = 1:length(v)
plot(x(v(j)), fT(x(v(j)), v(j)));
hold on
end
hold off
grid on
yline(0.25, '--', '0.25', 'color', '#7F7F7F')
xlim([ 0.0 9.0])
ylim([-0.5 1.0])
title('Flipped Trapezoids')
xlabel('x'), ylabel('y')
% Find the y value at x = 1.3579
y1 = oT(1.3579, v(1))
y2 = fT(1.3579, v(1))
0 Comments
Sam Chak
on 8 Sep 2023
Hi @davut
The complementary trapezoid is different from the flipped trapezoid, and thus, they have different mathematical formulas.
If you have a new issue related to the math-based trapezoid, please open a new question and tag me; I will answer it. However, please be reminded to post your MATLAB code, or there is a 90% chance it will be regarded as homework (even if it isn't) and closed by others. Consider voting the answer as a small act of kindness for helping you.
wA = 3/4;
oT = @(x, v) wA*trapmf(x, [v v+1 v+2 v+3]); % original
fT = @(x, v) 1 - wA*trapmf(x, [v v+1 v+2 v+3]); % complementary
x = @(v) linspace(v, v+3, 30001);
v = [1.0, 3.0, 5.0]; % starting point of trapezoid
% Original Trapezoids
subplot(2, 1, 1)
for j = 1:length(v)
plot(x(v(j)), oT(x(v(j)), v(j)));
hold on
end
hold off
grid on
yline(wA, '--', '$w_{A}$', 'Interpreter', 'latex', 'LabelHorizontalAlignment', 'left', 'color', '#7F7F7F')
xlim([ 0.00 9.00])
ylim([-0.25 1.25])
title('Original Trapezoids')
xlabel('x'), ylabel('Degree of membership')
% Complementary Trapezoids
subplot(2, 1, 2)
for j = 1:length(v)
plot(x(v(j)), fT(x(v(j)), v(j)));
hold on
end
hold off
grid on
yline(1-wA, '--', '$1 - w_{A}$', 'Interpreter', 'latex', 'LabelHorizontalAlignment', 'left', 'color', '#7F7F7F')
xlim([ 0.00 9.00])
ylim([-0.25 1.25])
title('Complementary Trapezoids')
xlabel('x'), ylabel('Degree of membership')
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!