Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

MATLAB Programming Workbook

Uploaded by

Rinu Christy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

MATLAB Programming Workbook

Uploaded by

Rinu Christy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

MATLAB Programming Workbook

Table of Contents

Module I: Introduction

Chapter 1: Programs Using Mathematical and Relational


Expressions
1.1 Basic Mathematical Operations
1.2 Relational Operators
1.3 Logical Operators
1.4 Example Programs
1.5 Exercises

Module II: Matrix, Array, and Basic Mathematical Functions

Chapter 2: Vectors and Matrices


2.1 Array Operations
2.2 Matrix Operations
2.3 Example Programs
2.4 Exercises

Chapter 3: Cell Arrays and Structures


3.1 Understanding Cell Arrays
3.2 Using Structures
3.3 Example Programs
3.4 Exercises

Module III: Basic Plotting, Sounds, and Images

Chapter 4: Plotting and Visualization


4.1 Basic 2D Plotting
4.2 Advanced Plotting Techniques
4.3 Example Programs
4.4 Exercises

Chapter 5: Sound and Image Processing


5.1 Handling Sound Files
5.2 Working with Images
5.3 Example Programs
5.4 Exercises

Module IV: Introduction to Programming


Chapter 6: Selection and Loop Statements
6.1 If-Else and Switch Statements
6.2 For and While Loops
6.3 Vectorizing Code
6.4 Example Programs
6.5 Exercises

Chapter 7: Input/Output and User-Defined Functions


7.1 Scripts and Functions
7.2 Input and Output Operations
7.3 Example Programs
7.4 Exercises

Module V: M-Files

Chapter 8: Functions and Data Handling


8.1 Advanced Functions
8.2 Data Transfer Techniques
8.3 Example Programs
8.4 Exercises
MODULE I: INTRODUCTION

Learning Objectives:

1. Understand mathematical and relational expressions.

2. Use operators effectively in MATLAB programs.

Chapter 1: Programs Using Mathematical and Relational


Expressions

Topics Covered:

 Basic mathematical operations (+, -, *, /, ^)

 Relational operators (<, <=, >, >=, ==, ~=)

 Logical operators (&, |, ~)

Example 1: Program to calculate the area of a circle

radius = 5;

area = pi * radius^2;

disp(['Area of the circle: ', num2str(area)]);

Example 2: Program to compare two numbers

a = 10;

b = 20;

if a > b

disp('a is greater than b');

else

disp('b is greater than or equal to a');

end

Exercises:

1. Write a program to calculate the perimeter of a rectangle.


2. Check if a number is even or odd using relational operators.

3. Write a program to find the maximum of three numbers.


MODULE II: MATRIX, ARRAY, AND BASIC MATHEMATICAL
FUNCTIONS

Learning Objectives:

1. Work with vectors and matrices.

2. Understand and apply array operations and matrix operations.

3. Use cell arrays, structures, and string manipulations.

Chapter 2: Vectors and Matrices

Example 1: Matrix multiplication

A = [1, 2; 3, 4];

B = [5, 6; 7, 8];

C = A * B;

disp('Result of matrix multiplication:');

disp(C);

Example 2: Array operations (element-wise)

A = [1, 2, 3];

B = [4, 5, 6];

C = A .* B; % Element-wise multiplication

disp('Element-wise multiplication:');

disp(C);

Chapter 3: Cell Arrays and Structures

Example 1: Creating and accessing a cell array

cellArray = {'MATLAB', 42, [1, 2, 3]};

disp('First element of cell array:');

disp(cellArray{1});

Example 2: Using structures

student.name = 'John';

student.age = 21;
student.marks = [85, 90, 78];

disp('Student structure:');

disp(student);

Exercises:

1. Create a program to add two matrices and display the result.

2. Write a program to store and display student details using


structures.

3. Convert a numeric array to a string array.


MODULE III: BASIC PLOTTING, SOUNDS, AND IMAGES

Learning Objectives:

1. Create advanced plots.

2. Process sound and image files.

3. Understand object-oriented programming basics.

Chapter 4: Plotting and Visualization

Example 1: Basic 2D Plot

x = 0:0.1:10;

y = sin(x);

plot(x, y, '-r', 'LineWidth', 2);

title('Sine Wave');

xlabel('x');

ylabel('sin(x)');

grid on;

Example 2: Adding multiple plots

x = linspace(0, 10, 100);

plot(x, sin(x), 'r', x, cos(x), 'b');

legend('sin(x)', 'cos(x)');

title('Trigonometric Functions');

Chapter 5: Sound and Image Processing

Example 1: Read and play a sound file

[y, Fs] = audioread('example.wav');

sound(y, Fs);

Example 2: Load and display an image

img = imread('example.jpg');

imshow(img);

title('Loaded Image');
Examples: Create a bar plot from data.

Program to create a bar plot

data = [5, 10, 15, 20];

bar(data);

title('Bar Plot');

xlabel('Categories');

ylabel('Values');

Explanation: 'bar' creates a bar chart, and we set labels and title for better
visualization.

Examples: Convert a colored image to grayscale.

Program to load and convert an image to grayscale

img = imread('example.jpg');

grayImg = rgb2gray(img);

imshow(grayImg);

title('Grayscale Image');

Explanation: 'imread' loads an image, 'rgb2gray' converts it to grayscale,


and 'imshow' displays it.

Examples: Load and visualize sound waveforms.

Program to read and visualize a sound file

[y, Fs] = audioread('example.wav');

plot(y);

title('Sound Waveform');

xlabel('Time');

ylabel('Amplitude');

Explanation: 'audioread' reads the sound file, and 'plot' visualizes the
waveform.

Exercises:

1. Create a bar plot for a given data set.


2. Load an image, convert it to grayscale, and display both.

3. Read a sound file and visualize its waveform.

MODULE IV: INTRODUCTION TO PROGRAMMING

Learning Objectives:

1. Use selection and loop statements.

2. Write scripts and user-defined functions.

3. Input and output values in programs.


Chapter 6: Selection and Loop Statements

Example 1: Using if-else

x = 10;

if mod(x, 2) == 0

disp('x is even');

else

disp('x is odd');

end

Example 2: Using a for loop

for i = 1:5

disp(['Iteration: ', num2str(i)]);

end

Examples: Display numbers from 1 to 10 using a loop.

Program to display numbers from 1 to 10 using a loop

for i = 1:10

disp(i);

end

Explanation: 'for' loop iterates over numbers from 1 to 10, and 'disp' prints
each number.

Examples: Calculate the factorial of a number.

Program to calculate the factorial of a number

number = 5; % Example value

factorial = 1;

for i = 1:number

factorial = factorial * i;

end

disp(['Factorial: ', num2str(factorial)]);


Explanation: Multiply each number from 1 to 'number' to calculate the
factorial.

Examples: Use a switch-case structure to display the day of the


week.

Program to display the day of the week using switch-case

dayNum = 3; % Example value

switch dayNum

case 1

disp('Monday');

case 2

disp('Tuesday');

case 3

disp('Wednesday');

case 4

disp('Thursday');

case 5

disp('Friday');

case 6

disp('Saturday');

case 7

disp('Sunday');

otherwise

disp('Invalid day number.');

end

Explanation: 'switch' matches the case based on 'dayNum' and displays


the correspondi

Exercises:
1. Write a program to display numbers from 1 to 10 using a loop.

2. Write a program to calculate the factorial of a number using a loop.

3. Use a switch-case to display the day of the week based on a


number.
MODULE V: M-FILES

Learning Objectives:

1. Write advanced functions.

2. Solve advanced mathematical problems.

3. Handle data transfer effectively.

Chapter 7: Functions and Data Handling

Example 1: Defining a simple function

function result = squareNumber(x)

result = x^2;

end

Example 2: Data transfer (saving and loading variables)

data = rand(10);

save('dataFile.mat', 'data'); % Save data

load('dataFile.mat'); % Load data

disp('Loaded Data:');

disp(data);

Exercises:

1. Write a function to calculate the sum of an array.


2. Save an array to a file and reload it.

3. Write a program that reads a file and displays its content.


Module III: Basic Plotting, Sounds, and Images

Description:
Learn how to visualize data, process sound files, and handle image data.

Examples:

1. Create a bar plot from data.

matlab

Copy code

% Program to create a bar plot

data = [5, 10, 15, 20];

bar(data);

title('Bar Plot');

xlabel('Categories');

ylabel('Values');

% Explanation: 'bar' creates a bar chart, and we set labels and title for
better visualization.

2. Convert a colored image to grayscale.

matlab

Copy code

% Program to load and convert an image to grayscale

img = imread('example.jpg');

grayImg = rgb2gray(img);

imshow(grayImg);

title('Grayscale Image');

% Explanation: 'imread' loads an image, 'rgb2gray' converts it to


grayscale, and 'imshow' displays it.

3. Load and visualize sound waveforms.

matlab

Copy code
% Program to read and visualize a sound file

[y, Fs] = audioread('example.wav');

plot(y);

title('Sound Waveform');

xlabel('Time');

ylabel('Amplitude');

% Explanation: 'audioread' reads the sound file, and 'plot' visualizes the
waveform.

Module IV: Introduction to Programming

Description:
Understand conditional statements, loops, and user-defined functions.

Examples:

1. Display numbers from 1 to 10 using a loop.

matlab

Copy code

% Program to display numbers from 1 to 10 using a loop

for i = 1:10

disp(i);

end

% Explanation: 'for' loop iterates over numbers from 1 to 10, and 'disp'
prints each number.

2. Calculate the factorial of a number.

matlab

Copy code

% Program to calculate the factorial of a number

number = 5; % Example value

factorial = 1;

for i = 1:number
factorial = factorial * i;

end

disp(['Factorial: ', num2str(factorial)]);

% Explanation: Multiply each number from 1 to 'number' to calculate the


factorial.

3. Use a switch-case structure to display the day of the week.

matlab

Copy code

% Program to display the day of the week using switch-case

dayNum = 3; % Example value

switch dayNum

case 1

disp('Monday');

case 2

disp('Tuesday');

case 3

disp('Wednesday');

case 4

disp('Thursday');

case 5

disp('Friday');

case 6

disp('Saturday');

case 7

disp('Sunday');

otherwise

disp('Invalid day number.');

end
% Explanation: 'switch' matches the case based on 'dayNum' and displays
the corresponding day.

You might also like