MATLAB Answers

Fix axis on plot

7 views (last 30 days)
Benjamin Nienhouse
Benjamin Nienhouse on 20 Nov 2020
Answered: Geoff Hayes on 21 Nov 2020
So I figured out how to fix both asix' based on the user input on all except on the "Angles 0-180" graph.
Any help? I don't know why what I did for the other plots didn't work on that one.
Thanks,
Ben
% h = input("Initial height (m): "); %Initial height
h = 10;
% angle = input("Angle of elavation (degrees): "); %Degrees
angle = 45;
% v = input("Initial velocity (m/s): "); %M/S
v = 45;
g=-9.81; %M/S^2
angles = 0:10:180; %Angle range
heights = 0:40:200; %Height range
velocity = 10:10:100; %Velocity range
figure %Plot with user input and angles ranging from 0 10 180
for a = angles
[x_val,y_val] = Arc(g,h,a,v);
% m_angle = [x_val', y_val'];
% max1 = max(max(m_angle));
and = plot(x_val,y_val);
hold on;
% axis([0 max1*1.05 0 max1*1.05]);
end
xlabel 'x (m)'
ylabel 'y (m)'
title 'Angles 0-180'
legend ('0 degrees', '10 degrees', '20 degrees', '30 degrees', '40 degrees', '50 degrees', '60 degrees', '70 degrees', '80 degrees', '90 degrees', '100 degrees', '110 degrees', '120 degrees', '130 degrees', '140 degrees', '150 degrees', '160 degrees', '170 degrees', '180 degrees');
figure
for hi = heights %Plot with user input and heights ranging from 0 10 100
[x_val,y_val] = Arc(g,hi,angle,v);
m_hi = [x_val', y_val'];
max2 = max(max(m_hi));
plot(x_val,y_val);
hold on;
axis([0 max2*1.05 0 max2*1.05]); %Fix axis to max * 1.05
end
xlabel 'x (m)'
ylabel 'y (m)'
title 'Height 0-100 (m)'
legend ('H = 0','H = 40','H = 80','H = 120','H = 160','H = 200');
figure
for vel = velocity %Plot with user input and velocities ranging from 10 10 180
[x_val,y_val] = Arc(g,h,angle,vel);
m_vel = [x_val', y_val'];
max3 = max(max(m_vel));
plot(x_val,y_val);
hold on;
axis([0 max3*1.05 0 max3*1.05]); %Fix axis to max * 1.05
end
xlabel 'x (m)'
ylabel 'y (m)'
title 'Velocity 10-100 (m/s)'
legend ('10 m/s','20 m/s','30 m/s','40 m/s','50 m/s','60 m/s','70 m/s','80 m/s','90 m/s','100 m/s');
[x_val,y_val] = Arc(g,h,angle,v);
m = [x_val',y_val'];
max_val = max(max(m));
figure
plot(x_val,y_val,'r');
hold on;
[~,idx] = max(y_val);
plot(x_val(idx),y_val(idx),'b-')
text(x_val(idx),y_val(idx),'max','VerticalAlignment','Bottom','HorizontalAlignment','Left','FontSize',8)
axis([0 max_val*1.05 0 max_val*1.05]); %Fix axis to max * 1.05
xlabel 'x (m)'
ylabel 'y (m)'
title 'Original Data'
legend 'User Data,'
% n = numel(x_val);
% F(n) = struct('cdata',[],'colormap',[]);
% for i = 1:n %Slow down plot
% o.XData(i) = x_val(i);
% o.YData(i) = y_val(i);
% drawnow
% F(i) = getframe;
% end
function [x_val,y_val] = Arc(g,h,angle,v) %Function to calculate the trajectory
y = h;
t = 0;
x_val = []; %Prealocate x data
y_val = []; %Prealocate y data
while y >= 0
y=.5*g*t.^2+v*sind(angle)*t+h;
x=v*cosd(angle)*t;
t = t + 0.01;
x_val = [x_val,x]; %Storing all the x data
y_val = [y_val,y]; %Storing all the y data
end
end

Answers (1)

Geoff Hayes
Geoff Hayes on 21 Nov 2020
Benjamin - I'm not entirely sure this is what you want, but I think that you need to find the minimum and maximum across all x and all y and use those values for your axis.
minX = Inf; % assign default values for min and max
maxX = 0;
minY = Inf;
maxY = 0;
for a = angles
[x_val,y_val] = Arc(g,h,a,v);
minY = min(minY, min(min(y_val))); % find the min Y given previous min and all y_val
maxY = max(maxY, max(max(y_val))); % find the max Y given previous max and all y_val
minX = min(minX, min(min(x_val))); % find the min X given previous min and all x_val
maxX = max(maxX, max(max(x_val))); % find the max X given previous max and all x_val
and = plot(x_val,y_val);
hold on;
end
axis([minX maxX minY maxY].*1.05);

Community Treasure Hunt

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

Start Hunting!