Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction To MATLAB

Similar presentations


Presentation on theme: "Introduction To MATLAB"— Presentation transcript:

1 Introduction To MATLAB
Lab sheet 2 – 2017/1439 By: Elham Sunbu

2 OUTLINE Matlab introduction Why Matlab
Matlab Software install and interface Matlab Variables Matlab Matrices

3 Matlab introduction MatLab : Matrix Laboratory
Numerical Computations with matrices Every number can be represented as matrix Why Matlab? User Friendly (GUI) Easy to work with Powerful tools for complex mathematics It was originally designed for solving linear algebra type problems using matrices

4 Why Matlab? Typical uses include: • Math and computation
• Algorithm development • Modeling, simulation, and prototyping • Data analysis, exploration, and visualization • Scientific and engineering graphics PC UNIX Subject 1 143 Subject 2 982 Subject 3 87 …

5 Matlab Software install
Download from this link: Or visit this link:

6 The MATLAB interface Current Directory type commands
View folders and m-files View program variables type commands view past commands save a whole session using diary

7 Variables No need for types. i.e., int a; double b; float c;
All variables are created with double precision unless specified and they are matrices. After these statements, the variables are 1x1 matrices with double precision. int a; double b; float c; Example: >>x=5; >>x1=2;

8 Creating Variables Names Value
Can be any string of upper and lower case letters along with numbers and underscores but it must begin with a letter Reserved names are IF, WHILE, ELSE, END, SUM, etc. Names are case sensitive Value This is the data the is associated to the variable; the data is accessed by using the name.

9 Example of Value: Singletons
To assign a value to a variable use the equal symbol ‘=‘ >> A = 32 To find out the value of a variable simply type the name The value of two variables can be added together, and the result displayed… >> A = 10 >> A + A or the result can be stored in another variable >> B = A + A

10 Matrices A MATLAB matrix is a rectangular array of numbers
Scalars and vectors are special cases of matrices MATLAB allows you to work with a whole array at a time Vectors: Use square brackets [ ] to contain the numbers

11 A matrix can be created in MATLAB as follows (note the commas AND semicolons): matrix = [1 , 2 , 3 ; 4 , 5 ,6 ; 7 , 8 , 9] matrix =

12 a vector x = [1 2 5 1] transpose y = x’ y =
transpose y = x’ y = 1 2 5 a matrix x = [1 2 3; 5 1 4; ]

13 Long Array, Matrix B = [1:4; 5:8] k =2:-0.5:-1 t =1:10 t = k = B =
k =2:-0.5:-1 k = B = [1:4; 5:8] B =

14 Some Matrix Functions in Matlab
Explain X = ones(r,c) % Creates matrix full with ones X = zeros(r,c) % Creates matrix full with zeros. A = diag(x) % Creates squared matrix with vector x in diagonal [r,c] = size(A) % Return dimensions of matrix A + - * / % Standard operations * ./ % Wise addition, substraction ,… v = sum(A) % Vector with sum of columns

15 Example of Function x = zeros(1,3) x = 0 0 0 x = ones(1,3) 1 1 1
x = ones(1,3) x = rand(1,3)

16 Example 2

17 Example 3

18 Example 4 >>x=(0:0.1:1)' >>y=sin(x) >>[x y]

19 Matrices Operations

20 Inner matrix dimensions must agree
Matrices Product A=3*2 , B=2*3 Inner matrix dimensions must agree n order to multiply two matrices, A and B, the number of columns in A must equal the number of rows in B. Thus, if A is an m x n matrix and B is an r x s matrix, n = r

21 A=2*3 Matrix must be square
Matrices Power A=2*3 Matrix must be square

22 Operators (Element by Element)
.* element-by-element multiplication ./ element-by-element division .^ element-by-element power

23 The use of “.” – “Element” Operation

24 Functions MATLAB has many built-in functions. Some math functions are:

25 Matlab Special Variables
ans: default variable name pi: ratio of circle circumference to its diameter, π = eps: smallest amount by which two numbers can differ inf or Inf: infinity nan or NaN: not-a-number, e.g. 0/0 date: current date in a character string format, such as 19-Mar-1998

26 Commands Involving Variables
who: lists the names of defined variables whos: lists the names and sizes of defined variables clear: clears all variables, resets default values of special variables clear var: clears variable var clc: clears the command window, homes the cursor (moves the prompt to the top line), but does not affect variables clf: clears the current figure and thus clears the graph window. more on: enables paging of the output in the command window more off: disables paging of the output in the command window.

27 Note The '%' sign indicates that there is a comment in that line and Matlab does not do anything with it. It is as if it were inexistent, and it exists only for explanatory purposes. “%” is the neglect sign for Matlab (equaivalent of “//” in C)

28 Useful Commands >>help functionname >>lookfor keyword
The two commands used most by Matlab users are >>help functionname >>lookfor keyword

29 Useful Commands

30 PLOTTING in MATLAB

31 EX:1 Plot the Function sin(x) between 0≤x≤4π
- Create an x-array of 100 samples between 0 and 4π. - Calculate sin(.) of the x-array - Plot the y-array >>x=linspace(0,4*pi,100); >>y=sin(x); >>plot(x,y)

32 EX2: Adding additional plots to a figure
HOLD ON holds the current plot HOLD OFF releases hold on current plot HOLD toggles the hold state » x = 0:.1:2*pi; y = sin(x); » plot(x,y,'b') » grid on » hold on » plot(x,exp(-x),'r:*') By default, PLOT deletes existing lines and resets all axis properties when a new line is drawn (HOLD OFF). To add lines to an existing plot turn HOLD ON. In this mode, MATLAB does not remove the existing graph but adds new lines to the current plot. If the new data falls outside the range of the original axes, MATLAB will rescale the axes accordingly. When you set HOLD OFF, the default mode is restored - PLOT command erases any existing plots and resets all axis properties before creating new plots. When you call HOLD, by itself, the hold state is toggled NOTE: If you do not want MATLAB to rescale the axes, issue the command >>axis manual before you plot your second set of data.

33 Plotting in MatLab Line plot FUNCTIONS: 1. Plot example:

34 Plotting in MatLab 2. Plot wit line specification example:
Linearly spread vector by default generate 100 points between x1 , x2

35 Plotting in MatLab - Plotyy ex:

36 Plotting in MatLab - Plot3 3D ex:

37 EX:3 Plotting in MatLab

38 Discrete Graph – Stem Function
stem(Y) plots the data sequence Y as stems from the x axis terminated with circles for the data value

39 Stair Function Stairs(Y) draws a stairstep graph of the elements of vector Y.S

40 EX:4 Plot sin(x) and cos(x) over [0,2π], on the same plot with different colures Method 1: >> x = linspace(0,2*pi,1000); >> y = sin(x); >> z = cos(x); >> hold on; >> plot(x,y,‘b’); >> plot(x,z,‘g’); >> xlabel (‘X values’); >> ylabel (‘Y values’); >> title (‘Sample Plot’); >> legend (‘Y data’,‘Z data’); >> hold off;

41 EX:5 con … y2=cos(t); plot(t,y2); plot(t,y,t,y2) legend('sin','cos') plot(t,y) hold on plot(t,y2,'m') hold off subplot(2,1,2) subplot(2,1,1) plot(t,y2)

42 Thank You


Download ppt "Introduction To MATLAB"

Similar presentations


Ads by Google