Plotting Several Data Sets in a For Loop
21 views (last 30 days)
Show older comments
George on 8 Mar 2023
Commented: William Rose on 9 Mar 2023
I am working through a problem that requires me to calculate trajectories of an object based on the angle the object is initially thrown. The end goal of this portion of the problem is to have one graph that shows all of the trajectories on one plot. The angles I am trying to iterate through are 0 to 80 in steps of 10 degrees. However, when I run the script, I am getting one plot where all of the data is the same and all of the data (regardless of the angle) has the same output (as evidenced by the legend on the plot). If I change the angle variable to just one value (i.e. to 10, instead of 0:10:80), the program will successfully output a graph for that angle. Can someone help me sort out what my issue is?
% Constant Variables
m=0.175; %mass, kg
A=(pi/4)*D^2; %Area, m^2
rho_air=1.23; %Density of air, kg/m^3
AoA_rad=deg2rad(-4); %Angle of Attack for Drag Calc.
title('Frisbee with Constant Velocity and Varying Angles')
xlabel ('Horizontal Distance (m)')
for j=0:10:80 %Angles to iterate through
%Defining and Summing Forces
%Finding Accelerations and New Velocity
%Finding x and y coordinates
which is not correct, given how you define variables. You define i as current time. xprev is updated on each inner loop pass. Therefore you should be using dt, the step size, rather than i=time, for this update step. Your equation for y_new has the same issue.
Inspect your inner loop code to identify calculations that can be pulled to the outer loop - that is, anything that is the same on each inner loop pass.
I would use NxM arrays for X, Y, Vel, etc., where N=length(time) and M=number of angles.
I would change the loop variables to integers and use them as indices for the arrays x(i,j) and y(i,j), which you will fill up as you go through the loops.
Here is a simple example for a ball launched at different angles. Your system is more complicated, so your equations for ax, ay will be different.
dt=.01; % time step (s)
time=0:dt:2; % time is not used in the loops, but you could use it to plot(t,x(:,j)), etc
g=-9.8; % y accel m/s^2
x=zeros(N,M); y=zeros(N,M); % allocate arrays for x, y
x(1,:)=zeros(1,M); % initial distance (m)
y(1,:)=ones(1,M); % initial height (m)
v0=14; % initial veloc (m/s)
ax=0; % no x acceleration in my simple model
ay=g; % y acceleration
y(i,j)=max(y(i-1,j)+dt*vy,0); % don't allow y<0
vx=vx+dt*ax; % update vx
vy=vy+dt*ay; % update vy
legend(legstr); grid on;
xlabel('Distance (m)'); ylabel('Height (m)');
That gives you some ideas.
Your script incldues the lines
After that, you use u0x and v0y to update x and y. In other words, the equations above are the x, y components of instantaneous velocity. I think the equations are incorrect, because they use alpha_rad, which is the initial launch angle. Alpha_rad does not get updated as the trajectory changes. It should. Or you can do what I did, which is to update vx and vy, based on the accelerations ax and ay.
vx=vx+dt*Fx/m; % update vx
vy=vy+dt*Fy/m; % update vy
Use vx and vy to update the positions:
x(i,j)=x(i-1,j)+dt*vx; % update x
y(i,j)=y(i-1,j)+dt*vy; % update y
That may explain the difference.
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!