Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Lab 3-Working with functions in MATLAB

Usman Khaliq Yousaf Baig Aliza khan Syed Anees Asad Zuhaa Ejaz Kazmi
Department of Department of biomedical Department of biomedical Department of biomedical Department of biomedical
biomedical engineering engineering engineering engineering engineering
Air university Air university Air university Air university Air university
Islamabad, Pakistan Islamabad, Pakistan Islamabad, Pakistan Islamabad, Pakistan Islamabad, Pakistan
usmankhaliq856@gm hyousafbaig@gmail.com Alizakhann@gmail.com aneesaleen@gmail.com Zuhaaejaz12@gmail.com
ail.com

I. Tools learnt how to make new functions ourselves and


implement them.
The fundamental software we used was:

• Matlab
Matlab has a variety of in built functions and Functions in MATLAB:
a multitude tools that aided us in this basic
lab and will be of great use for future signal • Function definition:
processing. In MATLAB, the function definition
comprises of four basic building blocks.
A. Keyword:
To build a function we need to use
II. INTRODUCTION the keyword function prior to the
Signal processing is an engineering subfield that function
allows us to scrutinize, modify and synthesize B. Output argument:
various signals such as images, sounds, videos. This The output argument stores the
allows us to observe things that aren’t visible answer after he function if
otherwise. As biomedical engineers signal processing performed
is a paramount area to master to efficiently generate C. Function name:
and manipulate signals as per our requirements. Is used to denote the function and
MATLAB, short for Matrix Laboratory, is a powerful later allows us to call it
and widely-used programming language and D. Input arguments:
numerical computing environment designed for These are the values passed to the
scientific computing, data analysis, and visualization. function on which the function
Developed by MathWorks, MATLAB integrates operates.
computation, visualization, and programming in an E. Function body:
easy-to-use environment, making it a versatile tool It tells what the function is
for engineers, scientists, and researchers. Its primary supposed to do
strength lies in its ability to handle matrix operations For example
seamlessly, making it particularly suitable for tasks function[sum_result,product_result
involving linear algebra, signal processing, image ]= calculate_sum_and_product(a,
analysis, and control system design. MATLAB's b)
extensive built-in libraries and toolboxes, coupled • Saving a function:
with a user-friendly syntax, empower users to To save a function we first write it out in the
perform complex computations and visualize data script and then save it with the same name
with ease. In this lab we delved deeper into matlab’s as the one utilized in the function definition.
working and learnt the use of functions. We were
introduced to many pre defined functions and also
• Calling the function: and `p`.`x = input('enter the value of matrix
to use a function we need to call it in the ');`: This line prompts the user to enter the
command window using the same name and matrix values.
the same input arguments. s = sum(x): Calculates the sum of all
For example: elements in the matrix `x` using the `sum`
[sum_result,product_result]= function.
calculate_sum_and_product(a, b) p = 1 Initializes the variable `p` to 1, which
will be used to calculate the product of the
matrix elements.The `for` loop iterates
through each element of the matrix (`x`),
multiplying it with the current product (`p`).
This loop effectively computes the product
III. LAB TASKS of all elements in the matrix.The `end`
keyword indicates the end of the function.
Task 1:
Task 2:
Create a function such that it takes one input. The
function should return two outputs. The first output Create a function that takes two vectors as inputs and
should be the sum of all elements in the input vector, returns a single vector that is the difference of the two
and the second output should be the product of all vectors (i.e., the first element of the output should be
elements in the vector. the difference of the first elements of the input
vectors.)
• Code:
• Code:

• Execution:

• Execution:

• Explanation:
function [s, p] = sum_and_product(x)`: This
line defines a MATLAB function named
`sum_and_product` that takes one input
parameter `x` and returns two outputs `s` • Explanation:
function [s] = subtraction(x, y): This line
declares a MATLAB function named
`subtraction` that takes two vectors (`x` and
`y`) as input parameters and returns a single
vector `s` as the output.`for i = 1:length(x)`:
This is a `for` loop that iterates through each
element of the vectors `x` and `y`. s(i) = x(i)
- y(i);`: Inside the loop, the difference
between the corresponding elements of
vectors `x` and `y` is calculated and stored
in the output vector `s`.

IV. CONCLUSION

In this Lab our primary goal was to familiarize


ourselves with the pre defined and user defined
functions of MATLAB and utilize them to perform
some tasks. We were introduced to defining
functions, and maneuvering functions to perform
desired tasks.

V. REFRENCES

[1]. "Digital Signal Processing Using MATLAB" by

Vinay K. Ingle and John G. Proakis

[2]. "Signals and Systems: A Primer with MATLAB"

by Matthew N. O. Sadiku

[3]. "MATLAB for Engineers" by Holly Moore

You might also like