Thursday, October 25, 2007

Matlab VI

Basics of For Loops



Everything I have showed you on Matlab Tutorial up to this point has been pretty simple and straight forward. But now I'm going to show a simple command that is the root to all of Matlab programming. It can be a simple command, but as you get better and better at using the system, you find that this command can be very large and complicating as well. This command is the "for loop". The command is used to perform equations or other actions as many times as specified by the index of the code. The syntax really isn't that bad, it's the the fact that you can have a lot of extra stuff inside the command to make things complex.



The first line of code starts with for X = Index bounds. The bounds can either be used with the colon command or linspace.



A quick lesson on both the colon command and linspace command, because I had failed to explain this before loops. Both of these commands are a way to count and display a series of number to you see fit. The colon command is simply [Start : Increment : End], where the first term is your starting value, the middle term is how much you go up or down (Negative increments count backwards), and your last value is the value the sequence stops at. Linspace is a little different, but is written in code as linspace (Start, End, #), where the first term is the start value, the second is the last value, and the last term is how many terms you want to consist in the sequence.



Now that you know how to count sequences, the for loop should be easier. So the first line can now be completed as for X = 1 : 1 : 10 for example, having the loop go from 1 to 10 in increments of 1. Now what you do with this index and your for loop has really endless possibilities. You could make another for loop inside your first, some logical statement, a summation, or another type of loop. For a simple example, you could have the for loop find values for an equation such as X^2 + 5 for each value of the index and display those in terms of a variable F. This would be written in as the next line, indented from the first: F = X^2 + 5 , where F would be displayed as the value of the equation with X changing each time. To end your loops, always have end indented as far as the for syntax. This example for loop would then run and output each of ten values in the equation F.



So that is the basics to making a safe for loop. Stay tuned to learn how to make other loops! Have a good week and weekend everyone!



Sorry for the title misspelling, it is called a for loop not a four loop.

Resources: Matlab Code

3 comments:

Anonymous said...

Predilection casinos? digging this still in diapers [url=http://www.realcazinoz.com]casino[/url] advisor and wing it crop online casino games like slots, blackjack, roulette, baccarat and more at www.realcazinoz.com .
you can also from our up to date [url=http://freecasinogames2010.webs.com]casino[/url] orientate at http://freecasinogames2010.webs.com and tone down corporeal bucks !
another late-model [url=http://www.ttittancasino.com]casino spiele[/url] purlieus is www.ttittancasino.com , because german gamblers, sprig in magnanimous online casino bonus.

Anonymous said...

Making money on the internet is easy in the underground world of [URL=http://www.www.blackhatmoneymaker.com]blackhat seo world[/URL], You are far from alone if you haven’t heard of it before. Blackhat marketing uses little-known or little-understood avenues to build an income online.

Anonymous said...

What's Up people, fantastic message board I find It exceedingly helpful & it has helped me out tons
I hope to be able to give something back and aid other users like this message board has helped me

_________________
[url=http://iphoneusers.com]iphone forum[/url]