Simple Matlab Example

Contents

(a) Solving a linear system

We can use the backslash command to solve a linear system Ax=b:

format compact              % tells Matlab not to print blank lines between answers
A = [1,2,3; 2,1,0; 3,0,2]   % matrix A
b = [2;-1;2]                % rhs vector b
x = A\b                     % find solution vector x
A =
     1     2     3
     2     1     0
     3     0     2
b =
     2
    -1
     2
x =
   -0.1333
   -0.7333
    1.2000

(b) Plotting the sine and cosine function

Note that x = 0:.1:10 generates a vector with entries 0,.1,...,9.9,10.

sin(x) yields a vector with entries sin(0),sin(.1),...,sin(9.9),sin(10).

x = 0:.1:10;                % use semicolon for stuff you don't want to print!
plot(x,sin(x),x,cos(x))
legend('sin(x)','cos(x)')