In this section, we will cover the fundamental operations and functions in MATLAB. This includes arithmetic operations, relational operations, logical operations, and some basic built-in functions. Understanding these basics is crucial for performing more complex tasks in MATLAB.

  1. Arithmetic Operations

MATLAB supports a variety of arithmetic operations. Here are the basic ones:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Exponentiation (^)

Example:

a = 5;
b = 3;

% Addition
sum = a + b; % sum = 8

% Subtraction
difference = a - b; % difference = 2

% Multiplication
product = a * b; % product = 15

% Division
quotient = a / b; % quotient = 1.6667

% Exponentiation
power = a ^ b; % power = 125

Exercise:

  1. Define two variables x and y with values 10 and 4, respectively. Perform all the arithmetic operations on these variables and display the results.

  1. Relational Operations

Relational operations compare two values and return a logical value (true or false).

  • Equal to (==)
  • Not equal to (~=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)

Example:

x = 10;
y = 4;

% Equal to
isEqual = (x == y); % isEqual = false

% Not equal to
isNotEqual = (x ~= y); % isNotEqual = true

% Greater than
isGreater = (x > y); % isGreater = true

% Less than
isLess = (x < y); % isLess = false

% Greater than or equal to
isGreaterOrEqual = (x >= y); % isGreaterOrEqual = true

% Less than or equal to
isLessOrEqual = (x <= y); % isLessOrEqual = false

Exercise:

  1. Compare the variables x and y from the previous exercise using all the relational operations and display the results.

  1. Logical Operations

Logical operations are used to combine or invert logical values.

  • AND (&)
  • OR (|)
  • NOT (~)

Example:

a = true;
b = false;

% AND
andResult = a & b; % andResult = false

% OR
orResult = a | b; % orResult = true

% NOT
notResult = ~a; % notResult = false

Exercise:

  1. Define two logical variables p and q with values true and false, respectively. Perform all the logical operations on these variables and display the results.

  1. Basic Built-in Functions

MATLAB provides a wide range of built-in functions for various operations. Here are some commonly used ones:

  • sqrt(x): Square root of x
  • abs(x): Absolute value of x
  • round(x): Round x to the nearest integer
  • floor(x): Round x down to the nearest integer
  • ceil(x): Round x up to the nearest integer
  • max(x, y): Maximum of x and y
  • min(x, y): Minimum of x and y

Example:

x = -7.5;

% Square root
sqrtResult = sqrt(abs(x)); % sqrtResult = 2.7386

% Absolute value
absResult = abs(x); % absResult = 7.5

% Round
roundResult = round(x); % roundResult = -8

% Floor
floorResult = floor(x); % floorResult = -8

% Ceil
ceilResult = ceil(x); % ceilResult = -7

% Max and Min
maxResult = max(x, 0); % maxResult = 0
minResult = min(x, 0); % minResult = -7.5

Exercise:

  1. Define a variable z with the value -3.7. Use the built-in functions to find the square root of the absolute value of z, round z to the nearest integer, and find the maximum and minimum between z and 5.

Summary

In this section, we covered the basic operations and functions in MATLAB, including arithmetic, relational, and logical operations, as well as some essential built-in functions. These fundamentals are crucial for performing more complex tasks and will be used throughout the course.

Solutions to Exercises:

x = 10;
y = 4;

% Addition
sum = x + y; % sum = 14

% Subtraction
difference = x - y; % difference = 6

% Multiplication
product = x * y; % product = 40

% Division
quotient = x / y; % quotient = 2.5

% Exponentiation
power = x ^ y; % power = 10000
% Equal to
isEqual = (x == y); % isEqual = false

% Not equal to
isNotEqual = (x ~= y); % isNotEqual = true

% Greater than
isGreater = (x > y); % isGreater = true

% Less than
isLess = (x < y); % isLess = false

% Greater than or equal to
isGreaterOrEqual = (x >= y); % isGreaterOrEqual = true

% Less than or equal to
isLessOrEqual = (x <= y); % isLessOrEqual = false
p = true;
q = false;

% AND
andResult = p & q; % andResult = false

% OR
orResult = p | q; % orResult = true

% NOT
notResult = ~p; % notResult = false
z = -3.7;

% Square root of the absolute value
sqrtResult = sqrt(abs(z)); % sqrtResult = 1.9235

% Round
roundResult = round(z); % roundResult = -4

% Max and Min
maxResult = max(z, 5); % maxResult = 5
minResult = min(z, 5); % minResult = -3.7

With these basics in hand, you are now ready to move on to more complex topics in MATLAB.

© Copyright 2024. All rights reserved