Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB giving 0 value for if/then statement

  1. Nov 16, 2009 #1
    Hi again! Working with an if/then statement and running into trouble. Here's my code.

    Code (Text):
    a=2;
    N = input('Enter the number of applied loads:');
    for i=1:N
        L(i)=input('Enter the force of the applied load in kN:');
        D(i)=input('Enter the position of the applied load in meters:');
    end
    if D(i) > a
        Z(i)=L(i)*-1
    else Z(i)=L(i)
    end
    And here is the result:
    Code (Text):
    Enter the number of applied loads:2
    Enter the force of the applied load in kN:1
    Enter the position of the applied load in meters:1
    Enter the force of the applied load in kN:3
    Enter the position of the applied load in meters:3

    Z =

         0    -3

    >> D

    D =

         1     3

    >> L

    L =

         1     3
    Why is Z(1) coming out zero? With my if/then statement, what I want is if D>a, then L= -1*L. If not, it should just stay the same, not return a zero.

    Any ideas? :confused:


    edit: Interesting....
    Code (Text):
    >> projectdebug
    Enter the number of applied loads:10
    Enter the force of the applied load in kN:5
    Enter the position of the applied load in meters:5
    Enter the force of the applied load in kN:5
    Enter the position of the applied load in meters:5
    Enter the force of the applied load in kN:5
    Enter the position of the applied load in meters:5
    Enter the force of the applied load in kN:5
    Enter the position of the applied load in meters:5
    Enter the force of the applied load in kN:5
    Enter the position of the applied load in meters:5
    Enter the force of the applied load in kN:5
    Enter the position of the applied load in meters:5
    Enter the force of the applied load in kN:5
    Enter the position of the applied load in meters:5
    Enter the force of the applied load in kN:5
    Enter the position of the applied load in meters:5
    Enter the force of the applied load in kN:5
    Enter the position of the applied load in meters:5
    Enter the force of the applied load in kN:5
    Enter the position of the applied load in meters:5

    Z =

         0     0     0     0     0     0     0     0     0    -5
    What's going on?! :cry:
     
    Last edited: Nov 16, 2009
  2. jcsd
  3. Nov 16, 2009 #2

    Mark44

    Staff: Mentor

    I think you want to embed this code in a for loop as well:
    Code (Text):

    if D(i) > a
        Z(i)=L(i)*-1
    else Z(i)=L(i)
    end
     
    After your first loop exits, what's the value in i? I don't know, and your problem is probably related to i not being defined outside your loop. Alternatively, i could be the first value that caused it to exit the first for loop, which is probably not what you intended.
     
  4. Nov 16, 2009 #3
    That's it! :smile: After the loop, it put i=2, which probably caused problems. I just threw the same if/then statement in another for loop, with "for i=1:N", and it works. Thanks a bunch, Mark! :wink:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: MATLAB giving 0 value for if/then statement
  1. Matlab if statements (Replies: 1)

Loading...