MATLAB giving 0 value for if/then statement

In summary, the conversation revolved around a code that used an if/then statement to determine the value of Z based on the values of D and L. The person was running into trouble and asked for ideas on why the output for Z(1) was coming out as zero. It was discovered that the problem was due to the value of i not being defined properly outside of the loop, and the solution was to embed the code in another for loop with "for i=1:N". This resolved the issue and the code now works as intended.
  • #1
bakin
58
0
Hi again! Working with an if/then statement and running into trouble. Here's my code.

Code:
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:
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:
>> 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:
Physics news on Phys.org
  • #2
I think you want to embed this code in a for loop as well:
Code:
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.
 
  • #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:
 

Related to MATLAB giving 0 value for if/then statement

1. Why is my if/then statement in MATLAB giving a 0 value?

There are several reasons why an if/then statement in MATLAB may be giving a 0 value. One possibility is that the condition being evaluated is not being met, causing the statement to return a false value. Another possibility is that there is an error in the syntax or logic of the statement. It is important to carefully check the condition and code to ensure they are correct.

2. How can I troubleshoot an if/then statement in MATLAB that is returning a 0 value?

If your if/then statement is returning a 0 value, there are a few steps you can take to troubleshoot the issue. First, check the condition being evaluated to make sure it is correct. Next, check for any errors in the syntax or logic of the statement. You can also try using the debugger tool in MATLAB to step through the code and see where the issue may be occurring.

3. Is a 0 value always an error in an if/then statement in MATLAB?

No, a 0 value in an if/then statement in MATLAB does not always indicate an error. In some cases, the condition being evaluated may simply be false, causing the statement to return a 0 value. It is important to carefully consider the logic of the statement to determine if a 0 value is expected or if there is an error in the code.

4. Can variables affect the value returned by an if/then statement in MATLAB?

Yes, variables can affect the value returned by an if/then statement in MATLAB. If the condition being evaluated includes variables, their values will be used to determine the outcome of the statement. It is important to make sure the variables are properly defined and have the correct values to ensure the statement returns the desired result.

5. How can I modify my if/then statement in MATLAB to avoid a 0 value?

If you are consistently getting a 0 value from your if/then statement in MATLAB, you may need to modify the code to ensure the condition being evaluated is met. This could involve changing the logic of the statement or modifying the values of any variables involved. It may also be helpful to consult the MATLAB documentation or seek assistance from other users to troubleshoot the issue.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
Replies
14
Views
722
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
741
Back
Top