Clear Filters
Clear Filters

how do I make the function stop showing output arguments if an invalid input is entered.

2 views (last 30 days)
function [max_h,H_dist]= A09Prob3_baler_erastogi(initial_v,launch_angle)
if ~(initial_v>=9.5 & initial_v<=13.25) %initial velocity limitation [m/s]
fprintf('Initial velocity input is invalid \n')
end
if ~(launch_angle>=30 & launch_angle<=60) % angle limitation [deg]
fprintf('Launch angle input is invalid \n')
end
t= (initial_v*sin(launch_angle))/g; % maximum time [s]
max_h= 1.25+(initial_v*sin(launch_angle)*t)-(0.5*g*t^2); % maximum height in m
H_dist= (initial_v*cos(launch_angle))*t; % horizontal distance in m
end

Answers (1)

Ameer Hamza
Ameer Hamza on 28 Sep 2020
Edited: Ameer Hamza on 28 Sep 2020
You can use assert(): https://www.mathworks.com/help/matlab/ref/assert.html. Which will throw an error if invalid input is given
function [max_h,H_dist]= A09Prob3_baler_erastogi(initial_v,launch_angle)
assert((initial_v>=9.5 & initial_v<=13.25), 'Initial velocity input is invalid')
assert((launch_angle>=30 & launch_angle<=60), 'Launch angle input is invalid')
t= (initial_v*sin(launch_angle))/g; % maximum time [s]
max_h= 1.25+(initial_v*sin(launch_angle)*t)-(0.5*g*t^2); % maximum height in m
H_dist= (initial_v*cos(launch_angle))*t; % horizontal distance in m
end
If you don't want to throw an error, then you can simply return, but in that case, you will need to provide values for output variables
function [max_h,H_dist]= A09Prob3_baler_erastogi(initial_v,launch_angle)
max_h = 0;
H_dist = 0;
if ~(initial_v>=9.5 & initial_v<=13.25) %initial velocity limitation [m/s]
fprintf('Initial velocity input is invalid \n');
return
end
if ~(launch_angle>=30 & launch_angle<=60) % angle limitation [deg]
fprintf('Launch angle input is invalid \n')
return
end
t= (initial_v*sin(launch_angle))/g; % maximum time [s]
max_h= 1.25+(initial_v*sin(launch_angle)*t)-(0.5*g*t^2); % maximum height in m
H_dist= (initial_v*cos(launch_angle))*t; % horizontal distance in m
end

Categories

Find more on Geographic Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!