Saturday, November 10, 2007

Matlab VIII

If You Were to Read This Article...





Hello everyone, how are we all doing this evening? If you answer good, then I would have to output a "I'm glad" statement. Else if you were to answer bad, then I would probably output a "That's really too bad" statement. My else would just be "Try again". Yes yes, I did just write that opening with poor, incomprehensible English. But I was speaking in terms of Matlab language. This week on Matlab Tutorial, I was going to focus on talking about If, Elseif, and Else commands while working on Matlab programs.



The basic function of an If, Elseif, or Else statement chain of commands is to give the program some options of what exactly it will be computing. Based on what is inputted and what certain variable values are, the if statements help filter and sort these values based on parameters that you set within the code so that the program runs through different choices of computations. Basically if you want the program to do a certain calculation when an arbitrary variable has a certain value, then you write an if statement there along with what the program does when it meets those needs specified.



For example: Lets say you have the user input (back from Matlab II) a value for the variable X. You then have a line of code squaring the value of X. Now you want to have two separate chain of commands depending on what the new value Y (X^2) is. Write the "if" command on the first line followed by a condition. For this example, the condition will be when Y = 4. You must use double equal signs when writing conditional arguments. So now if Y does indeed fit the statement it will do whatever follows that code. So this example will then have Y be divided by 2 to equal the variable Z. Now that is one option. If you finish the if statement with an "end", then you have a simply if statement. But you usually want an if statement to have outputs for every possibility. So you can either write the "else" syntax after your first computation followed by a line telling it to simply make Y = Z, or I will take it one step further and add the "elseif" statement in this example program. After the if statement and the equation for Z, a line of code should start with an "elseif", telling Matlab, "if Y didn't equal 4, see if it equals this". Let's just say if Y = 5. If this is true then our next line with ask the program to multiply Y by 2 to get Z. If Y doesn't equal either, then Y will just equal Z. End your if statements with the end command and output Z. Whew, that was a little rough. Lets see this example in the correct syntax:

X = input('Value for X= ')
Y = X^2
if Y == 4
Z = Y/2
elseif Y == 5
Z = Y * 2
else
Z = Y
end
Z



As you can see, the syntax and the process of the if statement is simpler to that of a loop. the only difference is that an if statement won't loop around multiple times. It will only go through the statements once. Unless of course it is nested in a for loop! Not only does it go through once, but understand that it goes through in sequential order. So make sure that the more important arguments and decisions you want the program to make be the first if statements in your chain of commands. Slowly but surely we are putting all the pieces together.....Good luck and stay tune for more exciting adventures of BATMAN AND -...oops, I mean MATLAB TUTORIAL!



Ok, I'm done.....



Resources: While Loop Help with Cyclismo, HTML Tags at Web Source

No comments: