Programming for Engineers Module 1 Lecture 1&2 PDF
Document Details
Uploaded by Deleted User
UPES
Dr. Rupendra Kumar Pachauri
Tags
Summary
This document covers the basics of MATLAB programming. It introduces variables, arrays, vectors, strings, functions, operators, and expressions. Topics also include matrix operations, indexing, and slicing. Applications to vector calculus are discussed. This is a lecture document, not a past paper.
Full Transcript
Programming for Engineers Module – 1 || Lecture - 1 Introduction to Programming Basics Dr. Rupendra Kumar Pachauri Electrical Cluster Module 1: Introduction to Programming Basics (2T + 8 Lab Sessions) o Introduction to basics of computer programming and terminologies (v...
Programming for Engineers Module – 1 || Lecture - 1 Introduction to Programming Basics Dr. Rupendra Kumar Pachauri Electrical Cluster Module 1: Introduction to Programming Basics (2T + 8 Lab Sessions) o Introduction to basics of computer programming and terminologies (variables, input, output, array, vector, strings, functions, command, syntax, algorithm etc.) o Getting started with MATLAB interface and workspace. o Data types, variables, operators, and expressions. o Scripting and debugging techniques. o Matrix operations, indexing, and slicing. o Vector calculus and applications. MATLAB Overview MATLAB (Matrix Laboratory) is: A high-level programming language and environment designed for numerical computing, data analysis, algorithm development, and visualization. It is widely used in engineering, science, and mathematics for its powerful computational capabilities and easy-to-use interface. Key Terminologies in MATLAB Variables Variables are used to store data that can be used and manipulated within a program. In MATLAB, variables are created by simply assigning a value to a name. Example a = 5; % Scalar variable b = [1, 2, 3, 4, 5]; % Row vector c = [1; 2; 3; 4; 5]; % Column vector Key Terminologies in MATLAB Input and Output Input: To get user input, MATLAB uses the input function. Output: To display output, MATLAB uses the disp or fprintf functions. Example: % Input x = input('Enter a number: '); % Output disp(['You entered: ', num2str(x)]); fprintf('You entered: %d\n', x); Key Terminologies in MATLAB Array and Vector Array: A collection of elements of the same type, arranged in rows and columns. Vector: A one-dimensional array, which can be either a row vector or a column vector. % Array A = [1, 2, 3; 4, 5, 6; 7, 8, 9]; % Row Vector rowVec = [1, 2, 3, 4, 5]; % Column Vector colVec = [1; 2; 3; 4; 5]; Key Terminologies in MATLAB Strings Strings are used to store text. In MATLAB, strings can be created using single quotes or the string function. Example: str1 = 'Hello, MATLAB!'; str2 = string('Hello, World!’); Key Terminologies in MATLAB Functions Functions are blocks of code designed to perform a specific task. In MATLAB, functions are defined using the function keyword. EXAMPLE: function result = addNumbers(a, b) result = a + b; end Example: Calculate the area of a circle % Function to calculate area function area = calculateArea(radius) area = pi * radius^2; end % Main script radius = input('Enter the radius of the circle: '); area = calculateArea(radius); fprintf('The area of the circle with radius %.2f is %.2f\n', radius, area); Example on Array In MATLAB, data structures like arrays and cell arrays are fundamental for storing and manipulating data efficiently. Arrays and Array Lists 1. Arrays: o In MATLAB, arrays are the basic data structure used to store collections of data. o Arrays can be one-dimensional (vectors), two-dimensional (matrices), or multi-dimensional. Example : Creating an array % Accessing elements % One-dimensional array (vector) element = A(3); % Access the third element of A A = [1, 2, 3, 4, 5]; % Modifying elements % Two-dimensional array (matrix) A(4) = 10; % Modify the fourth element of A B = [1, 2, 3; 4, 5, 6; 7, 8, 9]; % Multi-dimensional arrays % Numeric arrays (vectors and matrices) D = rand(3, 4); % 3x4 matrix with random numbers A = [1, 2, 3, 4, 5]; % Row vector % Concatenating arrays B = [1; 2; 3; 4; 5]; % Column vector E = [A, B]; % Concatenate A and B horizontally C = [1, 2, 3; 4, 5, 6]; % 2x3 matrix F = [A; B]; % Concatenate A and B vertically Operators, and Expressions in MATLAB Operators % Arithmetic operations Operators perform operations on variables and x = 5; values. MATLAB supports several types of y = 2; operators: Arithmetic Operators: sum = x + y; % 7 Addition: + diff = x - y; % 3 Subtraction: - prod = x * y; % 10 Multiplication: * quot = x / y; % 2.5 Division: / pow = x^y; % 25 Power: ^ Operators, and Expressions in MATLAB % Relational operations a = 5; Relational Operators: b = 10; Equal to: == isEqual = (a == b); % false Not equal to: ~= isNotEqual = (a ~= b); % true Greater than: > isGreater = (a > b); % false Less than: < isLess = (a < b); % true Greater than or equal to: >= isGreaterOrEqual = (a >= b); % false Less than or equal to: