Matlab Some Basics Data
Matlab Some Basics Data
http://www.math.utah.edu/~eyre/computing/matlab-intro/program.html
7.1 Some basics 7.2 m-files 7.3 Good practices At the user's level Matlab is an interpreted language that accesses compiled software. This has disadvantages and advantages. The advantage of this is that Matlab code is easy to debug. When I write Matlab code, I have an editor open in one window and I run Matlab in another window. Then I write a few lines of code, and I cut that code out of the edit window and paste it into the Matlab window to test it. This is very useful and fast. The disadvantage is that the interpreter is extremely slow. Therefore, to make Matlab a useful tool for numerical simulation, it is critical that you try to write code that utilizes the compiled (and consequently, fast) subroutines more than it utilizes the interpreter. I talk about this more below.
calculate the dot product of x and y. Conditional statements are evaluated with the if, while and switch commands. The syntax for if is
>> if expression commands elseif expression commands else
1 of 4
6/2/2012 1:32 AM
http://www.math.utah.edu/~eyre/computing/matlab-intro/program.html
commands end
7.2 m-files
Matlab allows the user to write programs, save them on the disk, and then to execute them. These programs are called m-files. By convention, they are named foo.m. To see an example of an m-file type
>> help function
This shows a function for computing the mean and standard deviation of a vector x. The format for a matlab m-file is shown in the stat function. Copy that format for your m-files. Another version of a m-file was shown in the ODE section of this web-page,
>> type lotka
This file specifies the right hand side of an ordinary differential equation that models a population of predators and their prey.
n = 100; A = rand(n); B=rand(n); C=zeros(n); t = cputime; for i=1:n, for j=1:n, for k=1:n C(i,j) = C(i,j) + (A(i,k)*B(k,j)); end end end >> t = cputime-t
2 of 4
6/2/2012 1:32 AM
http://www.math.utah.edu/~eyre/computing/matlab-intro/program.html
t = 97.9600
Notice that this segment of code required 98 seconds to compute the answer on my SGI O2. There are nine permutations of this scheme, and all have similar timings. We now consider using the colon range operator to compute the product of A and B by looping over all of the elements of the resulting matrix C. Notice that what is being computed for a given i and j is the dot product of the i^(th) row of A with the j^(th) column of B.
>> % Matrix multiplication example using inner products >> >> t = cputime; >> for i=1:n, for j=1:n C(i,j) = A(i,:)*B(:,j); end end >> t = cputime-t t = 2.6600
This segment of code took 2.66 seconds to complete the calculation. By eliminating the inner loop, we have decreased the effort required to compute C by a factor of 37 times! Finally, we make use of the * operator, i.e. we use the full compiled code to compute C.
>> t = cputime; t = 0.0400 C = A*B; t = cputime-t
This segment of code took 0.04 seconds, a savings of 2450 times over the fully looped calculation and 66 times over the inner product calculation. You would not want to program matrix multiplication if you are using Matlab, but this example should serve as an illustration of the kind of code that you should avoid writting if possible. On the other hand, be careful that you don't get too carried away with this process. The following code segments assign a vector to the sum the first j elements of another vector, i.e. y_j = x_1 + x_2 + ... + x_j
clear; n=2000; x = 1:n; x = x(:); y = zeros(n,1); % case 1 -- no vectorization t=cputime; for j=1:n, for k=1:j, y(j)=y(j)+x(k); end, end, cputime-t % case 2 -- partial vectorization t=cputime; for j=1:n, y(j)=sum(x(1:j)); end, cputime-t % case 3 -- full vectorization
3 of 4
6/2/2012 1:32 AM
http://www.math.utah.edu/~eyre/computing/matlab-intro/program.html
The partially vectorized case is much faster than the case without any vectorization, but the fully vectorized case takes more time than the partially vectorized case because it computes too many multiplications that are equal to zero. I'll close this section with a couple of suggestions for writing efficient Matlab code. Your code is only as good as your algorithm. Find a good algorithm if you can. Avoid accessing arrays via their individual elements. Use vectors instead. For example, use
>> n = 11; x = rand(n); y = rand(n) >> z = x(1:2:n) + y(1:2:n)
instead of
>> for i=1:2:n z(i) = x(i) + y(i); end
The result is easier to read and faster. Avoid nested looping. Looping is expensive because it accesses array elements. Avoid too many conditional statements. David Eyre 9/8/1998
4 of 4
6/2/2012 1:32 AM