function Basic_intro_to_matlab %The function name should be the same as the .m %end each line of code with a semicolon to supress its output. %using 3 dots ... at end of a line will tell matlab to continue reading the %next line below. %typing 'clc' into the commad window clears the workspace. %=========================== Structures ===========================% %Two basic structures of matlab are vectors and matrices, both are examples %of arrays. row = [1 2 3 4 5]; %generate a row vector. column = [1; 2; 3; 4; 5]; %generate a column vector. squ_matrix = [1 2 3 4 5; ... %generate a 5x5 matrix. 6 7 8 9 10; ... 11 12 13 14 15; ... 16 17 18 19 20; ... 21 22 23 24 25]; rect_matrix = [1 2 3 4 5; ... 6 7 8 9 10]; %you can access elements of a vector or matrix directly: row(1); %first element of row. column(3); %third element of column. squ_matrix(3,3); %element which is 3rd column and 3rd row. rect_matrix(1,1); %first element of the matrix. %you can access structures within these variables: squ_matrix(1,:); %outputs the entire first row. squ_matrix(:,1); %outputs the entire first column. %=========================== Basic arthmetic ===========================% a = 1; b = 2; add = a + b; sub = a - b; multi = a*b; div = a/b; %similar rules apply for vectors and matrices vector_add = row + row; vector_sub = column - column; vector_multi = row*column; vector_div = row/row; matrix_multi = squ_matrix*squ_matrix; %using the element wise operations, using the '.' vector_div = row./row; %i.e. element 1 of one vector is divded by the element 1 of the second vector. vector_div = row.^row; %element 1 of vector 1 raised to the power of element 1 of the second vector. %=======================================================================% %=========================== Printing outputs ===========================% %disp(a); %display 'a'. %disp([a b]); %display both a and b by placing them into a row vector. %fprintf('Addition = %f, Subtraction = %f, Multiplication = %f, Division = %f \n', add ,sub ,multi ,div); %fprintf is sometimes more useful than using just disp. The %f means, place %a floating point value of a variable here. They are then defined after the %string, %n can be used to place an integer n.b rounding! You can also %specify the precision the numbers are printed to, see internet. %========================================================================% %=========================== Loops ===========================% %main types of loops. 'for' and 'while'. for i=1:10 %default is to increment i by 1. x(i) = i*i; end for i=1:2:10 %each loop increments i by 2. y = i*i; %disp(y); end while i<100 %this considers up to and including i=99. z(i) = i; i = i+1; %need to increment i by 1 otherwise loop will not end. end %=============================================================% %=========================== if and else ===========================% j = 10; for i=1:20 if i==j %if i is equal to j, (== is boolean) then... x(j) = 100; else %if i is not equal to j, then do this. x(i) = i; end end %===================================================================% %=========================== plotting ===========================% %lots of different ways to plot, see mathworks website. x = 1:10; y = x.^2; %use the . to ensure each ELEMENT is squared, not squared vector. % figure % plot(x,y); % % figure % scatter(x,y); % % [X,Y,Z] = peaks(25); % % figure % surf(X,Y,Z); end