Upload
others
View
10
Download
0
Embed Size (px)
Citation preview
By: M. Irfan Jaffer
Variables
• There is no need to declare variable with
type
• a = 0;
• b = 5;
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
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
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
• str2num
• str2double
• Int2str, num2str
• a = 4;
• a^ 2; power of a
Input
• In c++ cin>> is used
• Here input function is used
• X = input(‘Enter Numer’);
Operators
• && AND
• || OR
• >
• >=
• <
• <=
• ~= NOT Equal
• 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]
If - Else
• if ((atten >= 0.90) & (marks >= 60))
• pass = 1;
• end;
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
For Loop
• for n = 1:5
• x(n) = 2 * x(n - 1);
• end
while
• while (b ~= 0) & (a/b > 18.5)
disp(‘hello’);
• end
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
Some Basics
• % - for comments same as // in C++, C#
• clc – clear command window
•
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
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
Assigning Strings to Variables
• use single quotes
>>some_text = ‘This is FAST!';
>>some_text
>>some_text =
This is FAST!
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
• 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
Just a reminder
• my_vector1 = [1, 5, 7]
• my_vector1 =
• 1 5 7
• my_vector2 = [1; 5; 7] my_vector2 =
• 1 5 7
Seeing a matrix:
• A(2,3) returns element
• A(2,:) returns row
• A(:,2) returns column
M-Files
M-files are text files containing MATLAB
code
.m extension
Text editor
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
% This is my practice script:
a = 3;
b = 4;
c = sqrt(a*a + b*b);
c
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
OR
Debug->Run
OR
Press F5
Work Directory
• Either save your files in the work directory
OR
• Add your folder to the work directory.
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.
• 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''.
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: ')
Display
• disp(X) - displays the array X
• disp(‘hello’) – displays hello
• disp does not display empty arrays.
Plotting a Graph
• x = 0:10;
• y = sin(x);
• plot (x, y)