Finding value that gives the max when entered in equations

1 view (last 30 days)
Hello I am having trouble finding the value of theta that gives me the max distance when plugged into my code. I can find the max distance simply by using max but I am not sure how to find the value of theta that creates that max.
x0 = 0; %initial postion
y0 = 0;
v0= 20; %initial velocity
g = 9.81; %gravity
theta = 0:1:90; %change in angle
vx0=v0*cosd(theta); %horizontal component
vy0 = v0*sind(theta);%vertical component
distance = x0 + 2*vx0.*vy0/g;
Header = {'Angle of projection', 'Horizontal Distance'};
T = table(theta',distance','VariableNames',Header);
disp(T);
Max_Distance = max(distance);

Accepted Answer

KSSV
KSSV on 11 May 2021
Along with max value you can get the index also...use this index to get the value of respective theta.
clear ; clc;
x0 = 0; %initial postion
y0 = 0;
v0= 20; %initial velocity
g = 9.81; %gravity
theta = 0:1:90; %change in angle
vx0=v0*cosd(theta); %horizontal component
vy0 = v0*sind(theta);%vertical component
distance = x0 + 2*vx0.*vy0/g;
Header = {'Angle of projection', 'Horizontal Distance'};
T = table(theta',distance','VariableNames',Header);
disp(T);
[Max_Distance,idx] = max(distance); % <--- get the index of max value
Max_theta = theta(idx) ; %<---- this gives respective theta

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!