Showing posts with label Matlab. Show all posts
Showing posts with label Matlab. Show all posts

Friday, November 30, 2007

Matlab Tutorial

Bubble Sorting



Welcome back to Matlab Tutorial, last week I didn't post an article but I am back this week to give some more fun tips and commands. For this week, I was going to do kind of a fun program that is useful to help organize and manipulate data points. It is what some call a bubble sort. This is where all the values that you are given can be sorted from increasing values to decreasing values or vise verse. It is a simple nested for loop that can help you when you want to find the median of a data set or the mode of the set of values.





So to write this program, you must start with loading your data. If you hard code the data in your program that's fine too. Basically however you have data load it or store it as a variable. After setting a variable to it find the number of data points you will be using for the sort using some kind of length/size command. You now write a nested for loop to rearrange the values so that they are in ascending order or descending order. For my example I will put them in order from least value to greatest value. The loops with be written to tell Matlab to compare each value to the one next to it. If the latter value is less than the former, then the values switch. The first loop's index should be from 1 to the number of values you are given. The second for loop's index will go from one to one less than the number the first index is in. This is prevent the loops from going over already compared numbers that were already sorted. The final nested function in this loop is an if statement. This is where it compares the second index term of the data set to the term right after the latter. It sees if the second term is less than the first. If that's a true statement, then it switches the values. It does this through assigning the first value to a random variable name. (Which I give as SwitchX) Then the first value because the second value and the second becomes the extra, or first, value. Let's show this with code:

clc
X = [9 10 5 3 6 7 1 4 2 8]
Length = length(X);< X(B);
for A = 1 : Length
for B = 1 : Length - A
if X(B+1) < X(B)
SwitchX = X(B);
X(B) = X(B+1);
X(B+1) = SwitchX;
end
end
end
X



That's all I got now. Stay tune for the possible LAST entry next week! Don't miss the stunning conclusion of Matlab Tutorial!

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

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

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