0% found this document useful (0 votes)
9 views35 pages

Introduction To MATLAB

The document is an introduction to MATLAB, covering essential topics such as basic arithmetic operations, variables, arrays, and matrices, as well as loops and built-in functions. It includes exercises for practical application and emphasizes MATLAB's capabilities in numerical analysis and scientific computing. The document also provides guidance on plotting and accessing MATLAB documentation.

Uploaded by

Prisha Gautam
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)
9 views35 pages

Introduction To MATLAB

The document is an introduction to MATLAB, covering essential topics such as basic arithmetic operations, variables, arrays, and matrices, as well as loops and built-in functions. It includes exercises for practical application and emphasizes MATLAB's capabilities in numerical analysis and scientific computing. The document also provides guidance on plotting and accessing MATLAB documentation.

Uploaded by

Prisha Gautam
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

Introduction to MATLAB

Numerical Analysis and Scientific Computing-I


MTH308

January 09,2026

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 1 / 35


Contents

1 Introduction
2 Basic arithmetic operations
3 Variables and simple function scripts in MATLAB
4 Displaying output (disp, fprintf, placeholders)
5 Arrays and matrices
6 Indexing and slicing of arrays and matrices
7 Elementwise versus matrix operations (.*, ./ vs *, /)
8 Basic loops (for, while, etc.)
9 Common built-in functions
10 User-defined functions
11 Basic plotting
12 Using help and doc for MATLAB documentation

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 2 / 35


Introduction

MATLAB stands for MATrix LABoratory


High-performance language for technical computing
Integrates computation, visualization, and programming
Widely used in engineering, science, and mathematics
Supports matrix operations, plotting, and algorithm
development
Includes toolboxes for specialized applications

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 3 / 35


Basic Arithmetic Operations

MATLAB supports standard arithmetic operators. Explicitly,


MATLAB is just a very powerful calculator .

Operator Meaning Examples:


+ Addition 1) >> 1+2
- Subtraction 2) >>1-2
* Multiplication 3) >>1*2
/ Division 3) >> 1/2
ˆ Exponentiation 4) >> 24

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 4 / 35


Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 5 / 35
Exercises:
Calculate the following using the correct order of operations -
Parantheses > Multiplication, Division > Addition, Subtraction
and verify the results using MATLAB.
1 6+4*3-2
2 (4 + 2) * 3 / 2
3 4 + (2 * 3)ˆ2
4 4*2/2*2

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 6 / 35


Variables and Simple Function Scripts in MATLAB

Variables store values: x = 5;


Variable names must start with a letter
Functions accept inputs and return outputs : y=sin(x);
Scripts (.m files) contain sequences of commands
Example script: example1.m

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 7 / 35


Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 8 / 35
Exercises:
1 For a=2 , b=3 , c=4 store the value of sum in d.
2 For x = 1/2 .Calculate eˆx.

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 9 / 35


Displaying Output

disp(x): displays value of x


fprintf: formatted output with placeholders
Placeholders: %d (integer), %f (float), %s (string)
For floating point numbers, you can use .b%f, to display b
many decimal points of the number.
Examples:
>> x = 5;
>> disp(5);
>> y = sin(x);
>> fprintf(’Value of y is %d\n ’, y);

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 10 / 35


Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 11 / 35
Exercises:
1 Display the value of y as a floating point number, with 2
decimal points.
2 Find y /x and display that with 5 decimal points.

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 12 / 35


Arrays and Matrices

Matrix: 2D array of numbers


Row vector: [1 2 3]
Column vector: [1; 2; 3]
Matrix creation:
>> A = [1 2 3; 4 5 6; 7 8 9]

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 13 / 35


Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 14 / 35
Exercises:
Save a 3x3 identity matrix as I, and display it.
Find the transpose of R, using the transpose function,
and name it as RT. Is it the same as C?

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 15 / 35


Indexing and Slicing of Arrays and Matrices

Access elements with A(i,j)


Colon (:) operator for ranges
end keyword for last index
Example : >> A(2,3) ( element at row 2, col 3)
ans = 6
>> A(1:2, :) ( first two rows)
ans =
123
456
>> A(:, end) (last column)
ans =
3
6
9

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 16 / 35


Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 17 / 35
Exercises:
1 Find and display the first column and row of the matrix I,
from the previous exercises.
2 Find and display the second element of R and RT . Are
they the same? Will they always be?

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 18 / 35


Elementwise vs Matrix Operations

.*, ./, .ˆ operate element-by-element


*, / perform matrix algebra
Example :
>> A = [1 2; 3 4];
>> B = [5 6; 7 8];
>> A .* B
>> A * B

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 19 / 35


Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 20 / 35
Exercise:
1 Find and display A + B.
2 Find the result of A./B and A/B. Are they the same? What
do you think each of them returns?

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 21 / 35


Basic Loops

for loop: fixed number of iterations


while loop: repeats while condition true
break exits loop, continue skips to next iteration
1)for loop example
for i = 1:5
disp(i);
end
2) while loop example
x = 1;
while x < 10
x = x * 2;
end
disp(x);

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 22 / 35


Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 23 / 35
Exercises:
1 Write a for loop to find the factorial of 15. Check if your
answer is correct using the factorial() function.
2 Write a while loop that finds the largest multiple of 17 that
is less than 150.

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 24 / 35


Common Built-in Functions

sin, cos, tan


exp, log, log10
sqrt, abs
max, min, sum
size, length
Examples:
>> sin(pi/2)
ans = 1
>>sqrt(16)
ans = 4
>> max([1 5 3])
ans = 5

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 25 / 35


Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 26 / 35
Exercises:
1 Using length, verify the number of elements in
V = [1, 9, 8, 74, 2, 91, 3, 8].
2 Using size, verify the dimensions of the I matrix from
previous exercises.
3 Using in-built functions, find the minimum, maximum and
sum of elements in V , and display them.
4 Using in-built functions, find and display the element-wise
logarithm and exponent of V . (Note that you do not need a
loop here)

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 27 / 35


User-defined Functions

You can also define your own functions, using the format :
function y = func name(inputs)
- Code to calculate y -
end
Example:
function y = matrix product(A,B)
y = A*B;
end
User-defined functions must be placed at the end of script
files for the script to execute.

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 28 / 35


Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 29 / 35
Basic Plotting

plot(x, y) for 2D graphs


Add titles, labels, legends
Multiple plots in one figure
Example:
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y);
title(’Sine Wave’);
xlabel(’x’);
ylabel(’sin(x)’);
grid on;

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 30 / 35


Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 31 / 35
Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 32 / 35
Exercises:
1 Using the same x vector, plot a similar graph for cos(x).
2 Plot a graph of exp(x), for the range of x starting at 0, and
ending at 100, with a step increment of 0.5.

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 33 / 35


Using help and doc

help function name: Show brief help in Command


Window
doc function name: opens full documentation
lookfor keyword: searches for functions by keyword
Example:
>> help sin
SIN Sine of argument in radians.
>> doc plot
Opens detailed plotting guide

Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 34 / 35


Numerical Analysis and Scientific Computing-I MTH308 Introduction to MATLAB 35 / 35

You might also like