When I try to plot my projectile motion function, the GUI plot does not display the entire trajectory as it does when I run the function in regular MATLAB. Instead it displays the projectile (dot) moving along while the y-axis constantly shifts to capture the height of the projectile. I would the entire trajectory to be displayed and also not have the y-axis shift so frantically. Below is the code in the GUI and after that, the code from the MATLAB function.
angle = app.angle.Value; x0 = app.x0.Value; y0 = app.y0.Value; v0 = app.v0.Value; g = 9.81; hangtime = 2*v0*sind(angle)/g; t = 0:hangtime/100:hangtime; vix = v0*cosd(angle); viy = v0*sind(angle); x = x0+vix*t; y = y0+viy*t-(g*t.^2)/2; maxheight = y0 + (viy)^2./(2*g) xheight = x0 + vix*(t/2); for k=2:length(t) x(k) = x0 +(vix*t(k)); y(k) = y0 +(viy*t(k))-0.5*g*t(k)^2; h = plot(app.UIAxes,x(k),y(k),'.'); hold(app.UIAxes); set(h,'MarkerSize',10); set(h,'Color',[0.1,0.3,1]); pause (0.02); end end
Now this is the original function code:
function projmotion(x0,y0,v0,theta)% projmotion calculates projectile motion
%
% projmotion(x0,y0,v0,theta)
%% x0 = initial x(horizontal) position
% y0 = initial y(vertical) position
% v0 = initial velocity
% theta = angle of launch
%
% Ver 1.0 by RSS
g = 9.81;angle = theta*(pi./180);hangtime = 2*v0*sin(angle)/g;t = hangtime;vix = v0*cos(angle);viy = v0*sin(angle);x = x0+vix*t;y = y0+viy*t-(g*t.^2)/2;maxheight = y0 + (viy)^2./(2*g)xheight = x0 + vix*(t/2);if theta >90 error('Please select angle value of 90 degrees or less')end;figure('Color', [1 1 1]);for k=0:t/100:t x = x0 +(vix*k); y = y0 +(viy*k)-0.5*g*(k^2);end for k=0:t/100:t x = x0 +(vix*k); y = y0 +(viy*k)-0.5*g*(k^2); h = plot(x,y,'.'); xlabel('Horizontal Distance (meters)'); ylabel('Height (meters)'); title('Trajectory Time'); set(h,'MarkerSize',10); set(h,'Color',[1,0.3,0.5]); hold on; pause(0.02); ends1 = sprintf('Total time of travel is %f seconds.\n', t)s2 = sprintf('Range is %f meters.\n', x-x0)s3 = sprintf('Max height is %f meters.\n', maxheight)s4 = sprintf('X position at max height is %f meters.\n', xheight)message = sprintf('%s%s%s%s%s%s%s%s',s1, s3, s2, s4);uiwait(helpdlg(message, 'Simulation Results'));
Thanks for your time out there!
Best Answer