Plotting Trajectories on graph

40 次查看(过去 30 天)
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 中查找有关 Graphics Performance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by