Programming in Matlab 21BRL57
Programming in Matlab 21BRL57
Programming in MATLAB
(21BRL57)
Register Number:
Branch:
Semester:
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
Course Content
Introduction to Matlab
1. Program to understand different modes of input/output operations on numerical data and character strings
8. Program to define a user-defined function and call this function in the main program to realize the task.
12. Program to understand the control structures: if, if-else and switch
14. Program to find the roots of a given polynomial and to construct the polynomial from the given roots of
the polynomial.
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
Introduction to Matlab
MATLAB (short for MATrix LABoratory) is a high-level programming language and environment designed
primarily for numerical computation, data analysis, algorithm development, and visualization. It is widely
used in fields such as engineering, physics, finance, and mathematics.
MATLAB Workflow
1. Develop Code: Write scripts or functions in the MATLAB editor or command window.
2. Run and Debug: Execute your code and view the output in the Command Window.
3. Analyze Data: Perform calculations, manipulate data, and visualize results.
4. Optimize: Refactor and optimize code for efficiency, particularly for large datasets.
5. Deploy: You can generate standalone applications, integrate with other software, or deploy
algorithms to hardware.
Applications of MATLAB
1. Engineering: Modeling physical systems, designing control systems, signal processing, etc.
2. Finance: Data analysis, modeling financial systems, risk management.
3. Image and Video Processing: Analyzing images, detecting patterns, and enhancing visuals.
4. Machine Learning: Training models, classification, clustering, and neural networks.
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
Experiment 01
Aim: Execute a Program to understand different modes of input/output operations on numerical data and
character strings
Below is a MATLAB program that demonstrates various input/output operations, including:
1. Reading numerical data from the user using input().
2. Reading a string input using input() with the 's' flag for strings.
3. Writing numerical data to the command window using disp() and fprintf().
4. Writing character strings to the command window using disp() and fprintf().
5. Reading from a file using fopen(), fscanf(), and fgets().
6. Writing to a file using fopen() and fprintf().
The program for different modes of input/output operations on numerical data and character strings will
follow these steps:
1. Prompt the user to input a numerical value and a string.
2. Display these inputs to the command window.
3. Save the inputs to a file.
4. Read the data from the file and display it.
5. Use various I/O functions like input(), disp(), fprintf(), and file handling functions.
B.
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
Experiment 02
Aim: Execute a Programs to define and perform different operations on vector.
A. Adding Vectors
disp(result_add);
B. Scalar Multiplication
scalar = 3;
% Scalar multiplication
disp(result_scalar_mult);
% Multiply two vectors element by element (requires same size vectors), Element-wise multiplication
disp(result_elementwise_mult);
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
D. Vector Dot Product
disp(dot_product);
% Cross product
disp(cross_product);
magnitude = norm(row_vector);
disp(magnitude);
G. Transpose of a Vector
% Transpose the row vector to make it a column vector using Transpose operator
transpose_row_vector = row_vector.';
disp(transpose_row_vector);
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
Practice Program:
A.
B.
C.
D.
E.
F.
G.
1. Matrix Addition
matrix_add = A + B; % Add two matrices
disp('Matrix Addition (A + B):');
disp(matrix_add);
2. Matrix Subtraction
matrix_sub = A - B; % Subtract matrix B from matrix A
disp('Matrix Subtraction (A - B):');
disp(matrix_sub);
5. Matrix Transpose
matrix_transpose = A.'; % Transpose of matrix A
disp('Transpose of Matrix A:');
disp(matrix_transpose);
6. Determinant of a Matrix
determinant_A = det(A); % Calculate the determinant of matrix A
disp('Determinant of Matrix A:');
disp(determinant_A);
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
7. Inverse of a Matrix
inverse_A = inv(A); % Calculate the inverse of matrix A
disp('Inverse of Matrix A:');
disp(inverse_A);
8. Matrix Size
matrix_size_A = size(A); % Get the size of matrix A
disp('Size of Matrix A:');
disp(matrix_size_A);
9. Identity Matrix
identity_matrix = eye(3); % Create a 3x3 identity matrix
disp('3x3 Identity Matrix:');
disp(identity_matrix);
2.
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
3.
4.
5.
6.
7.
8.
where a, b, and c are coefficients, and x represents the variable. The roots (solutions) of a quadratic equation
can be found using the quadratic formula:
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
Practice Program:
1.
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
Experiment 05
Aim: Program to understand different arithmetic operators
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
Practice Program:
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
Experiment 06
Aim: To Execute simple Program to understand different logical operations
2. OR operation (A OR B)
% | is the logical OR operation
or_result = A | B;
disp(['A OR B = ', num2str(or_result)]);
% Expected output: 1 (true)
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
5. Comparison operations
a) Equal comparison (A = = B)
equal_result = (A == B);
disp(['A == B: ', num2str(equal_result)]);
% Expected output: 0 (false)
1. Union (A ∪ B)
union_result = union(A, B);
disp('Union of A and B (A ∪ B):');
disp(union_result); % Expected output: [1, 2, 3, 4, 5, 6, 7, 8]
2. Intersection (A ∩ B)
intersection_result = intersect(A, B);
disp('Intersection of A and B (A ∩ B):');
disp(intersection_result); % Expected output: [4, 5]
3. Difference (A - B)
difference_result = setdiff(A, B);
disp('Difference of A and B (A - B):');
disp(difference_result); % Expected output: [1, 2, 3]
4. Symmetric Difference (A Δ B)
symmetric_diff_result = setxor(A, B);
disp('Symmetric Difference of A and B (A Δ B):');
disp(symmetric_diff_result); % Expected output: [1, 2, 3, 6, 7, 8]
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
Output:
Set A: […………………………………….]
Set B: […………………………………….]
[……………………………………………………………………………….]
[……………………………………………………………………………….]
[……………………………………………………………………………….]
[……………………………………………………………………………….]
[……………………………………………………………………………….]
Task:
The task here is to define a function that computes the area of a circle based on a given radius. The main
program will ask the user for the radius and then call the user-defined function to calculate and display the
area of the circle.
Step 1: Create the User-Defined Function
Note: Save this function as circleArea.m.
% Function to calculate the area of a circle
% This function takes the radius as input and returns the area
function area = circleArea(radius)
% Formula for the area of a circle: A = pi * r^2
area = pi * radius^2;
end
Output:
Enter the radius of the circle: …………………….
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
Experiment 09
Aim: Writing and Executing the Programs to understand basic algebra functions.
understand basic algebra functions such as solving linear equations, finding roots of polynomials, matrix
operations, and working with symbolic algebra can be computed using Matlab, the following programs are
the examples:
1. Solving a System of Linear Equations
Output
Matrix A: Matrix B: Matrix A + B:
….. …… ….. ……
….. …… ….. ……
disp(myCellArray);
firstElement = myCellArray{1};
secondElement = myCellArray{2};
thirdElement = myCellArray{3};
disp(thirdElement);
fourthElement = myCellArray{4};
disp(fourthElement);
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
6. Modify the first element (change number)
myCellArray{1} = 42;
disp(myCellArray);
myCellArray{2} = 'world';
disp(myCellArray);
myCellArray{end+1} = true;
disp(myCellArray);
disp(nestedCell);
nestedElement = nestedCell{3}{2}; % Accessing the second element inside the third cell
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
Output:
………………………………………………………………………………
………………………
………………………
………………………
………………………………………………………………………………
………………………………………………………………………………
………………………………………………………………………………
………………………………………………………………………………
………………………………………………………………………………
Nested Element: 4
………………………………………………………………………………
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
Experiment 11
Aim: Create a Matlab Program to generate and plot and subplot 2D and 3D data
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
Practice Program:
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
Experiment 12
Aim: Show a sample Program to understand the control structures: if, if-else and switch
1. if Control Structure
% Given number
number = 7;
if number > 0
end
The if-else statement is used when you want to execute one block of code if a condition is true, and another
block if it is false.
% Given number
number = -3;
if number > 0
else
end
The switch statement is used to execute different blocks of code based on the value of a variable.
day = 'Tuesday';
switch day
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
case 'Monday'
case 'Tuesday'
case 'Wednesday'
case 'Thursday'
disp('Almost there!');
case 'Friday'
otherwise
disp('Weekend!');
end
Practice Program:
The while loop repeatedly executes a block of code as long as the given condition remains true.
counter = 1;
end
The for loop is used to iterate over a specified number of times. It is commonly used when the number of
% Loop from 1 to 5
for i = 1:5
end
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
Output
coefficients = [1 -6 11 -6];
% Finding the roots of the polynomial
roots_of_polynomial = roots(coefficients);
disp('Roots of the polynomial:');
disp(roots_of_polynomial);
Practice Program:
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
Viva Question and Answers
1. What is MATLAB?
Answer: MATLAB stands for "Matrix Laboratory." It is a high-level programming language used primarily
for numerical computation, data analysis, visualization, and algorithm development. MATLAB's core
functionality revolves around matrix operations.
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006
Mysore University School of Engineering
8J99+QC7, Manasa Gangothiri, Mysuru, Karnataka 570006
7. What is the difference between = and == in MATLAB?
Answer:
• = is the assignment operator, used to assign a value to a variable.
• == is the equality operator, used to compare whether two values are equal.
Prepared by: Mr Thanmay J S, Assistant Professor, Bio-Medical & Robotics Engineering, UoM, SoE, Mysore 57006