Problem with conditions in If statement
Show older comments
i am trying to fool proof my code. If i were to put 3 as the input for the varible, unit; it will loop back one but then skip a couple of commands.
Sorry, if my explainantion doesn't make sense :)
This screenchot is me just following the directions:

Now if I were to try and enter a number other than 1 or 2:

function [vf, k]=hhvelocity10
clc
close all
format compact
fprintf('\n')
unit=input(['Enter 1 to work with an initial velocity or enter 2 to work \n ' ...
'with a pre-selected initial velocity = '],'s');
fprintf('\n')
initialvelocity='1';
preinitialvelocity='2';
if contains (unit,initialvelocity)
g=9.8;
InitialVelocity=input('Please give me the Initial Velocity in meters per second = ');
fprintf('\n')
InitialHeight=input('Please give me the Initial Height in meters = ');
fprintf('\n')
y=InitialHeight;
v=InitialVelocity;
vf=sqrt(2*g*y+v.^2);
fprintf('The velocity is %.2f m/s \n', vf);
fprintf('\n')
elseif contains (unit,preinitialvelocity)
fprintf('Your pre-selected velocity is 10 m/s. \n')
fprintf('\n')
g=9.8;
InitialHeight=input('Please give me the Initial Height in meters = ');
fprintf('\n')
y=InitialHeight;
v=10;
vf=sqrt(2*g*y+v.^2);
fprintf('The velocity is %.2f m/s \n', vf);
fprintf('\n')
else
(unit~=1 || unit~=2);
disp('Please enter 1 or 2.')
unit=input(['Enter 1 to work with an initial velocity or enter 2 to work \n ' ...
'with a pre-selected initial velocity = '],'s');
fprintf('\n')
end
mu=input(['Please enter the coefficient of friction of the rough \n' ...
'horizontal surface = ']);
fprintf('\n')
m=input('Please enter the mass of the object in kg = ');
fprintf('\n')
f=mu*m*g;
x=input(['Please enter the distance you want the object to travel on \n' ...
'the rough horizontal region in meters = ']);
fprintf('\n')
x_s=input(['How far would you like for the object to compress \n' ...
'the spring in meters? ']);
fprintf('\n')
v_f=vf;
v_i=v_f;
x_f=x_s;
k=(2*(-f)*(x+x_s))/(x_f.^2)+(m*v_i.^2)/(x_f.^2);
2 Comments
Dyuman Joshi
on 2 May 2022
So if I am understanding it correctly, If someone enters a value other than 1/2, you want to ask them to enter again, either 1 or 2?
Miya Harris
on 2 May 2022
Accepted Answer
More Answers (0)
Categories
Find more on Assembly 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!