12.07.2015 Views

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1.5 The Colon OperatorTo generate a vector <strong>of</strong> equally-spaced elements matlab provides thecolon operator. Try the following comm<strong>and</strong>s:1:50:2:100:.1:2*piThe syntax x:y means roughly “generate the ordered set <strong>of</strong> numbersfrom x to y with increment 1 between them.” The syntax x:d:y meansroughly “generate the ordered set <strong>of</strong> numbers from x to y with incrementd between them.”1.6 LinspaceTo generate a vector <strong>of</strong> evenly spaced points between two end points,you can use the function linspace(start,stop,npoints ):>> x = linspace(0,1,10)x =Columns 1 through 70 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667Columns 8 through 100.7778 0.8889 1.0000generates 10 evenly spaced points from 0 to 1. Typing linspace(start,stop ) will generate a vector <strong>of</strong> 100 points.1.7 Plotting VectorsWhereas other computer languages, such as Fortran, work on numbersone at a time, an advantage <strong>of</strong> matlab is that it h<strong>and</strong>les the matrix asa single unit. Let us consider an example that shows why this is useful.Imagine you want to plot the function y = sin x for x between 0 <strong>and</strong> 2π.A Fortran code to do this might look like this:DIMENSION X(100),Y(100)PI = 4*ATAN(1)DO 100 I = 1,100X(I) = 2*PI*I/100Y(I) = SIN(X(I))100 CONTINUEPLOT(X,Y)Here we assume that we have access to a Fortran plotting packagein which PLOT(X,Y) makes sense. In matlab we can get our plot bytyping:c○ 2000 by CRC Press LLC

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!