Learning some basic Matlab tasks

First, there are several Demos and an extensive help system on matlab. Be sure to check them out:

Here are some exercises to run through to see some very simple operations in matlab.
You should type them into the >> prompt.

Just type the part after the >>

Basic data entry

(Basic arithmetic)
>> 1 + 1
>> 1+3*4^2

(An array, or a matrix with only one row. )
>> a = [ 1 2 3 ]

(A matrix with one row.)
>> b = [ 4; 5; 6; ]

(A multi-dimensional array or a matrix)
>> c = [ 1 2 3; -1 2 2; 0 8 -9; ]

(matrix math)
>> a + 2
>> a*b
>> b*a
>> det(c)
>> inv(c)
>> eye(3,3)

Application: Solving simultaneous linear equations:


Say we want to solve:
		 x + 2y + 3z = 9
		2x -  y +  z = 8
		3x      -  z = 3

>> a = [1 2 3; 2 -1 1; 3 0 -1;]
>> b = [9; 8; 3;]
>> a\b

Symbolic Toolkit

>> syms x
>> f = sin(5 * x)
>> diff(f)


>> syms h n x
>> limit ((cos(x+h) - cos(x))/h,h,0)
>> limit((1 + x/n)^n,n,inf)

Graphics Plotting

(peaks generates a sample 49x49 matrix)
>> Z = peaks
>> plot(Z)

Starting a .m file

File | New | .m file
Here are the file contents:
         CLA; hold on; axis ([0 1 0 1]); Grid on;
         Fig=[0;0;]; i=1; z=1;
         while z==1, 
             [x,y,z]=ginput(1);
             FIG(:,i)=[x,y]';
             plot(FIG(1,:),FIG(2,:));
             i=i+1;
         end
Save the file as stick.m

In command window type: stick

Draw one of your initials, the lines have to be contiguous so you must backtrace or make block letters.

Close figure window when done.

>> E=FIG
>> cla;
>> plot(E(1,:),E(2,:))

(Shear matrix)
>> SM=[1 0; 1 1;]
>> SE=SM*E
>> plot(SE(1,:),SE(2,:))

(Rotation matrix of pi/6 degrees)
>> RM = [ cos(pi/6) -sin(pi/6) ; sin(pi/6) cos(pi/6)]
>> RE = RM*E

Exercise

Try making a .m file that calculates the sum of i^3 from i between 1 and 10.
The syntax of doing loops is in the help system indexed under loop.

You will probably want to do the following steps:

  1. Initialize a total count variable to zero
  2. Start a loop with a counter that goes from 1 to 10
  3. Inside the Loop, set the total variable to the total variable plus the counter^3