clc echo on % Example 1 % This is a script % Executing a script is the same as typing the % commands in the command window. % % The percent indicates the beginning of a comment % % MATLAB can do routine calculations 2+2 sqrt(2) pause clc % MATLAB can use variables % The equals sign indicates assignment s = ans t = s*s u = exp(t) pause clc % MATLAB can do complex arithmetic s = sqrt(-2) t = s*s % and it knows Euler's formula u = exp(s) cos(imag(s))+i*sin(imag(s)) v = log(u) pause clc % MATLAB is designed to work with vectors, % matrices, and arrays. x = [1; 2; 3] A = [1 1 1; 2 3 4; 0 1 -1] y = A*x pause clc % Unless otherwise indicated, arrays are % treated as matrices. A A^2 % Use a period in front of the operator % to shift to an array interpretation A.^2 pause clc % MATLAB has loops and case statements k = 1; while k<3 if mod(k,2)==0 display('k is even') else display('k is odd') end k = k+1 end pause echo on clc % Plotting is so easy in MATLAB it gets a STAR theta = 4*pi/5 echo off for k=0:5 z(k+1) = i*exp(i*k*theta); end plot(z) echo off