Download as DOCX, PDF, TXT or read online from Scribd
Download as docx, pdf, or txt
You are on page 1/ 5
Expt No: 01
Expt Name: Introduction to MATLAB
Objectives: Introduction to the basic interface of MATLAB To understand the basic function of MATLAB To implement various codes using MATLAB Theory: MATLAB (Matrix Laboratory) is a high-level programming language and an integrated development environment (IDE) designed for numerical computation, data analysis, algorithm development, and visualization. It was developed by MathWorks and has become a standard tool in engineering, science, mathematics, and related fields. MATLAB allows users to work with matrices and vectors, perform complex mathematical computations, create data visualizations, and develop algorithms efficiently.
Fig.-1.1: Opening window in MATLAB
The MATLAB desktop environment consists of several components, each serving specific functions. Here are the main functions of a typical MATLAB window: Command Window: The Command Window is where you can directly interact with MATLAB by entering commands and expressions. It displays the results of computations, error messages, and prompts for user input. Users can execute individual commands, run scripts, and access the command history. Editor/Script Window: The Editor or Script Window is used for writing and editing MATLAB scripts and functions. It provides syntax highlighting, code indentation, and debugging tools to aid in script development. Users can create, edit, save, and run MATLAB scripts from this window. Current Folder Window: The Current Folder window provides access to files and folders on your computer. It allows users to navigate directories, open files, and manage MATLAB's working directory. Users can view and manipulate files, including MATLAB scripts, functions, data files, and other resources. Workspace Window: The Workspace window displays information about the variables and data currently stored in MATLAB's workspace. It shows the names, sizes, and data types of variables, as well as their current values. Users can inspect variables, clear workspace contents, and manage workspace data. Command History Window:The Command History window maintains a record of previously executed commands and expressions. It allows users to recall and re-execute commands from previous sessions. Users can search, filter, and manage command history entries to streamline workflow and reduce repetitive tasks. Figure Windows: Figure windows are used for creating and displaying plots, charts, and graphical visualizations in MATLAB. They provide tools for customizing plot appearance, adding annotations, and exporting graphics. Users can interactively explore and analyze data using MATLAB's plotting functions and tools. Help Browser: The Help Browser provides access to MATLAB's documentation, including function references, examples, and tutorials. It allows users to search for specific functions, browse through documentation pages, and access online resources. Users can find detailed information, usage examples, and troubleshooting tips for MATLAB functions and features. Basic Commands of MATLAB: 1. Arithmetic Operations +, -, *, /, ^: Basic arithmetic operators for addition, subtraction, multiplication, division, and exponentiation. mod(a, b): Returns the remainder of a divided by b. sqrt(x): Computes the square root of x. 2. Matrix and Array Operations : (Colon Operator): Used for creating sequences and selecting ranges in arrays. For example, 1:5 creates a vector with elements [1, 2, 3, 4, 5]. linspace(a, b, n): Creates a linearly spaced vector with n elements between a and b. zeros(m, n): Creates an m x n matrix filled with zeros. ones(m, n): Creates an m x n matrix filled with ones. eye(m, n): Creates an identity matrix with dimensions m x n. size(x): Returns the dimensions of an array or matrix x. transpose(x): Returns the transpose of matrix x. 3. Data Management load(filename): Loads data from a file into MATLAB. save(filename, variable): Saves a specified variable to a file. who: Lists all variables in the current workspace. clear variable_name: Clears a specific variable from the workspace. clear without arguments clears all variables. 4. Plotting and Visualization plot(x, y): Creates a 2D line plot with x as the x-axis and y as the y-axis. xlabel('text'), ylabel('text'): Adds labels to the x-axis and y-axis, respectively. title('text'): Adds a title to the plot. legend('label1', 'label2'): Adds a legend to the plot. figure: Creates a new plotting window. scatter(x, y): Creates a scatter plot with x and y. 5. Control Structures if condition: Conditional statement that executes a block of code if the condition is true. else: Specifies an alternate block of code if the if condition is false. for index = start:end: A loop that iterates over a specified range. while condition: A loop that continues to execute while the condition is true. break: Exits a loop early. continue: Skips to the next iteration of a loop. 7. Advanced Operations and Debugging clc: Clears the Command Window. close all: Closes all open figure windows. pause(seconds): Pauses execution for a specified number of seconds. try...catch: Catches errors and handles them without stopping execution. debug: Initiates debugging mode for step-by-step execution.
Some operations using MATLAB:
1. Generate a square wave using its Fourier series: MATLAB Code: 2. Find the CT Average of several students from an Excel File:
Python Advanced Programming: The Guide to Learn Python Programming. Reference with Exercises and Samples About Dynamical Programming, Multithreading, Multiprocessing, Debugging, Testing and More
Python Advanced Programming: The Guide to Learn Python Programming. Reference with Exercises and Samples About Dynamical Programming, Multithreading, Multiprocessing, Debugging, Testing and More