Using Euler's Method for projectile motion

9 views (last 30 days)
Problem statement: Write a program that employs the Euler method to compute the solution to the freely falling object. That is, calculate 𝑣 as a function of time. Consider different starting velocities over a time range from 𝑡 = 0 to 𝑡 = 10 s.
I'm not sure how to set up my time interval from 0-10 sec. I'm guessing "for j = t(1):dt:tf ". But not fully sure how to proceed. Any help would be appreciated. Thnak you.
What I have so far:
clear;
N = 100; % points to be taken
g = 9.8; % acceleration due to gravity (m/s^2)
dt = 0.01; % time step for time range of 1-10 sec.
theta = input('Angle of projection(in degrees): '); % Allows user to input
h = input('Initial height (in meters): ');
v_0 = input('Initial velocity (in m/s): ');
% Initial conditions below
x(1) = 0; % intial x position (m)
y(1) = h; % intial y position (m)
t(1) = 0; % inital time (s)
tf = 10; % final time (s)
v(1) = v_0; % initial velocity (m/s)
v_x = v_0*cos(theta); % "x" component of the velocity
v_y = v_0*sin(theta); % "y" component of the velocity
v_x(1) = v_x;
v_y(1) = v_y;
n = 1;
while n < N
% for statement here?
v(n+1)=sqrt((v_x(n)^2)+(v_y(n)^2));
v_x(n+1) = v_x(n);
v_y(n+1) = v_y(n) - g*dt;
x(n+1) = x(n) + v_x(n)*dt;
y(n+1) = y(n) + v_y(n)*dt;
if y(n+1)<0
n = N;
else
n = n+1;
end
end

Accepted Answer

Alan Stevens
Alan Stevens on 2 Nov 2022
A little more like this? (I've used arbitrary values for initial conditions etc.)
N = 200; % points to be taken
g = 9.8; % acceleration due to gravity (m/s^2)
dt = 0.01; % time step for time range of 1-10 sec.
theta = 45; %input('Angle of projection(in degrees): '); % Allows user to input
h = 1; %input('Initial height (in meters): ');
v_0 = 5; %input('Initial velocity (in m/s): ');
% Initial conditions below
x(1) = 0; % intial x position (m)
y(1) = h; % intial y position (m)
t(1) = 0; % inital time (s)
tf = N*dt; % final time (s)
v(1) = v_0; % initial velocity (m/s)
v_x = v_0*cos(theta); % "x" component of the velocity
v_y = v_0*sin(theta); % "y" component of the velocity
v_x(1) = v_x;
v_y(1) = v_y;
n = 1;
for n = 1:N
v(n+1)=sqrt((v_x(n)^2)+(v_y(n)^2));
v_x(n+1) = v_x(n);
v_y(n+1) = v_y(n) - g*dt;
x(n+1) = x(n) + v_x(n)*dt;
y(n+1) = y(n) + v_y(n)*dt;
t(n+1) = t(n) + dt;
if y(n+1)<0
break
end
end
plot(x,y),grid
xlabel('x'),ylabel('y')
  3 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!