Docsity

Introduction to Programming with MATLAB in Mechanical Engineering, Assignments of Computer Science

Partial preview of the text

Download Introduction to Programming with MATLAB in Mechanical Engineering and more Assignments Computer Science in PDF only on Docsity!

Dr. Zia R Tahir

Problem Solving in Engineering

with MATLAB

Mechanical Engineering Department

Introduction to Matlab

Problem Solving in Engineering with MATLAB Dr ZiaRTahir

Introduction to Matlab

 Outline

  • The Matlab Environment
  • Basic operations
  • Variables
  • Vectors
    • Definition
    • Basic operations
    • 2D Plots
    • Access information
  • Matrices
    • Definition
    • Basic operations
    • Access information
  • Structures

Problem SolvinginEngineeringwithMATLAB DrZiaRTahir

Introduction to Matlab

 Matlab as a numerical calculator

  • Basic arithmetic operations
      • Addition
      • Subtraction
      • Multiplication
    • / Division
  • Hierarchy of arithmetic operations
    1. Content of brackets is evaluated starting from the innermost bracket
    2. Exponentials, from left to right (The symbol for exponentiation is ^)
    3. Multiplications and divisions, from left to right
    4. Additions and subtractions, from left to right

Problem SolvinginEngineeringwithMATLAB DrZiaRTahir

Introduction to Matlab

 Definition of variables

  • To create a variable assign a value to a name using =

    r = 2.

  • Use ; at the end to suppress the output

    r = 2.131;

  • The name of variables is case sensitive

    r = 2.131; R = 90.121;

  • Variables can be created using expressions involving other variables:

    L = 2pir; A = pi*r^

  • The variables appear in the workspace.
    • Note that “pi” is a reserved word. There is no need to define it.

Problem SolvinginEngineeringwithMATLAB DrZiaRTahir

Introduction to Matlab

 Use Maltab help!

  • No need to remember all functions:
    • lookfor
  • No need to remember how to use all functions:
    • help
  • Extensive use of Matlab product help is strongly recommended, specially at the early stages of your learning!

Problem SolvinginEngineeringwithMATLAB DrZiaRTahir

Introduction to Matlab

 Vectors

  • Row vector: using square brackets and separating the components by commas or spaces. - Example: w = [0, 7.8 9, - 1.1];
  • Column vector: using square brackets and separating the components by semicolon - Example: >> v = [-6.2; 5.1; 0.1];

Problem SolvinginEngineeringwithMATLAB DrZiaRTahir

Introduction to Matlab

 Vectors

  • Ways to build large vectors without writing all the components:
  1. Function ones:

    y = ones( 3 , 1 ) ; Vector of 3 rows and 1 column fill in with ones

  2. Concatenate vectors of the same type (row or column) Row vectors:

    v = [1, 8, 4]; w = [7, 9, 1, - 4]; z = [v, w]; Column vectors: v = [1; 4]; w = [7; 9; - 4]; z = [v; w];

Problem SolvinginEngineeringwithMATLAB DrZiaRTahir

Introduction to Matlab

 Operations with vectors

  • Addition and subtraction of vectors of the same type (row or column) and length >> v = [8, 2, 9]; >> w = [-2, 0.1, 7]; >> v + w
  • Scalar product of a row vector and a column vector

    v = [6, 3, 1]; w = [4; 2; 0]; v*w

  • Functions applied to vectors

    v = [30 60 90]; w = sind(v) This means w = [ sind(30), sind(60), sind(90)];

Problem SolvinginEngineeringwithMATLAB DrZiaRTahir

Introduction to Matlab

 Basic 2D plots

Linear plot using two vectors

  • Plot the function cos(x) for the angle x between [0,720] degrees

    x = [0:45:720]; y = cosd(x); plot(x,y)

  • Increase the number of sample points to get more “resolution”.

    x = [0: 5 :720]; y = cosd(x); plot(x,y)

Problem SolvinginEngineeringwithMATLAB DrZiaRTahir

Introduction to Matlab

 Basic 2D plots

Improving the look of the plots by

changing line and colours

plot(x,y,'LineWidth', 4 ) 4 is the thickness of the line plot(x,y, 'r--o') r indicates red colour, -- indicates a discontinuous line o indicates symbol=circle

Problem SolvinginEngineeringwithMATLAB DrZiaRTahir

Introduction to Matlab

 Accessing information from vectors

 The name of the vector followed by the component/s to access

in between brackets

v = [10, - 12, 35, 7.5, 9 , 12, 4]; v(3)

 The result is a scalar

1 2 3 4 5 6 7 10 - 12 35 7.5 9 12 4

Problem SolvinginEngineeringwithMATLAB DrZiaRTahir

Introduction to Matlab

 Accessing information from vectors

 The name of the vector followed by the component/s to access

in between brackets

v = [10, - 12, 35, 7.5, 9 , 12, 4]; v(3)

 We can access several components using the semicolon

v(1:5)

 The result is a row vector

1 2 3 4 5 6 7 10 - 12 35 7.5 9 12 4

Problem SolvinginEngineeringwithMATLAB DrZiaRTahir

Introduction to Matlab

 Accessing information from vectors

 The name of the vector followed by the component/s to access

in between brackets

v = [10, - 12, 35, 7.5, 9 , 12, 4]; v(3)

 We can access several components using the semicolon

v(1:5) v(5:-2:1)

 The result is a row vector

1 2 3 4 5 6 7 10 - 12 35 7.5 9 12 4

Problem SolvinginEngineeringwithMATLAB DrZiaRTahir

Introduction to Matlab

 Accessing information from vectors

 The name of the vector followed by the component/s to access

in between brackets

v = [10, - 12, 35, 7.5, 9 , 12, 4]; v(3)

 We can access several components using the semicolon

v(1:5) v(1:2:5), v(5:-2:1)

 Even if the components don’t follow a pattern

v([1,4,6]) 1 2 3 4 5 6 7 10 - 12 35 7.5 9 12 4