0% found this document useful (0 votes)
44 views7 pages

Industrial and Manufacturing Engineering Department: Programming

This document provides an overview of basic MATLAB programming concepts such as variables, expressions, matrices, logical operators, and formatting text output. It explains how to get help in MATLAB, perform simple calculations and assignments, reference matrix elements, and use logical constructs and conditional statements. Examples are provided to demonstrate MATLAB commands for arithmetic operations, creating and manipulating matrices, and formatting output with functions like fprintf.

Uploaded by

Abdul Rashid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views7 pages

Industrial and Manufacturing Engineering Department: Programming

This document provides an overview of basic MATLAB programming concepts such as variables, expressions, matrices, logical operators, and formatting text output. It explains how to get help in MATLAB, perform simple calculations and assignments, reference matrix elements, and use logical constructs and conditional statements. Examples are provided to demonstrate MATLAB commands for arithmetic operations, creating and manipulating matrices, and formatting output with functions like fprintf.

Uploaded by

Abdul Rashid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Industrial and Manufacturing 1

Engineering Department

IM-101 Computer Applications in Engineering

Course instructor:
Mr. Shakeel Ahmed
(Assistant Professor)

Programming 2

• The most awful truth about computers is that they are all
dumb. Everything must be explicitly told. Spelling out the
most minor and obvious detail.
• Otherwise, you will either get a syntax error (computer not
understanding what you are saying) or a programming error
(the computer understanding what you are saying but it is
different from what you mean).
• There are many programing languages. Why use Matlab? As a
first programming language, it is great, it has simple syntax,
it is easy to get started with and easy to do graphics. While
other programming languages are faster running they are
slower to learn and to use.

1
The Command Prompt: 3

• In the interactive terminal of Matlab you will see the


command prompt (>>). This is Matlab's way of telling you
that it is ready to receive instructions from you. To
command Matlab, type your request and hit Enter. Matlab
then evaluates your command and displays the output (if
any).

Getting Help 4

• help: should be used when you know a command name. e.g. help zeros
• Look for: can be used to find a command to perform a task e.g.
lookfor roots
• doc: same as help but opens help in a new window with more details
e.g. doc plot
• Exercise:
• Learn about the commands ones, zeros, sum, and diag.
• Learn about magic and verify what you learned with sum.

2
Simple Expressions 5

• Expressions: 1, 1+1,4*3, 4^2, 5/6.


• Matrices and Vectors: [1 2], [1:10],
A =[1 2 3 4; 5 6 7 8 ; 9 10 11 12];
• Transpose: A’ (see the difference between A’;)
• Exercise:
• Create the vector consisting of the all even numbers between 15 and 27.
• Create the vector consisting of the all odd numbers between 15 and 27.
• Create the vector of the previous question in decreasing order.

Using Variables 6

• isvarname: checks if a variable name is valid


>> s = '8th_column'; >> s = 'column_8th';
>>isvarname(s) >>isvarname(s)
ans = ans =
logical logical
0 1

Iskeyword: Determine whether input is MATLAB keyword


>>iskeyword(‘while’) Note: to get a list of all
MatLab keyword:
ans =
>>iskeyword
logical
1

3
Referencing matrix elements 7

>> A = [1 2 3; 4 5 6; 7 8 9] >> A([2 3], [1 2])


A=
ans =
1 2 3
4 5
4 5 6
7 8
7 8 9
Note: A(4) = A(1,2) = 2
The second entire row
What is A(8) = ???? >> A(2, 1:end) or A(2, : )
Submatrices: The third entire column
>> A(2, [1 2]) >> A(1:end, 3) or A( : , 3 )
ans = Note ‘: = everything’
4 5

Assigning into Submatrices 8

>> A
A=
1 2 3
4 5 6
7 8 9
A(2,2:3) = 10:11
A=
1 2 3
4 10 11
7 8 9

4
Exercise 9

1. Write an expression for the sum of the integers from 1 to 100

2. Write an expression for the sum of the squares of the numbers from 1 to 10

3. Write an expression for the sum of the powers of 0.5 to the numbers from 1 to
10

Logical Constructs 10

>> 1==2
>> 3~=5
>> 5<=6
>> 7<=10
Boolean operations
1~=2 | 3<4
3~=4 & 8==(4+4)
~(2<=8)

5
Formatting Text 11

• fprintf('hello, here is a number %d',17)


ans =
hello, here is a number 17
• fprintf('my name is %s and I am %g years old',‘Alex',24)
ans =
my name is Alex and I am 24 years old

Examples 12
A1 = [9.9, 9900];
A2 = [8.8, 7.7 ; 8800, 7700];
formatSpec = 'X is %4.2f meters or %8.3f mm\n';
fprintf(formatSpec,A1,A2)
ans =
X is 9.90 meters or 9900.000 mm
X is 8.80 meters or 8800.000 mm
X is 7.70 meters or 7700.000 mm

%4.2f in the formatSpec input specifies that the first value in each
line of output is a floating-point number with a field width of four
digits, including two digits after the decimal point.
\n is a control character that starts a new line.

6
Example 13
Practice with the following code:

A = magic(4);

fprintf('%5d %5d %5d %5d\n', A)

fprintf('%05d %05d %05d %05d\n', A)

fprintf('%-5d %-5d %-5d %-5d\n', A)

fprintf('%+5d %+5d %+5d %+5d\n', A)

For complete list of text symbols:


doc text properties

You might also like