Adjust the launching angle to produce the greatest range (i.e. maximum horizontal distance) but within 0° ≤ 𝜃଴ ≤ 90° in incremental of 0.1. Evaluate your results.

2 views (last 30 days)
I need help trying to code this,
The pdf attached shows the questions, question 2vi is a problem because when i plot it into a graph theres so many lines that its a blur to try and determine which angle creates the furthest horizontal distance. please someone help!!
  3 Comments
Walter Roberson
Walter Roberson on 23 Feb 2022
You plot() a 2d array. When you do that, one line is created for each column of the array. Try transposing the arrays
plot(x.', y.')

Sign in to comment.

Answers (3)

David Hill
David Hill on 23 Feb 2022
%% Question v
Y = @(t,theta)y0 - 1/2*g*t.^2 + (v0*sind(theta))*t;
X = @(t,theta)x0 + (v0*cosd(theta))*t;
%% Question vi
theta=0:.1:90;
a = -1/2*g;
b = v0*sind(theta);
c = y0;
ytminus = ((-b) - sqrt((b.^2)-4*a*c))/(2*a);
[~,idx] = max(x0 + (v0*cosd(theta)).*ytminus);
%%
figure(f);hold on;%previous f=figure; (for the figure to reuse)
plot(X(t,theta(idx)),Y(t,theta(idx)));
  7 Comments
Steven Lord
Steven Lord on 23 Feb 2022
Many functions in MATLAB can operate on arrays of data all at once and so don't need to be called once per element. For example if I wanted to take the sine of a vector of angles:
x = 0:0.1:2*pi;
I could do it one at a time:
y = zeros(size(x)); % Make an array of 0's the same size as x
for whichOne = 1:numel(x)
y(whichOne) = sin(x(whichOne)); % Fill in that array of 0's
end
Or since the sin function is vectorized I could do it in one fell swoop.
z = sin(x);
The two approaches give the same answer.
isequal(y, z) % true
ans = logical
1
David Hill
David Hill on 23 Feb 2022
Edited: David Hill on 23 Feb 2022
The power of matlab is in the use of matrx and array operations. If you examine the below code:
Y = @(t,theta)y0 - 1/2*g*t.^2 + (v0*sind(theta))*t;%anonymous function with inputs of time-array and theta
X = @(t,theta)x0 + (v0*cosd(theta))*t;%anonymous function
theta=0:.1:90;%array of theta [0 .1 .2 .3 .4 .... 90]
a = -1/2*g;%scalar
b = v0*sind(theta);%array since theta is an array sind(theta) produces an array
c = y0;%scalar
ytminus = ((-b) - sqrt((b.^2)-4*a*c))/(2*a);
% .^2 is an element-wise operation and squares each element of the b-array
% sqrt takes the sqrt of all elements of the array. -b-array - sqrt-array performs
% element-wise substraction (arrays must be the same size). /(2*a) divides
% each element by (2*a).
[~,idx] = max(x0 + (v0*cosd(theta)).*ytminus);% max finds the maximum value of the array (which we don't care about) and the index of the maximum value.
plot(X(t,theta(idx)),Y(t,theta(idx)));
%plots alls X values for the given time-array using the theta indexed at
%the maximum horizontal distance previously determined and similarily all
%corresponding Y values.
When using arrays, no for-loops are needed in the above code. If individual calculations were done, you would have to cycle through all the theta values going from 0 to 90 degrees.

Sign in to comment.


Kara Streader
Kara Streader on 23 Feb 2022
sorry i'm new to matlab, what are the @ symbols representing?

Torsten
Torsten on 23 Feb 2022
To answer vi), use ii) to determine t when the ball hits the ground for a given value of theta. Then, for this value for t, determine x.
If you do this for the 901 theta values, you'll get a vector of 901 corresponding x-values.
Determine the maximum x value within this vector and the corresponding theta value (use max).
Plot the x-y-trajectory within the graph from iii) (use hold on/off)

Products

Community Treasure Hunt

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

Start Hunting!