How to Add Constraint Lines with Ticks to Both Plot and Legend in MATLAB?
72 views (last 30 days)
Show older comments
Hi! I’ve created a graphical solution for an optimization problem. To present this properly, I need to use proper constraint lines, meaning a line with ticks that symbolize the area I need to stay within. I’ve managed to manually add ticks to the constraint lines in the plot, but I can’t get them to appear correctly in my legends.
Does anyone know the best way to solve this?
I’m attaching an image of my plot and another one showing how I want it to look, drawn in Paint. I’m also attaching my code.
I feel like there should be a function in MATLAB to do this automatically, but I haven’t been able to find one.
Any suggestions?
xh=linspace(0.005, H - 0.005, 1000);
xb=linspace(0.005, B - 0.005, 1000);
[bg,hg]=meshgrid(xb,xh);
I = (B.*H.^3-bg.*hg.^3)/12;
volym=L*(B.*H-bg.*hg); % Volume
massa=Density*volym; % Mass
sigma = (Mmax * C) ./ I; % Bending stress
contour(bg,hg,massa,[2.5,3,3.5,4,4.5,5],'LineWidth',1.2,'ShowText','on'); % Mass in kg
colormap("cool");
hold on
bbb=((B*H^3)./xh.^3)-((12*Mmax*C)./(ReLSF*xh.^3))
ht=(((B*H^3)/(B-0.005))-(12*Mmax*C)/(250e6*(B-0.005)))^(1/3);
bt=(B*H^3)/(ht)^3-(12*Mmax*C)/(ReLSF*(ht)^3)
line([B-0.005,B-0.005],[0,H], 'Color', 'black', 'LineStyle', '-', 'LineWidth', 1); % Vertical line for b
line([0,B],[H-0.005,H-0.005], 'Color', 'black', 'LineStyle', '-', 'LineWidth', 1); % Horizontal line for h
% Fixed axis limits
x_min = 0.005; x_max = 0.017;
y_min = 0.005; y_max = 0.04;
% Proportion between x and y axes
aspect_ratio = (x_max - x_min) / (y_max - y_min);
% Tick length (adjusted for proportions)
base_length = 0.0003; % Base length
x_length = base_length; % Length in x
y_length = base_length / aspect_ratio; % Length in y
% Vertical line for b
line([B-0.005, B-0.005], [0, H], 'Color', 'black', 'LineStyle', '-', 'LineWidth', 1); % Vertical line
% Add ticks to the vertical line
yv = linspace(0, H, 30); % Ticks for the vertical line
xv = ones(size(yv)) * (B - 0.005);
for i = 1:length(yv)
line([xv(i) xv(i) + x_length], [yv(i) yv(i)], 'Color', 'red', 'LineWidth', 1); % Horizontal ticks
end
% Horizontal line for h
line([0, B], [H-0.005, H-0.005], 'Color', 'black', 'LineStyle', '-', 'LineWidth', 1); % Horizontal line
% Add ticks to the horizontal line
xh_k = linspace(0, B, 50); % Ticks for the horizontal line
yh_k = ones(size(xh_k)) * (H - 0.005);
for i = 1:length(xh_k)
line([xh_k(i) xh_k(i)], [yh_k(i) yh_k(i) + y_length], 'Color', 'red', 'LineWidth', 1); % Vertical ticks
end
plot(bbb,xh)
scatter(bt, ht);
xlabel("Lilla b (m)")
ylabel("Lilla h (m)")
legend("Funktionen","Bivillkor (b)","Bivillkor (h)","Objektsfunktion",Location="best")
plot(NaN, NaN, 'or', 'DisplayName', 'Optimal solution')
axis([0.005 0.017 0.005 0.04])
INData:
P=5200; %Newton
L=0.82; %Meter
B=0.021; %Meter
b=0.011; %Meter
H=0.041; %Meter
h=0.031; %Meter
E=205*10^9; %Pascal
Vmax=P/2;
t=B-b; %Livets tjocklek
k=(H-h)/2; %Flänsens tjocklek
I=((B*H^3)-(b*h^3))/12; %I-värde balk
ypl=h/4; %Y' fläns
ypf=((H-h)/4)+(h/2); %Y' liv
apl=(h*t)/2; %A' liv
apf=B*k; %A' fläns
apt=apl+apf; %A' tot
ReL=450*10^6;
SF=1.8;
ReLSF=ReL/SF;
Density=7.81*1000; %omvandla till kg/m3
Without ticks:
With ticks (handmade)
0 Comments
Answers (2)
Jacob Mathew
on 22 Nov 2024 at 15:03
Hey Filip,
You can use the Vertical ( | ) and Horizontal ( _ ) marker types to achieve this effect. The example code below creates the same:
% Example number of points
numPoints = 10;
% Horizontal line parallel to x-axis
x_horizontal = linspace(0, 9, numPoints);
y_horizontal = ones(1, numPoints) * 5; % y = 5
% Vertical line parallel to y-axis
y_vertical = linspace(0, 9, numPoints);
x_vertical = ones(1, numPoints) * 3; % x = 3
% Plot the horizontal line with black color and red '|' markers
h1 = plot(x_horizontal, y_horizontal, 'k-|', 'MarkerEdgeColor', 'r', 'DisplayName', 'Boundary');
hold on;
% Plot the vertical line with black color and red '|' markers
h2 = plot(x_vertical, y_vertical, 'k-_', 'MarkerEdgeColor', 'r');
% Hide the second plot from the legend
set(get(get(h2, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off');
% Add labels and title
title('Lines Parallel to Axes');
xlabel('x');
ylabel('y');
legend;
grid on;
hold off;
Benjamin Kraus
on 25 Nov 2024 at 16:39
Edited: Benjamin Kraus
on 25 Nov 2024 at 16:40
If you have access to the Aerospace Toolbox (and MATLAB R2021b or later), you can use the boundaryline command to draw something like this:
boundaryline([0.02 0],[0.035 0.035], Hatches='|');
boundaryline([0.016 0.016],[0 0.04], Hatches='|');
xlim([0.005 0.02])
ylim([0.005 0.04])
legend
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!