Introduction to CAD/CAM
ME 608
Lab Class 1: Introduction to MATLAB
Department of Mechanical Engineering
Indian Institute of Technology Guwahati
31st July 2023
Contents
❑ Introduction to MATLAB
o Online Matlab
o GUI
❑ Built in/Elementary Functions
o Mathematical
o Trigonometric
❑ Plot
o 2D Plot
o 3D Plot
❑ Functions and Function files
Introduction
3
Introduction
MATLAB
• MATLAB is a high level programming language and numeric
computing environment invented by Prof. Cleve Moler, University of New
Maxico in late 1970s.
• In 1984, Moler and his student formed MathWork and launched MatLab
commercially to make programming easy and solve numerical problems.
• MATLAB stands for Matrix Laboratory.
• It is an interactive environment for numerical computation, programming
and data visualization statistical and machine learning, signal processing,
control systems, neural networks, image processing and text analytics, etc.
• One of the famous computational software due to its various advantages such
easy to use, developed computational codes easily, familiar graphics user
interface.
4
Introduction
MATLAB Online
• Open the following link https://matlab.mathworks.com/
• Log in into MathWork Account with IITG Credentials
• If You are using Institute license of MATLAB for the first time, create a
MathWork Account with IITG Credentials.
• Once you will log on into it, The Online MATLAB GUI will be launched.
• To access the created files, log in into https://drive.matlab.com/
5
Introduction
MATLAB GUI
6
Introduction
MATLAB Script Files
• It is a sequence of MATLAB Commands. It is also known as M files due to its
extension. The script files are saved as filename.m
• These files can be created using any text editors and then imported into Matlab
• The advantage of using script files over command window is it can be edited
and executed many times. The sequence of commands are repeated in ordered
way every time.
• When a script file calculates an output, it can be displayed in command
window by writing the variable without semicolon (;)
• Script files can be executed by clicking the RUN icon.
• User inputs can be taken with the help of input()
variable_name = input(‘Enter your input’)
• The output of an variable can be displayed with the help of disp()
• The comments in the script can be added by starting statement with #
• Opening a file in a script:
variable = fopen(‘FileName’, ‘Permission’)
7
Built in function
8
Built in Functions
Function Description Example Function Description Example
+ Addition 3+4 sin(x) Sine of angle x(x in radians) Sin(pi/6) = 0.5
- Subtraction 7-3 round(x) Round of the nearest integer Round(17/5) =3
* Multiplication 3*4 fix(x) Round towards zero Fix(13/5) = 2
^ Exponentiation 4^2 ceil(x) Round towards infinity Ceil(11/5) = 3
/ Division 7/2 floor (x) Round towards minus infinity floor (-9/5) = -3
sqrt(x) Square root Sqrt(81) rem (x,y) Returns the reminder after x is divided rem (13,5) = 3
=9 by y
exp Exponential (ex) exp(6) Sign (x) Signum function. Returns 1 if x>0, -1 if Sign (5)=1
x<0, and 0 if x=0
abs(x) Absolute value abs(-24) = log10(x) Base 10 logarithm log10(100)= 2
24
Factorial To find factorial Factorial(5) Input ( ) To take user input x = input(‘Enter x’)
of a number =120
9
Built in Function
Variable Initialization
• Scalar Variable
𝑉𝑎𝑟𝑖𝑎𝑏𝑙𝑒𝑛𝑎𝑚𝑒 = 𝑉𝑎𝑙𝑢𝑒
• Vector Variable
𝑉𝑎𝑟𝑖𝑎𝑏𝑙𝑒𝑣𝑒𝑐𝑡𝑜𝑟 𝑖 = 𝑉𝑎𝑙𝑢𝑒1 𝑉𝑎𝑙𝑢𝑒2 𝑉𝑎𝑙𝑢𝑒3
• Variable with constant Spacing
𝑉𝑎𝑟𝑖𝑎𝑏𝑙𝑒 = 𝐼𝑛𝑖𝑡𝑖𝑎𝑙 𝑣𝑎𝑙𝑢𝑒: 𝑠𝑝𝑎𝑐𝑖𝑛𝑔: 𝐹𝑖𝑛𝑎𝑙 𝑣𝑎𝑙𝑢𝑒
𝑉𝑎𝑟𝑖𝑎𝑏𝑙𝑒 = 𝑙𝑖𝑛𝑠𝑝𝑎𝑐𝑒(𝑥𝑖 , 𝑥𝑓 , 𝑁)
• Storing data in Matrix
𝑉𝑎𝑟𝑖𝑎𝑏𝑙𝑒𝑣𝑒𝑐𝑡𝑜𝑟 𝑖 = 𝑎11 𝑎12 𝑎13 ; 𝑎21 𝑎22 𝑎23
𝑎11 𝑎12 𝑎13
>> 𝑎
21 𝑎22 𝑎23
• Addressing Matrix using Colon:
A(:,n) , A(n, :), A(:, m:n), A(m, n, :), A(m : n, p:q)
Demonstration in MATLAB
10
Built in Function
Array Multiplications
𝑎11 𝑎12 𝑏 𝑏12
Matrix A = and B = 11
𝑎21 𝑎22 𝑏21 𝑏22
• Matrix Multiplication
C=A*B
𝑎11𝑏11 + 𝑎12𝑏21 𝑎11𝑏12 + 𝑎12𝑏22
C=
𝑎21𝑏11 + 𝑎22𝑏21 𝑎21𝑏12 + 𝑎22𝑏22
• Element by element multiplication
𝑎 𝑏 𝑎 𝑏
A .* B= 11 11 12 12
𝑎21𝑏21 𝑎22𝑏22
• Dot Product of two vectors: 𝑑𝑜𝑡(𝑣1 , 𝑣2 )
• Cross Product of two vectors: 𝑐𝑟𝑜𝑠𝑠(𝑣1 , 𝑣2 )
11
Built in Function
Conditional statement
• If - end statement structure
if conditional expression
operation
end
• if-elseif-else-end structure
if conditional expression
operation 1...
elseif
operation 2...
else
operation 3…
end
• Switch case statement
switch switch_expression
case value 1
operation 1
case value 2
operation 2
case value 3
operation 3
otherwise
operation 4
end
12
Built in Function
Loops
• For Loop
Syntax: 𝑓𝑜𝑟 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒𝑖𝑡𝑒𝑟𝑎𝑡𝑖𝑣𝑒 = 𝑥𝑖 : 𝑠𝑝𝑎𝑐𝑖𝑛𝑔: 𝑥𝑛
{Expression}
𝑒𝑛𝑑
• While
Syntax: 𝑤ℎ𝑖𝑙𝑒 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒 = 𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛
{Expression}
𝑒𝑛𝑑
• Do…While
Syntax: 𝐷𝑜 𝑂𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛
while (Condition)
13
Plot
14
Plot
2D
• Plot(x, y, ‘line specifier’, ‘Property Name’, Property Value)
• Axis([xmin, xmax , ymin, ymax])
• Polyshape([x_coordinates],[y_coordinates]) Demonstration in MATLAB
Property Name Description Values
Linewidth Specifies the width of line In points
Markersize Specifies the size of the marker In points
MarkerEdgeColor Specifies the colour of the marker r, g, b, y, c, m, k
MarkerFacecolor Specifies the colour of the filling for filled markers r, g, b, y, c, m, k
Linestyle Specifies the style of the plotted Line -, --, :, -.
Color Specifies the colour of the Line r, g, b, y, c, m, k
Marker Specifies marker of the points in plot +, o, *, . , s, d, p ,h
Xlebel(‘Text’) To label the x axis String
Title(‘Text’) To label the Graph String
Grid On To show the grids
15
Built in Function
Plot: 3D
• Plot3(x, y, z ‘line specifiers’, ‘Property Name’, Property Value)
• The grid of points in 2D plane can be generated by using the function meshgrid
[X, Y] = meshgrid (x, y)
• If Z = f(x, y) then surface plot can be obtained by surf(X, Y, Z)
• The mesh plot can be obtained by using mesh(X,Y,Z)
• To draw contours beneath the mesh plot: meshc(X, Y, Z)
• To draw contours beneath the surface plot: surfc(X, Y, Z)
• To plot 3D contour: contour3(X, Y, Z, No. of contour level)
• To change the viewing angle of the plot: View(angle wrt –y axis, angle wrt xy
plane)
16
User defined Function
17
User Defined Functions
Creating the Function file
Function Definition Line:
function [Output Arguments] = function_name(input arguments)
Function Body:
Output arguments = f(input arguements )
The name of the function file should be same as that of function name.
Save the script as function_name.m
Calling the function:
Variable_name = function_name(Input argument);
18
Differentiation
19
Differentiation
Sysm variables
F = f(variables)
Diff (f, differentiating variable, order of differentiation)
Example:
Sysm x, y
𝑓 = 𝑥 2𝑦
Z = diff(f, x, 1)
>> Z = 2y * x^(2y-1)
20
Report Format
Name:
Roll Number:
Assignment Title:
• Theory
Algorithm • Block Diagram
• Flowchart
Application
Code
Output
21
Thank You
22