COMSATS University Islamabad: Sahiwal Campus
COMSATS University Islamabad: Sahiwal Campus
COMSATS University Islamabad: Sahiwal Campus
Sahiwal Campus
MTH375-Numerical Computation
Lab Experiment:1
Introduction to MATLAB and Revision of Basic Programming Concepts
Objectives:
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
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.
Arithmetic Operators
^ 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𝜋
Elementary Functions
>> cos(0)
>> 6*sin(pi/2)
>> exp(1)
>> log(exp(3))
>> sqrt(9)
Note:
▪ The log is the natural log (often labelled ln on calculators). The log base 10 is log10.
>> s=5.6
▪ 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
>> r=5
>> A=pi*r^2
>> A=pi*r^2
Exercise Task
Garden P
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
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
3. Create m-file and named it as sine and save it as sine.m. The contents of the file should be format
long
y = sin(x)./x
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.
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
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
true is represented by 1
Examples
Booleans may be combined using logical operators. Use brackets to make sure of the order of
evaluation.
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,
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
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:
a = log(abs(x))./log(3);
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.