Plot results of a While loop
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Raphael Biendarra
il 11 Ott 2020
Modificato: Raphael Biendarra
il 11 Ott 2020
Hi,
The code calculates the velocity and the x and y position of a PingPong ball for several time intervalls. Each time intervall is 1/100 seconds
I want to plot all results from the while loop so I can see how "y_final" and "x_final" vary with time.
The problem is when I run the code, I get only a emty graph.
Can someone give me a hint?
..................................................................................................................
clear
% specify initial position of ball (i.e. point where ball leaves laucher):
x_0 = -0.05; %m
y_0 = 0.1; %m
% specify initial velocity and angle
u_0 = 5; % m/s
theta_0 = 10*(pi/180); % rads
%specify length of time-step for calculation
delta_t = 1/100; % s
%let i_ts be the time-step number
i_ts = 1; % (i.e. this is the first time-step
t(i_ts) = 0; % s
% set initial conditions for first time-step
x_init = x_0;
y_init = y_0;
ux_init = u_0 * cos(theta_0);
uy_init = u_0 * sin(theta_0);
% record the position and velocity of the ball at the start of the
% time-step
x_ball(i_ts) = x_init;
y_ball(i_ts) = y_init;
ux_ball (i_ts) = ux_init;
uy_ball (i_ts) = uy_init;
% find the acceleration acting on the projectile at the start of the
% time-step
accel_x = 0;
accel_y = -9.81;
% calculate final velocity and position at the end of the first time-step
x_final = x_init + ux_init*delta_t + 0.5*accel_x*(delta_t^2);
y_final = y_init + uy_init*delta_t + 0.5*accel_y*(delta_t^2);
ux_final = ux_init + accel_x*delta_t;
uy_final = uy_init + accel_y*delta_t;
while y_final>0;
delta_t = delta_t + 0.01;
x_final = x_init + ux_init*delta_t + 0.5*accel_x*(delta_t^2);
y_final = y_init + uy_init*delta_t + 0.5*accel_y*(delta_t^2);
ux_final = ux_init + accel_x*delta_t
uy_final = uy_init + accel_y*delta_t
figure(1)
plot (x_final, y_final, ux_final, uy_final);
ylim([0 1000]);
xlim([ 0 10]);
end
hold on
box on
.......................................................................................................................
1 Commento
Asad (Mehrzad) Khoddam
il 11 Ott 2020
You have plotted y_final vs x_final that is only one point. You should plot them vs time. t_final, x_final and y_final should be a vector of values to be plotted
Risposta accettata
Image Analyst
il 11 Ott 2020
There are a ton of errors in that and I didn't fix your homework 100%, but I fixed a lot of things, like the plotting. See if you can discover the error to complete it:
clc; % Clear the command window.
close all; % Close all figures
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
fprintf('Beginning to run %s.m ...\n', mfilename);
% specify initial position of ball (i.e. point where ball leaves laucher):
x_0 = -0.05; %m
y_0 = 0.1; %m
% specify initial velocity and angle
u_0 = 5; % m/s
theta_0 = 10*(pi/180); % rads
%specify length of time-step for calculation
delta_t = 1/100; % s
%let i_ts be the time-step number
i_ts = 1; % (i.e. this is the first time-step
t(i_ts) = 0; % s
% set initial conditions for first time-step
x_init = x_0;
y_init = y_0;
ux_init = u_0 * cos(theta_0);
uy_init = u_0 * sin(theta_0);
% record the position and velocity of the ball at the start of the
% time-step
x_ball(i_ts) = x_init;
y_ball(i_ts) = y_init;
ux_ball (i_ts) = ux_init;
uy_ball (i_ts) = uy_init;
% find the acceleration acting on the projectile at the start of the
% time-step
accel_x = 0;
accel_y = -9.81;
% calculate final velocity and position at the end of the first time-step
x_final = x_init + ux_init*delta_t + 0.5*accel_x*(delta_t^2);
y_final = y_init + uy_init*delta_t + 0.5*accel_y*(delta_t^2);
ux_final = ux_init + accel_x*delta_t;
uy_final = uy_init + accel_y*delta_t;
loopCounter = 1;
maxIterations = 1000; % Failsafe
while y_final>0 && loopCounter < maxIterations
delta_t = delta_t + 0.01;
x_final = x_init + ux_init*delta_t + 0.5*accel_x*(delta_t^2);
y_final = y_init + uy_init*delta_t + 0.5*accel_y*(delta_t^2);
ux_final = ux_init + accel_x*delta_t;
uy_final = uy_init + accel_y*delta_t;
fprintf('For iteration %d, ux_final = %.4f, uy_final = %.2f\n', loopCounter, ux_final, uy_final);
% Draw line from (x, y) to (ux, uy).
plot ([x_final, ux_final], [y_final, uy_final], 'b.-', 'MarkerSize', 15, 'LineWidth', 2);
hold on;
drawnow; % See it "live" as it does each iteration.
loopCounter = loopCounter + 1;
end
grid on
box on
2 Commenti
Image Analyst
il 11 Ott 2020
I've been using MATLAB 14 years and had decades of programming experience before that. What I tell people who program for me are these 3 things:
- Use descriptive variable names, even if they're longer. Like loopCounter instead of "cnt" like most programmers seem to use. No one can maintain a program that looks like alphabet soup of one and two letter variable names.
- Put in way, way more comments than you think you'll need. And put them in as you program. Don't think you will come along later and put them in because you never do. Comments are needed by others who might inherit your code. Heck even I need comments to understand what I did just a few months or years ago. See my attached comment counter program. For my, about 37% of my code is either a full comment, or has a comment on the line.
- Anticipate what might possibly go wrong. Use try/catch. I'd say probably a third of my code is code to handle potential errors and correct them or alert the users. Like they say, it's hard to make something idiot proof because idiots are so ingenius. I'm continually surprised by the bone-headed things my users tried to do. I guess I shouldn't be because it's so common that they don't follow instructions or put in data that's not in the expected format.
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!