In: Electrical Engineering
MATLAB
Write a user defined function for a projectile motion. If a ball is launched from initial position(0,0) with a velocity v0 at angle θ, determine your horizontal and vertical position. Please plot x vs. t, y vs. t and y vs. x.
clc;
close all;
clear all;
v0 = 20; % Initial velocity is 10 m/s
theta = 45 ; % Launch angle in degrees
vx0 = v0 * cos (theta*pi/180); % the initial velocity
along the x-axis is vx0
vy0 = v0 * sin (theta*pi/180); % the initial velocity along the
y-axis is vy0
t = 0:0.01:3; % time
g = 9.8;% acceleration due to gravity is g
vx = vx0; % the velocity along the x-axis is vx ,
x = vx * t; % Horizontal distance
y = vy0 .*t - 1/2* g * t.^2; % Vertical distance
% for k = 1:length(t)
% if y(k)<-0.1
% y(k) = 0;
% x(k) = 0;
% end
% end
figure;plot(t,x);grid;xlabel('time (seconds)');ylabel('x
- distance (m)');title('t vs x');
figure;plot(t,y);grid;xlabel('time (seconds)');ylabel('y -
distance(m)');title('t vs y');
figure;plot(x,y);grid;xlabel('x - distance (m)');ylabel('y - distance (m)');title('x vs y');