필터 지우기
필터 지우기

Plotting Trajectories on graph

조회 수: 24 (최근 30일)
Alexander
Alexander 2023년 3월 27일
댓글: Dyuman Joshi 2023년 3월 27일
Hi, I want to plot the trajectories of an object over a range of angles (theta = 5 - 85 degrees, increasing in increments of 5 degrees).
I am having trouble with the arrays, as I have an array for my angles, and then you can use these to find a value for tmax, which you then use to create an array for t (as shown in code below)
When I try to run this code, it only gives me a single value for my variables x and y. How do I get it so that i can get an array of x and y values over the range of t, at each value of theta. This is so that i can plot a graph that shows the trajectory of the object at each of these values of theta when given a value of inital velocity.
My thinking with the for loops is that the parent loop will calculate the values vx, vy and tmax at each angle of theta. Then the nested loop will calculate the values for x and y at each value of t given the value of theta from the parent loop. I think this approach would be mean i would need to get an output of 2 multi dimensional arrays which contain the x and y data over t, at each value of theta. I suspect this may be possible using some kind of user defined function, but in truth I have no idea.
Any help would be great. Thanks
Code shown below:
prompt = "Please enter an inital speed for the ball : ";
vo = input(prompt);
xo = 0;
yo = 0;
g=9.81;
for theta = 5:5:85
vx = vo*cosd(theta);
vy = vo*sind(theta);
tmax = 2.*vy/g;
for t = 0:0.01:tmax
x = xo + vx.*t;
y = yo + vy.*t - 0.5*g.*t.^2;
end
end
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 3월 27일
If you just need the data to plot, you can choose to plot the graphs without saving the data. That would require only a small modification in the code you wrote above.

댓글을 달려면 로그인하십시오.

채택된 답변

Joe Vinciguerra
Joe Vinciguerra 2023년 3월 27일
편집: Joe Vinciguerra 2023년 3월 27일
Because your results will be arrays of varying size you should consider using cells or structures to store the results. In this example I chose to use structures. You can also calculate entire arrays instead of indivudual values within a loop.
% prompt = "Please enter an inital speed for the ball : ";
% vo = input(prompt);
vo = 5;
xo = 0;
yo = 0;
g = 9.81;
% create an array instead of processing in a loop
theta = 5:5:85;
% each of these results will now also be arrays
vx = vo*cosd(theta);
vy = vo*sind(theta);
tmax = 2.*vy/g;
% preallocate a structure to store your results
results = struct('t',[], 'x',[], 'y',[]);
% create a figure to plot the results
figure; nexttile(); hold on; grid on; xlabel('x'); ylabel('y');
% loop through the calculations for each theta and plot that set of results
for i = 1:length(theta)
% create an array for t instead of looping through
results(i).t = (0:0.01:tmax(i))';
% store X and Y into the structure
results(i).x = xo + vx(i).*results(i).t;
results(i).y = yo + vy(i).*results(i).t - 0.5*g.*results(i).t.^2;
% plot the results
plot(results(i).x, results(i).y)
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Title에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by