🧮

MATLAB Basics and Operations

Sep 2, 2025

Overview

This lecture covers basic and advanced MATLAB mathematical statements, built-in functions, array operations, display formatting, error types, and concludes with homework involving scripting for specific engineering applications.

MATLAB Mathematical Statements & Operators

  • Scalars are single-value variables (1x1 arrays) in MATLAB.
  • Assignment operator (=) assigns the evaluated right side to the left-side variable.
  • Standard operators: + (add), - (subtract), * (multiply), / (divide), ^ (power).
  • Programming '=' is assignment, not equality like in math.
  • Operator precedence: parentheses > power > multiplication/division > addition/subtraction.

Parentheses & Operator Precedence Examples

  • Correct use of parentheses is critical for accurate expression evaluation.
  • Parentheses enforce evaluation order in complex formulas.
  • Example: x^3+2x-1/(x(x-4)) must be written carefully; grouping denominators/numerators as needed.

Array Operations in MATLAB

  • Use dot operators (.* ./ .^) for element-wise multiplication, division, and power on arrays.
  • Addition/subtraction use + and - as in scalars.
  • Arrays must be the same size for element-wise operations.

Built-in Math Functions

  • abs(x): absolute value
  • sqrt(x): square root
  • nthroot(x,n): n-th root of x (returns real root)
  • sign(x): returns 1 for positive, -1 for negative, 0 for zero
  • rem(x,y): remainder of x/y (integer division)
  • exp(x): e^x
  • log(x): natural log (base e)
  • log10(x): base-10 log

Trigonometric and Hyperbolic Functions

  • Trig: sin(x), cos(x), tan(x) (x in radians); asin(x), acos(x), atan(x) for inverse
  • Hyperbolic: sinh(x), cosh(x), tanh(x)
  • Add 'd' (e.g., sind(x)) to indicate degrees as input

Rounding & Discrete Math Functions

  • round(x): nearest integer
  • fix(x): truncates toward zero
  • floor(x): nearest lower integer
  • ceil(x): nearest higher integer
  • factor(x): prime factors
  • gcd(x,y): greatest common divisor
  • lcm(x,y): least common multiple
  • factorial(x): product of all positive integers up to x
  • nchoosek(n,k): number of k-combinations from n

Display and Formatting in MATLAB

  • Format options: 'short' (4 decimals), 'long' (15 decimals), 'short/long e' (scientific), 'rat' (fraction), etc.
  • disp() function displays values or text in command window.
  • num2str() and int2str() convert numbers to strings for display.

Special Values & Error Types

  • pi, i, j, inf, nan are predefined special variables.
  • Avoid redefining special variables to prevent issues.
  • Syntax error: code does not follow MATLAB rules (e.g., misspelled function).
  • Runtime error: problem occurs during execution (e.g., invalid input type).
  • Logical error: code runs but logic is incorrect.

Useful MATLAB Commands

  • help functionname: shows help for function
  • lookfor keyword: searches for keyword
  • clear: clears workspace
  • clc: clears command window
  • diary filename: saves command window output to file
  • who/whos: lists workspace variables

Example Engineering Script

  • Inputs: prompt user for lengths, angles, or temperature ranges.
  • Constants: define any necessary fixed values.
  • Calculations: use input and constants to compute outputs.
  • Display: output results in tables with headers, using disp and formatting.

Key Terms & Definitions

  • Scalar — a single value variable (1x1 array)
  • Assignment Operator (=) — assigns value from right to left variable
  • Dot Operator (. ./ .^ )* — element-wise operation for arrays
  • disp() — displays text/values in command window
  • num2str(), int2str() — convert numbers to strings for display
  • pi — mathematical constant (~3.14159)
  • rem(x, y) — remainder after integer division
  • inf — infinity in MATLAB
  • nan — not-a-number in MATLAB

Action Items / Next Steps

  • Review posted Sample Problem #1 and Quiz #1.
  • Complete Assignment 2: write a script to compute robot arm hand position for user-input lengths and angles; test with provided values.
  • Submit your script with commented sample output by Friday midnight.