Sie sind auf Seite 1von 4

MATLAB Lab 09

a) lab09_1.m
function f=lab09_1(t,v) % Order of input of v = [x y vx vy] %% Defining constants volironball = (4/3)*pi*(0.1)^3; %m3 rhoironball = 7874; %kg/m3 massironball = rhoironball*volironball; %kg g = 9.80665; %m/s2 rhoair = 1.2; %kg/m3 turdrag = 0.5; %Cd R=0.1; %m %% Defining ODEs f(1) = v(3); f(2) = v(4); f(3) = ((-0.5)*pi*(R^2)*rhoair)*(turdrag/massironball)*(v(3))^2; f(4) = -g+((-0.5)*pi*(R^2)*rhoair)*(turdrag/massironball)*(v(4))^2; f=f'; %transposing to column vector for ode operation

canonballplot.m
clc;clear;clf %% Specifying the ODE parameters and solving the ODE tspan=[0 10]; %s V0=25; %m/s d=45; %degrees v0=[0; 2; V0*cosd(d); V0*sind(d)]; % The four initial conditions x,y,vx and vy [t, v]=ode45(@lab09_1,tspan,v0); %% Plotting the two graphs subplot(1,2,1); %top graph plot(v(:,1),v(:,2),'r-'); % The first column of v gives the x values, while the second gives the y values of the cannonball xlabel('horizontal displacement (m)'); ylabel('vertical displacement (m)'); title('A graph of the trajectory of the cannonball'); grid on subplot(1,2,2); %bottom graph plot(t,v(:,2),'b-'); xlabel('time (s)'); ylabel('vertical displacement (m)'); title('A graph of the vertical displacement of the cannonball versus time'); grid on

The trajectory is a parabolic curve that increases to a maximum before decreasing to the negative y region. There is no physical meaning for the negative region of the curve, since the cannonball hits the ground at y=0. From the second graph at the right, it can be seen that the time taken for the cannonball to hit the ground is roughly about 3.7 seconds.

b) lab09_2.m
function [dummy, terminate, direction] = lab09_2(t,v) % Specifying the events for the ODE solver dummy = v(2); % basically dummy is the vertical displacement of the cannonball terminate = 1; % this statement is to terminate the ode solver direction = -1; % ode solver will stop when dummy becomes negative

cannonballplot2.m
clc;clear;clf %% Specifying the ODE parameters and solving the ODE tspan=[0 10]; %s V0=25; %m/s d=45; %degrees v0=[0; 2; V0*cosd(d); V0*sind(d)]; % The four initial conditions x,y,vx and vy [t, v]=ode45(@lab09_1,tspan,v0,odeset('Events',@lab09_2)); % using odeset this time %% Plotting the trajectory of the cannonball plot(v(:,1),v(:,2),'r-'); % trajectory of the cannonball xlabel('horizontal displacement (m)');

ylabel('vertical displacement (m)'); title('A graph of the trajectory of the cannonball'); grid on

When the vertical displacement of the cannonball reached 0, the ode solver stopped, and thus the graph only shows the trajectory of the cannonball until it hits the ground. c) cannonballplot3.m
clc;clear;clf %% Specifying the fixed ODE parameters tspan=[0 10]; %s V0=25; %m/s %% Solving the ODE in a for loop and plotting the trajectories d=0:20:80; % A row matrix of different launch angles in degrees linecolor=['r-';'b-';'g-';'k-';'m-']; % A column vector listing different colors for i=1:numel(d) v0=[0; 2; V0*cosd(d(i)); V0*sind(d(i))]; [t, v]=ode45(@lab09_1,tspan,v0,odeset('Events',@lab09_2)); plot(v(:,1),v(:,2),linecolor(i)); % Plotting the trajectory using different line colors hold on end xlabel('horizontal displacement (m)'); ylabel('vertical displacement (m)'); ylim([0 35]);

text(0,2,'x','HorizontalAlignment','center'); title('A graph of the trajectory of the cannonball'); legend('Launch at 0\circ','Launch at 20\circ','Launch at 40\circ','Launch at 60\circ','Launch at 80\circ'); grid on

Das könnte Ihnen auch gefallen