Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

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

Thursday, October 11, 2007

Matlab Tutorial IV

More Fun with Graphs



So if you read the last issue of Matlab Tutorial, Matlab Tutorial III , then you remember the skill of making graphs. We covered the basic two variable plot, titles, labels for the axes, and even the hold on and hold off graphs. Well in this issue we will cover how to distinguish each graph with different visual designs and options.




If you do any kind of customizing for your graphs, at least if you plan on changing the line design, color, and symbol used to represent a line or points, then you must input any of the syntax within the plot command. The two inputs that can be added to the end of your plot command is a letter for a certain color and a symbol for the point marker type.



Different colors that can be selected, at least from the source I found not limited to, are yellow (y), magenta (m), cyan (c), red (r), green (g), blue (b), white (w), and black (k). This syntax comes before the point marker type syntax. Adding color makes it much easier to tell the difference between multiple graphs, especially when they are on the same plot graph.



The different point marker types vary in design and type. Some can be used for a set of points, and some can be used for continuous functions. A few to pick from are point (.), circle (o), X-mark (x), plus (+), solid (-), star (*), dotted (:), dashed-dot (-.), and just dashed (--). Not only does this further individualize each graph you make, but it looks cool too, especially the stars!



So using the above symbols for graph syntax you, write them in the plot function inside their own set of single quotes after the second variable followed by a comma: plot (X, Y, 'r:') This example would give you a plot of red dotted lines.



Well that's all I have to contribute to the Matlab Tutorial, everyone have fun at making their graphs. Stay tune for the next issue and program on!





Resources : CTM: Plotting in Matlab

Wednesday, October 10, 2007

Free Time Programming

Over the break, I had a significant amount of free time that I could spend making a good program for fun, not for class. So, I did. I made a world with one species of plant, one species of herbivores and one species of carnivores.

They have rather simple behaviors. Plants just throw off seeds every so often. For the animals, it's a little more complex. If they see something that can kill them (in the case of the herbivores), they run away from it. If they see something they can mate with (For herbivores, an herbivore of the opposite sex. For carnivores, any other carnivore - they're hermaphroditic), they move towards it and mate with it. If they see something they can eat, they move towards it and eat it. If there aren't any of those things, they move around to look for them.

Here's a screenshot. The green dots are plants, the blue dots are herbivores, and the red dots are carnivores. On the right side are the population and average genome of the three groups.

Yes, genome. Every individual has its own genes that determine things like how fast it moves or its range of vision (for animals) or how frequently it throws off seeds and how many seeds it throws off when it does (for plants). A parent will pass its genes onto its children with a small chance of mutation.

Although this all works properly as I programmed it, there are some things I should change the next time I have time. The most obvious, and easiest to fix is that of instant gestation and maturation. That is, as soon as two creatures mate, there the offspring is, and fully grown at that. No pregnancy and no period of youth.

Also, there should be more genes. Genes that determine things like how likely a gene is to be inherited from the father or mother, so it's not a static 50% or genes that determine how likely a mutation is to occur.

And what is to me, the most important (and hardest) change is to make behavior much more flexible. For example, mating is always given a higher priority than eating, even if the creature is literally starving. I'd like to change that so they can decide to do different things in different circumstances. Those could also be inheritable traits.


This is a screenshot from the same simulation as before, but significantly later.

Tuesday, October 2, 2007

Matlab Tutorial III



Plotting Data on Graphs



So I have thought of a new topic to discuss on Matlab Tutorial, that can help everyone out there make a better visual representation of their data that they are working with on Matlab. I am going to teach everyone how to make graphs. Now graphs are pretty simple to make, but there are a lot of extra little features you can add to make your graphs more colorful and easier on the eyes.



First, to understand what exactly we can plot. So far the only thing I have perfected is plotting two variables on an X-Y axis. To do this, you most have two arrays or sets of data (both of the same size) that are defined in the program first. After you have the variables labeled and defined, then type plot (X,Y) , where X is the label of your independent variable vector or array.



Now that you have your beautiful graph, you should put some other features on it to add just a little more customization to your Matlab graph. One thing that you should do just as a rule of thumb is have a title for the graph. The title usually should be relevant to the axes or what the graph is showing, but virtually anything can be written. The syntax for this command is title ('Your Title!') . It is important to write the '' in your syntax, otherwise the text won't show up.



Another good thing to add to your graph for informational and entertainment-al purposes is to also label your X and Y axes. This can be done by writing xlabel ('Your X Label Here!') or ylabel ('Y Label Here!') in your syntax. Again, keep the '' in there and make the labels somewhat relevant to your graph. Usually it is best to write down the axes' variables and their units.



For the instances that you want to graph multiple graphs on one plot, you have to use the hold command to keep them together and not on seperate graphs. First you must plot the first graph using the previously learned plot command. The next command that should be used is the hold command which starts with hold on, the second plot, and then finally hold off. The hold on tells Matlab to keep the graph being used for the first graph to stay in place. Essentially, you could have as many graphs as you want on one plot, but that would get complex and messy, especially using multiple axis ranges and variables. The "hold off" is used to turn off the hold command for any other commands you use in the program to not be plotted in the same display.



After you master these simple things, I can show you ways to change the graphs colors and and legends and other cool things!!! OK honestly, it's not THAT cool, but it is nice to see something that you created on the screen. It gives you a sense of accomplishment. Well till the next exciting addition of Matlab Tutorial, you stay classy San Diego!



References: University of Florida Matlab page; University of Michigan Engineering Matlab page; MathWorks Resource Page