필터 지우기
필터 지우기

How to make axis continuous based on input

조회 수: 1 (최근 30일)
Mitul Dattani
Mitul Dattani 2017년 4월 11일
편집: Sebastian Castro 2017년 4월 11일
Hi, ive written a code based on projectile motion, and created a GUI with the intent to create an app as a project for university. The issue I run into is my x axis is pre-defined, I was wondering is there a way to change it to where it constantly increased until the line reaches zero in the y-direction, e.g. (6,0) or (58.53,0). In the code below the main block will be in the button callback section. The code below is just the script I use to test, as the forum did not let me post the GUI but its the same without the restrictions.
Thanks in advance!
%======================================================================%
%Arrow is fired from a cliff plot its projectile motion 10m above ground at
%20 m/s
%======================================================================%
clear all %clears workspace
%angle = 0.4; %Size of angle it is fired at in radians from cliff
V0 = 20; %inital velocity = 20 m/s
g = -9.81; %Gravity force taking up as +ive
t = 0:0.1:4; %time
%t = zeros(30);
%h = 10; %height
%Ax = 0; %Vertical Acceleration
Ay = g; %Horizontal Acceleration
%======================================================================%
%xVelocity = V0 * cos(angle);
yVelocity = V0 * sin(angle); %Vertical Velocity
x = t; %x axis = time
y = h + (yVelocity * t + (1/2) * Ay * t.^2); %s = ut + (0.5)at^2
y = max(0,y);
%======================================================================%
plot(x, y);%+10 BECAUSE 10M ABOVE GROUND
%%%%ADD IN LOOP TO STOP WHEN Y AXIS GOES BELOW ZERO THEN INCREASE
%%%%INCREMENTS
%======================================================================%
caption = sprintf('Angle = %.2f radians', angle); %Title of graph defined
title(caption, 'FontSize', 15); %Title of graph inserted
grid on; %Grid on graph
%======================================================================%
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
%======================================================================%

채택된 답변

Sebastian Castro
Sebastian Castro 2017년 4월 11일
편집: Sebastian Castro 2017년 4월 11일
Sure. There's an xlim function in MATLAB that lets you set the axes.
For example, to do limits from 0 to 58 you would type
xlim([0 58])
To do axis limits as soon as a ground collision is detected, you could do something like:
% Find all the indices of y that are less than zero
zeroIdx = find(y < 0);
% If there are indices with altitude less than zero, find the first one and set the X axis limits
if ~isempty(zeroIdx)
finalPosition = y(zeroIdx(1));
xlim([0 finalPosition])
end
% Set all altitudes less than zero to zero (can't penetrate the ground!)
y(zeroIdx) = 0;
- Sebastian

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by