BAHCESEHIR UNIVERSITY
MAT3012 NUMERICAL ANALYSIS
(LAB APPLICATOINS)
Week-1, Spring
Resch. Asst. Aysun Soysal
SYLLABUS
Instructor: Resch. Asst. Aysun Soysal
https://akademik.bahcesehir.edu.tr/web/aysunsoysal/tr/index.html
Email: aysun.soysal@eng.bau.edu.tr
Course materials: http://buei.itslearning.com
Software: MATLAB
Grading: 2 Quizzes + MATLAB Homework Assignment 2
LAB-1 OBJECTIVES
1. MATLAB Environment
2. Some Useful Commands
3. Input-Output Function
4. Array, Vector and Matrix
5. Mathematical Functions and Operations
3
1. MATLAB ENVIRONMENT
MATLAB is a computer program that provides the user with a convenient environment for
performing many types of calculations. In particular, it provides a very nice tool to implement
numerical methods.
MATLAB uses three primary windows:
• Command window which is used to enter commands and a data
• Graphics window which is used to display plots
• Edit window which is used to create and edit M-files
4
2. SOME USEFUL COMMANDS IN MATLAB
• help gives information about specific MATLAB command or function; e.g. ‘help for’.
• doc displays documentation for the MATLAB function in the Help browser; e.g. ‘doc for’.
• pwd exracts the address of your current working directory.
! MATLAB is case sensitive for variable names and built-in functions.
• clc clears window
• clear x clears x from work space
• clear all clears memory
5
• format long scales fixed point format with 15 digits; e.g.
>> pi
ans =
3.1416
>> format long
>> pi
ans =
3.141592653589793
• format short scales fixed-point format with 5 digits
• format long e gives floating point with 15 digits; e.g.
>> format long e
>> pi
ans =
3.141592653589793e+00 6
• format short e gives floating point with 5 digits
3. INPUT - OUTPUT FUNCTION
Two functions provide ways to enter and display information directly using the command
window:
• The input function
• The disp function
7
▪ The input function. This function allows you to prompt the user for values directly from the
command window. Its syntax:
n=input(‘prompt string’)
i.e., this function displays the prompt string, waits for keyboard input, and then returns the value
from the keyboard.
• If the user enters a value, it would then • If the user enters a string, an ‘s’ is
be assigned to the variable m. appended to the function’s argument list.
>> m=input('Enter your age:') >> name=input('Enter your name:','s')
Enter your age: 31 Enter your name: Aysun
m= name =
31 Aysun 8
▪ The disp function. This function provides a handy way to display a value. Its syntax:
disp(value)
where value= the value you would like to display. It can be a numeric costant or variable, or a
string enclosed hyphens.
>> disp('Aysun’) % as a string
Aysun
>> x=5;
>> disp(x) % as a value
5
9
4. ARRAY, VECTOR AND MATRIX
An array is a collection values that are represented by a single variable.
• One-dimensional array is called a vector.
• Two-dimensional array is called a matrix.
▪ There are three ways to create a vector
1. By brackets:
>> a=[1 2 3 4 5] % a row vector
a=
1 2 3 4 5
>> a=[1;2;3;4;5] % a column vector
a=
1
2
3
10
4
5
2. By colon operator:
>> t=1:5
t=
1 2 3 4 5
>> t=1:0.5:5
t=
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000
3. By linspace function whose syntax:
linspace(x1,x2,n)
which generates n points between x1 and x2.
>> linspace(0,1,6)
ans = 11
0 0.2000 0.4000 0.6000 0.8000 1.0000
▪ For a matrix:
>> A=[2 4 6 8; 10 12 14 16]
A=
2 4 6 8
10 12 14 16
! size gives the dimension of the matrix A.
>> size(A) row number
ans = column number
2 4
>> B=[1 3 6 ;3 9 7 ; 2 1 0]
>> size(B)
ans = 12
3 3
5. MATHEMATICAL FUNCTIONS AND OPERATIONS
! help elfun helps to take a look at the built-in functions which are commonly in use.
Some mathematical operations on MATLAB:
^ exponentiation
~ negation
* multiplication
/ division
\ left division (applies to matrix algebra)
+ addition
- substraction
13
Ex1. Evaluate 𝑒 −𝑎 𝑠𝑖𝑛𝑥 + 10 𝑦 on MATLAB when 𝑎 = 5, 𝑥 = 2, 𝑎𝑛𝑑 𝑦 = 8.
>> a=5;x=2;y=8;
>> exp(-a)*sin(x)+10*sqrt(y)
ans =
28.2904
Ex2.
>> t=0:pi/5:2*pi
t=
0 0.6283 1.2566 1.8850 2.5133 3.1416 3.7699 4.3982 5.0265 5.6549
6.2832
14
Ex3.
>> A
A=
2 4 6 8
10 12 14 16
>> sin(A)
ans =
0.9093 -0.7568 -0.2794 0.9894
-0.5440 -0.5366 0.9906 -0.2879
Ex4.
>> t=1:5
t=
1 2 3 4 5
>> s=t.^2
s= 15
1 4 9 16 25
THANK YOU FOR YOUR ATTENTION !
16