A team of engineering students is writing a MATLAB program that can accurately c
ID: 3667831 • Letter: A
Question
A team of engineering students is writing a MATLAB program that can accurately calculate and plot the projectile motion of shooting a cannonball at any height, shooting angle, and initial velocity based on the user input. This program will also simulate the cannonball target practice result. Place a 10 meters tall target on the ground at any horizontal distance the user specifies, the program will predict hit or miss.
The program should gather the following information from the MATLAB command window:
The vertical height of the cannon in meters.
The shooting angle in degrees.
The initial velocity in meters per second (m/s).
The horizontal location of the target in meters.
Based on the height of the cannon, the shooting angle, and the initial velocity, the program should calculate the maximum height (hmax) the cannonball can reach, the horizontal distance (x) the cannonball travels, and the total time (ttotal) the cannonball is in the air.
Format the answers to display 3 digits after the decimal point in the command window.
The program should then separate the total time (ttotal) the cannonball is in the air into 1000 equally spaced time points. Compute the x and y values of each time point.
Plot the projectile motion of the cannonball in the x and y coordinates from the moment the cannonball is released in the air to the moment the cannonball reaches the ground.
Plot a line that represents the height of the cannon in the same graph.
Plot the 10 meters tall target in the same graph.
Add plot title, legends, and label each axis clearly.
After the computation, add an if..elseif..else..end statement in the program to analyze if the cannonball will hit the target and print results in the command window for the user.
Explanation / Answer
function cannonball %% Initialize MATLAB clear all; close all; clc; %% Global variables didCollide = 0; keepPlaying = 1; rotationVector = linspace(360, 0, 250) * pi/180; while keepPlaying == 1 % Conditional loop to ensure that the user wants to play again %% Generate shapes square = [[0 5 5 0 0];[0 0 5 5 0]]; cannon = [[5 10 9 4 5];[2 6 8 4 2]]; cannonBase = [[3 5 6 2];[0 4 0 0]]; target = [[100 110 110 100 100];[80 80 90 90 80]]; %% Prepare the figure window figure(1) axes('xlim', [0 275], 'ylim', [0 200]); hold on; %% Generate random variables randX = randi([0, 100]); %Used to shift the x position of the target randY = randi([0, 110]); %Used to shift the y position of the target %% Add translations for target and fill targetfill = fill(target(1,:) + randX, target(2,:) + randY, 'b'); %% Draw cannon and base on figure baseFill = fill(cannonBase(1,:)*3, cannonBase(2,:)*3, 'k'); cannonFill = fill(cannon(1,:) * 3, cannon(2,:) * 3, 'r'); %% Get user input and convert to integers v = inputdlg('Enter desired speed: ', 'Fire!'); velocity = str2num(v{1}); %Conversion to ensure that the program can understand the input a = inputdlg('Enter desired angle: ', 'Fire!'); angle = str2num(a{1}); %Conversion to ensure that the program can understand the input %% Generate equations t = linspace (0,7, 250); %Time to run the simulation for vx = velocity * cosd(angle); %Horizontal component of velocity vy = velocity * sind(angle) - (9.8*t); %Vertical component of velocity %% Perform animation i = 1; while iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.