by: m. irfan jaffer · disp • disp display text or array • syntax – disp(x) • description...

40
By: M. Irfan Jaffer

Upload: others

Post on 27-Jul-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

By: M. Irfan Jaffer

Page 2: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a
Page 3: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a
Page 5: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

Variables

• There is no need to declare variable with

type

• a = 0;

• b = 5;

Page 6: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a
Page 7: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a
Page 8: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

Rules for Variable Names

• Making Sure Variable Names Are Valid

• Don't Use Function Names for Variables

• Checking for Reserved Keywords

• Avoid Using i and j for Variables

• Avoid Overwriting Variables in Scripts

Page 9: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

How to display

• In C++, we use cout<<“my first prog”;

• Here we use disp(‘my first program’);

• In C++, for string double quotes are used

• Here single quote is used

Page 10: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a
Page 11: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

disp

• disp Display text or array

• Syntax – disp(X)

• Description disp(X) displays an array, without printing the array name. If X contains a text string, the string is displayed. Another way to display an array on the screen is to type its name, but this prints a leading "X =," which is not always desirable. Note that disp does not display empty arrays. Examples One use of disp in an M-file is to display a matrix with column labels:

• disp(' Corn Oats Hay')

• disp(rand(5,3))

• which results in Corn Oats Hay

• 0.2113 0.8474 0.2749

• 0.0820 0.4524 0.8807

• 0.7599 0.8075 0.6538

• 0.0087 0.4832 0.4899

• 0.8096 0.6135 0.7741

Page 12: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

• str2num

• str2double

• Int2str, num2str

• a = 4;

• a^ 2; power of a

Page 13: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

Input

• In c++ cin>> is used

• Here input function is used

• X = input(‘Enter Numer’);

Page 14: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

Operators

• && AND

• || OR

• >

• >=

• <

• <=

• ~= NOT Equal

Page 15: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

• a = zeros(5)

• b = ones(3)

• c = [1 2 3] rows vector

• d = [1;2;3] column vector

• e = [1 2 3; 4 5 6; 5 6 7]

Page 16: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

If - Else

• if ((atten >= 0.90) & (marks >= 60))

• pass = 1;

• end;

Page 17: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

If else if

• if A if A

• x = a x = a

• else elseif B

• if B x = b

• x = b elseif C

• else x = c

• if C else

• x = c x = d

• else end

• x = d

• end

• end

• end

Page 18: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

For Loop

• for n = 1:5

• x(n) = 2 * x(n - 1);

• end

Page 19: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

while

• while (b ~= 0) & (a/b > 18.5)

disp(‘hello’);

• end

Page 20: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

switch

• method = 'Bilinear';

• switch lower(method)

• case {'linear','bilinear'}

• disp('Method is linear')

• case 'cubic'

• disp('Method is cubic')

• case 'nearest'

• disp('Method is nearest')

• otherwise

• disp('Unknown method.')

• end

Page 21: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

Some Basics

• % - for comments same as // in C++, C#

• clc – clear command window

Page 22: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

Suppressing Results with Semicolons

• Semicolons typed after commands can be

used to hide the printing out of results

• For example:

>>a = 10;

>>b = 20;

>>the_average = (a + b) / 2;

>>the_average

the_average =

15

Page 23: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

Variable Names

• You cannot have any spaces in your

variable names

• my variable -this is not allowed

• my_variable -allowed

Remember!

• MATLAB is case sensitive

Page 24: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

Assigning Strings to Variables

• use single quotes

>>some_text = ‘This is FAST!';

>>some_text

>>some_text =

This is FAST!

Page 25: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

Matrices-The Colon Operator

• : is one of the most important MATLAB

operators. It occurs in several different

forms.

• The expression 1:10 is a row vector

containing the integers from 1 to 10,

1 2 3 4 5 6 7 8 9 10

Page 26: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

• To obtain non unit spacing, specify an

increment/ decrement

• For example, 100:-7:50 is

100 93 86 79 72 65 58 51

• 0:pi/4:pi

is 0 0.7854 1.5708 2.3562 3.1416

Page 27: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

Just a reminder

• my_vector1 = [1, 5, 7]

• my_vector1 =

• 1 5 7

• my_vector2 = [1; 5; 7] my_vector2 =

• 1 5 7

Page 28: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

Seeing a matrix:

• A(2,3) returns element

• A(2,:) returns row

• A(:,2) returns column

Page 29: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

M-Files

M-files are text files containing MATLAB

code

.m extension

Text editor

Page 30: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

Text Editor

• To start the MATLAB text editor simply

type “edit” at the command prompt

OR

• Select File->New->M-file from the

MATLAB desktop menu bar

Page 31: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

% This is my practice script:

a = 3;

b = 4;

c = sqrt(a*a + b*b);

c

Page 32: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

Saving M- File

• Save the file with .m extension

• Example: mytext.m

• Type the file name without the extension

on the command prompt

>>mytext

• You will get the answer:

>>c = 5

Page 33: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

OR

Debug->Run

OR

Press F5

Page 34: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

Work Directory

• Either save your files in the work directory

OR

• Add your folder to the work directory.

Page 35: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

Functions

• function_name(input_parameter_list)

• The first word must always be ``function''.

• The function_name is a character string that

will be used to call the function.

Page 36: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

• The function_name must also be the same

as the file name (without the ``.m'') in which

the function is stored.

• In other words the MATLAB function,

``matrixadd'', must be stored in the file,

``matrixadd.m''.

Page 37: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

Input - Prompt for user input

• number = INPUT('How many students:')

• If the user presses the return key without

entering anything, INPUT returns an

empty matrix.

• disp('Value of first quadratic root: ')

Page 38: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

Display

• disp(X) - displays the array X

• disp(‘hello’) – displays hello

• disp does not display empty arrays.

Page 39: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a

Plotting a Graph

• x = 0:10;

• y = sin(x);

• plot (x, y)

Page 40: By: M. Irfan Jaffer · disp • disp Display text or array • Syntax – disp(X) • Description disp(X) displays an array, without printing the array name. If X contains a