Introduction to MATLAB and learn about Basic Arithmetic and Matrices.

 

Lab Report #01

Objective:

Introduction to MATLAB and learn about Basic Arithmetic and Matrices.



MATLAB:

MATLAB is a programming platform designed specifically for engineers and scientists to analyze and design systems and products that transform our world.

MATLAB is started just like any other Windows program. Just go to your program files menu and search for the MATLAB folder. When you click on it, you will see several options depending on your installation, but you will have at least the following three options

Ø  MATLAB (version number)

Ø  M-file editor

Ø  Uninstaller

To start the program, you select MATLAB. The default MATLAB desktop will then open on your screen (see Figure 1). As shown in the figure, the screen is divided into three main elements. These are

Ø  File listing in the current directory

Ø  Command History Window

Ø  Command Window



 

For Command entry we use:

Ø  Command window (also show result).

Ø  M- file editor (only code writing)

Common Window and Basic Arithmetic:

The Command Window is found on the right-hand side of the MATLAB desktop. Commands are entered at the prompt with looks like two successive “greater than” signs: ‘>>’.

Let’s start by entering a few basic commands. If you want to find the value of a numerical expression, simply type it in. Let’s say we want to know the value of 433.12 multiplied by 15.7. We type 433.12 * 15.7 at the MATLAB prompt and hit the enter key. The result looks like this:

A screenshot of a computer

Description automatically generated

Basic coding:

Once a variable has been entered into the system, we can refer to it later. Suppose that we want to compute a new quantity that we’ll call y, which is equal to x multiplied by 3.56 where x=30. Then

 

 

 

Basic Arithmetic:

Let’s summarize basic arithmetical input in MATLAB.

  1. To write the multiplication ab, in MATLAB, we type

a * b

  1. For division, the quantity a divided by b is typed as

a / b

  1. Exponentiation ab is entered in the following way

a ^b

  1. Finally, addition and subtraction are entered in the usual way

a +b

a -b

Example 01:

Use MATLAB to evaluate:

and

MATLAB has been spitting out numbers with four decimal places. This is known as short format in MATLAB. It’s the default in MATLAB and if that’s all the precision you require, then you don’t have to do anything. If you want more, then you can tell MATLAB to add more digits to the right of the decimal point by using the format command. If we want 16 digits instead of 4, we type format long. To see how this works, look at the following calculation, displayed in both formats:

For example:

MATLAB displays large numbers using exponential notation. That is, it represents    5.4387 × 103 as 5.4387e + 003. If you want all numbers to be represented in this fashion, you can do so. This type of notation can also be defined using the short or long formats. For short (four decimal places plus the exponent) you can type format short e. To allow 15 decimal digits plus the exponent, type format long e. Here is an example of the short exponent format

For example:

If you type format rat, then MATLAB will find the closest rational expression it can that corresponds to the result of a calculation. Let’s repeat the previous calculation

Basic Notes:

  1. To open pervious code (press straight arrow) .
  2. If we don’t want to show answer in command window write ( ; ) next to equation.
  3. If we write ‘who’ it will show all variables in workspace.
  4. If we write ‘who’s’ it will show all variables including ans and also showing how its matrix will be formed.

Basic Mathematical Definitions:

MATLAB comes with many basic or familiar mathematical quantities and functions built in. Let’s show how to use π in an example.

Example 02:

Find the volume of a sphere of radius 2 m using MATLAB.

Solution:

The volume of a sphere is given by

Another famous number that shows up in many mathematical applications is the exponential function. That is, e ≈ 2.718. We can reference e in MATLAB by typing exp(a) which gives us the value of e.

For example:

Calculate e1 and e2 using MATLAB.

To find the square root of a number n, we type sqrt(n).

For example:

Calculate square root of 9 and 11 using MATLAB.

A screenshot of a computer

Description automatically generated

To find the natural log of a number n, type log(n).

For example:

Find natural log of 3.2 and 5 using MATLAB.

A screenshot of a computer

Description automatically generated

If you want the base ten logarithm, type log10(x)

For example:

Calculate log base 10 of 3 using MATLAB.

A screenshot of a computer

Description automatically generated

MATLAB comes equipped with the basic trigonometric functions and their inverses, taking radian argument by default. These are typed in lower case using the standard notation

For example:

Calculate using MATLAB.

A screenshot of a computer

Description automatically generated

To use an inverse of a trigonometric function, add on an ‘a’ before the name of the trigonometric function.

For example:

Compute the inverse tangent of a number .

A screenshot of a computer

Description automatically generated

Complex Numbers:

A complex number is one that can be written in the form z = x +iy, where x is the real part of z and y is the imaginary part of z. It is easy to enter complex numbers in MATLAB, by default it recognizes as the square root of minus one.

For example:

Add two complex numbers and .

A screenshot of a computer

Description automatically generated

Matrices:

A matrix is a two-dimensional array of numbers. To create a matrix in MATLAB, we enter each row as a sequence of comma or space delimited numbers, and then use semicolons to mark the end of each row.

 

For example:

Write a matrix .

A screenshot of a computer

Description automatically generated

Dimensions of matrix:

            To show dimensions of matrix ‘x’ in MATLAB, use command size(x).

For example:

Show dimensions of matrix .

A screenshot of a computer

Description automatically generated

Transpose of matrix:

            To take transpose of matrix ‘z’ using MATLAB, use command z’.

For example:

Take transpose of matrix .

A screenshot of a computer

Description automatically generated

Post a Comment

0 Comments