Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

COMSATS University Islamabad: Sahiwal Campus

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

MTH375-Numerical Computation Fall 2019

COMSATS University Islamabad

Sahiwal Campus

MTH375-Numerical Computation

Experiment 01: Introduction to MATLAB and Revision of Basic


Programming Concepts

DEPARTMENT OF ELECTRICAL & COMPUTER ENGINEERING

Prepared By: Checked By: Approved By:

Engr. M. Hamid Saeed Mr Omar OBE committee

----------------------------------- ----------------------------------- -----------------------------------

COMSATS University Islamabad, Sahiwal Campus


MTH375-Numerical Computation Fall 2019

Lab Experiment:1
Introduction to MATLAB and Revision of Basic Programming Concepts
Objectives:

1. To be familiar with software tool MATLAB (basic features and commands)


▪ Using MATLAB as a calculator.
▪ Constants
▪ Arithmetic operators and expressions
▪ Elementary functions, help, format
▪ Variables, reserved words,
▪ Introduction to MATLAB files
▪ Input
2. Revision of Programming fundamentals.
Pre-Lab
MATLAB, which stands for MATrix LABoratory, is a state-of-the-art mathematical software
package, which is used extensively in both academia and industry. MATLAB deals mainly
with matrices. A scalar is a 1-by-1 matrix and a row vector of length say 5, is a 1-by-5 matrix.
The purpose of this lab is to familiarize the beginner to MATLAB, by introducing the basic
features and commands of the program and implementing programming techniques in
MATLAB.

In-Lab Exercise
MATLAB is a computer language/application designed for scientific computation. It can be used in
two ways, interactively (like a calculator) or non-interactively (as a programming language).

Starting

Click on the Windows start symbol at the bottom left of the screen, click Programs from the menu that
appears, Type maths, then move to click Matlab. This opens the Command Window where you can
use MATLAB like a calculator.

Prompt

When MATLAB is ready to accept a new instruction, it shows a prompt >>.

Enter

Press the enter key when you have typed in your instruction. It does not matter if the cursor is not at
the end of the line.

COMSATS University Islamabad, Sahiwal Campus


MTH375-Numerical Computation Fall 2019

Arithmetic Operators

The arithmetic operators in decreasing order of precedence are:

Arithmetic operator Operation performed

^ raise to a power

/ Division

* Multiplication

- Subtraction

+ Addition

Arithmetic Expressions

>>2*3
ans=6
>>12/5
ans=2.400
>>2^3
ans=8
>>10-(1+3)
ans=6

Task

Write in MATLAB

1. 32 + 5

2. 32+5

60
3. 2+3

60+3
4.
2

5. −2 × 5 × −7

12−3
6. 5+1

3 4
7. (4)

8. 5𝜋

COMSATS University Islamabad, Sahiwal Campus


MTH375-Numerical Computation Fall 2019

Elementary Functions

Most of the usual mathematical functions are implemented in MATLAB.

>> cos(0)
>> 6*sin(pi/2)
>> exp(1)
>> log(exp(3))
>> sqrt(9)

Note:

▪ MATLAB uses radians, not degrees.

▪ The log is the natural log (often labelled ln on calculators). The log base 10 is log10.

Variables and Memory

A variable is a labelled piece of computer memory.

>> s=5.6

The number 5.6 is stored in the computer memory and labelled.

▪ Matlab is case sensitive. That means that the upper case and lower case of a letter are
different. For example, the variable called s is different from the variable called S.

▪ Reserved words are variable names it is best not to use. This is often because they are names
of built-in functions.

▪ A semicolon after a statement suppresses the output. For example, the statement x=2*3 will
set the value of variable x to 6 and also show the result on the screen. However, x=2*3; still
sets the value of x to 6, but does not show the result of the multiplication on the screen.

Example

Find the area of a circle with radius 5cm.

>> r=5
>> A=pi*r^2

Now let radius be 7cm. >> r=7

>> A=pi*r^2

COMSATS University Islamabad, Sahiwal Campus


MTH375-Numerical Computation Fall 2019

Exercise Task

A piece of wire netting to put a fence around a garden is 10 meters long.


q

Garden P

If p = 2, what is q and what is the area enclosed?

Matlab file Types:

To take advantage of MATLAB’s full capabilities, we need to know how to construct long (and
sometimes complex) sequences of statements. This can be done by writing the commands in a file and
calling it from within MATLAB. Such files are called “m-files” because they must have the filename
extension “.m”. This extension is required for these files to be interpreted by MATLAB.

Types of M-files

There are two types of m-files:

1. Script files.

2. Function files.

Script Files

A program is a sequence of statements that are performed in order. They are stored in a script file. A
file is the basic unit that the computer stores on mass storage (such as hard disk or floppy disk). Files
can store pictures, or programs (such as MATLAB script files) or other data (such as text files). To
run a program either type the name of its file in the Command Window or use the Debug menu in its
window and choose Save and Run. Note that the Save and Run commands changes to Run if the file
has been saved.

Examples

1. Write a script file to set a value for d, a distance in km, and calculate how long it takes to drive that
distance at 80 kph.

d=240

t=d/80

2. Write a script file to set a value for r, the radius of a circle, and calculate the area of the circle and
its circumference.

r=2

a=pi*r^2

c=2*pi*r

COMSATS University Islamabad, Sahiwal Campus


MTH375-Numerical Computation Fall 2019

3. Create m-file and named it as sine and save it as sine.m. The contents of the file should be format
long

x = [0.1, 0.01, 0.001];

y = sin(x)./x

Go to console window and run sine command.

For loop

The “for” loop allows us to repeat certain commands. If you want to repeat some action in a
predetermined way, you can use the “for” loop.

>> for j=1:4

j+2

end

input statement

In the example above we put values directly into the Matlab script file. The input statement is
another way to enter values for MATLAB. It includes a string which is used as a prompt so the
user knows what to enter.

Example

x=input(’Enter a positive number ’);

When this statement is executed, the string

Enter a positive number

will appear in the command window. The user then types in a value, and this value is assigned
to the variable x.

Note: that just this statement will not check that the number entered is positive.

Boolean Expressions

Boolean Expressions are comparisons which are either true or false.

In MATLAB: false is represented by 0

true is represented by 1

(or any other non-zero number)

COMSATS University Islamabad, Sahiwal Campus


MTH375-Numerical Computation Fall 2019

Examples

Boolean result Boolean result


6>3 true (or 1) 3>6 false (or 0)
7==4 false (or 0) 7~=4 true (or 1)

Booleans may be combined using logical operators. Use brackets to make sure of the order of
evaluation.

~ logical NOT changes to opposite truth value


| logical OR at least one relation is true
& logical AND both relations are true

If statement

Use an if statement to execute some commands only when a certain Boolean is true (i.e. when
some condition is met). Use end to show the end of the statements to be executed. For
example,

n=input(’Enter the number now ’)


if n<0
disp(’This number is negative’)
end

We can also include the command else which executes the command if the Boolean is false
(i.e the condition is not met). For example

n=input(’Enter the number now ’)


if n<0
disp(’This number is negative’)
else
disp(s’Square root is’)
disp(sqrt(n))
end

Function files

Function files, on the other hand, play the role of user defined commands that often have input and
output. You can create your own commands for specific problems this way, which will have the same
status as other MATLAB commands:

create m file and named it as log3.m. The contents of file should be:

function [a] = log3(x)

a = log(abs(x))./log(3);

Go to console window and run log3(5)

Syntax of Function files

function [output] = Name (input)

COMSATS University Islamabad, Sahiwal Campus


MTH375-Numerical Computation Fall 2019

Post Lab
Q.1 Write Matlab expressions to calculate
𝜋
 42 + 3cos 2
 √1 − 𝜋
 5𝑒 3

Q.1 Write a MATLAB script file that takes input of two different 4 by 4 matrices A and B
from user.
(i) Find Transpose of B.
(ii) Find the inverse of A.
(iii) Multiply the first three rows of A with the last three columns of B to get a 3 by 3
matrix C.

Q.2 Plot the graph of 𝑦 = 𝐴 sin(𝑏𝑥 + 𝑐), 𝑦 = 𝐵 cos(𝑥), for two different values of A,
B, b and c, and analyze.

Note: Submit your Lab report consisting of all in-lab exercise tasks results and post lab tasks
within two working days. After that your lab report won’t be considered.

COMSATS University Islamabad, Sahiwal Campus

You might also like