P. 1
Introduction to Matlab

Introduction to Matlab

|Views: 261|Likes:
Published by thesuperleo

More info:

Published by: thesuperleo on Sep 28, 2009
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





When a script doesn’t work properly, you need to find out why and then fix it. It is very
helpful in this debugging process to watch what the script does as it runs, and to help you
do this, Matlab comes with two important features: breakpoints and stepping.
To see what a breakpoint does, put the cursor on the x=0:h:20 line in the sample script
above and either click on Breakpoints on the tool bar and select Set/Clear, or press F12.
Now press F12 repeatedly and note that the little red dot at the beginning of the line toggles
on and off, meaning that F12 is just an on-off switch set set a breakpoint. When the red
dot is there it means that a breakpoint has been set, which means that when the script runs
it will execute the instructions in the script until it reaches the breakpoint, and then it will
stop. Make this happen by pressing F5 and watching the green arrow appear on the line
with the breakpoint. Look at the workspace window and note that h has been given a value,
but that x has not. This is because the breakpoint stops execution just before the line on
which it is set.

Now you can click on the Debug icon on the tool bar to see what to do next, but the most
common things to do are to either step through the code executing each line in turn (F10)
while watching what happens to your variables in the workspace and array editor windows,


or to just continue after the breakpoint to the end (F5.) Take a minute now and use F10 to
step through the script while watching what happens in the other windows.
When you write a script to solve some new problem, you should always step through it
this way so that you are sure that it is doing what you designed it to do. You will have lots
of chances to practice debugging this way as you work through the examples in this book.

2 Variables

Maple has over 100 different data types; Matlab has just two: the matrix and the string. In
both languages variables are not declared, but are defined on the fly as it executes. Note
that names in Matlab are case sensitive, so watch your capitalization. Also: please don’t
follow the ridiculous trend of making your code more readable by using long variable names
with mixed lower- and upper-case letters and underscores sprinkled throughout. Newton’s
law of gravitation written in this style would be coded this way:

Force_of_1_on_2 = G*Mass_of_1*Mass_of_2/Distance_between_1_and_2^2

You are asking for an early end to your programming career via repetitive-stress injury if
you code like this. Do it this way:


You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->