Lab Tutorial: Introduction to MATLAB
Objective: To familiarise students with the MATLAB interface, basic operations, commands,
and mathematical computations.
1. Getting Started with MATLAB
1.1 Starting MATLAB
Open MATLAB from the Start menu or a desktop icon.
Familiarize yourself with the MATLAB desktop environment:
o Command Window: Main area for entering commands.
o Current Folder: Shows files in the active directory.
o Workspace: Displays variables created during the session.
o Command History: Lists previously executed commands.
Customize the layout via the Layout menu.
1.2 Entering Commands
Click in the Command Window to activate it.
Examples:
>> a = [2 4 7];
>> b = 3 + 5;
>> c = sin(80);
Rules to remember:
o MATLAB is case-sensitive.
o Use ; to suppress output.
o Use the up arrow key to recall previous commands.
1.3 Getting Help
Use help followed by a function name to learn about it. Example:
>> help linspace
Access more options under the Help menu in the toolbar.
1.4 Exiting MATLAB
Type exit or quit in the Command Window, or click the close button.
2. Basic Operations and Variable Assignment
2.1 Arithmetic Operations
MATLAB uses these symbols:
o Addition: +
o Subtraction: -
o Multiplication: *
o Division: /
o Exponentiation: ^
Example:
>> result = 6^3 - (7 + 5)/2 + 9*4;
2.2 Errors and Debugging
If there is a syntax error, MATLAB highlights the issue. Example:
>> 4x^4
Error: Invalid expression.
Correct input:
>> 4*x^4;
2.3 Aborting Calculations
Use Ctrl + C to stop a running command.
3. Vectors and Matrices
3.1 Creating Vectors
Row vector:
>> v = [1 2 3 4];
Column vector:
>> v = [1; 2; 3; 4];
Using the colon operator:
>> v = 1:5;
3.2 Vector Operations
Element-wise operations use .*, ./, or .^.
>> x = [1 2 3];
>> y = [4 5 6];
>> z = x .* y;
3.3 Creating Matrices
Example:
>> A = [1 2 3; 4 5 6; 7 8 9];
Access elements:
>> A(2,3); % Element in 2nd row, 3rd column
4. Numerical Operations
4.1 Precision and Formatting
Control output format:
>> format long; % Display more digits
>> format short; % Default format
4.2 Rounding Functions
Examples:
>> round(53/7);
>> floor(53/7);
5. Managing Variables
5.1 Clearing Variables
Clear all variables:
>> clear;
Clear specific variables:
>> clear x;
5.2 Viewing Variables
Use who or whos to list active variables.
6. Symbolic Operations
6.1 Creating Symbolic Variables
Example:
>> syms x y;
>> z = x^2 + y^2;
6.2 Simplifying Expressions
Use simplify:
>> simplify((x^3 - y^3)/(x-y));
Conclusion: This lab introduces MATLAB’s basic interface and computational capabilities.
Practice the examples above to build a strong foundation for more advanced topics.