• MATLAB how to make loop that user keeps pressing a negative number

    ShikhaTan Member

    How do I keep looping so that if the user keeps pressing a negative number it keeps displaying the message “error”?

    g=9.81
    V0=input ('Enter initial velocity of projectile (m/s):')
    if V0<0
    disp ('Error. Velocty must be greater than 0');
    V0=input ('Enter initial velocity of projectile (m/s):')
    else
    x=input ('Enter target location in X direction (>0m):')
    theta=(asin(x*g/V0^2))/2
    end
  • Adan Member

    you have to do is to give an initial value to V0 for instance: V0=-1
    and then run the loop(put the whole code inside this loop)

  • Abhey Member

    If i keep inputting a negative answer it keeps repeating which i want
    but if i run the program from the beginning and input a vale >0 for V0 it jsut falls through and gives an answer for theta without asking for the x value

    g=9.81
    V0=0
    
    V0=input ('Enter initial velocity of projectile (m/s):')
    while (V0<0)
    disp ('Error. Velocty must be greater than 0');
    
    V0=input ('Enter initial velocity of projectile (m/s):')
    if (V0>0)
    x=input ('Enter target location in X direction (>0m):')
    theta=(asin(x*g/V0^2))/2;
    end
    end
     theta
    
Viewing 2 reply threads
  • You must be logged in to reply to this topic.
en_USEnglish