• No results found

Introduction to MATLAB.pptx

N/A
N/A
Protected

Academic year: 2020

Share "Introduction to MATLAB.pptx"

Copied!
25
0
0

Loading.... (view fulltext now)

Full text

(1)

MATLAB

MATRIX stands for MATrix LABoratory

The aim of education is to teach students how

(2)

Introduction

MATLAB is a mathematical and graphical

software package; it has numerical, graphical, and programming capabilities. It has built-in functions to do many operations, and there are toolboxes that can be added to augment these functions (e.g., for signal processing). There are versions available for different

(3)

There are several commands that can serve as an introduction to MATLAB and allow you to get help: • info will display contact information for the

product;

demo has demos of several options in MATLAB;help will explain any command;

(4)

Starting MATLAB

You can enter MATLAB by double-clicking on the MATLAB shortcut icon on your Windows desktop. When you start

MATLAB, a special window called the MATLAB desktop appears. The desktop is a window that contains other windows. The

major tools within or accessible from the desktop are: • The Command Window

The Command History The Workspace

The Current Directory The Help Browser

(5)

Quitting MATLAB

To end your MATLAB session, type quit at the

prompt in the Command Window, or select File Exit MATLAB in the desktop main

(6)

Uses of MATLAB

MATLAB is widely used as a computational tool in science

and engineering encompassing the fields of physics,

chemistry, math and all engineering streams. It is used in a range of applications including:

signal processing and communications image and video processing

control systems

(7)

Creating variables in MATLAB

• The name must begin with a letter of the alphabet. After that, the name can contain letters, digits, and the underscore character (e.g., value_1), but it cannot have a space.

• There is a limit to the length of the name; the built-in function

namelengthmax tells how many characters this is.

• MATLAB is case-sensitive. That means that there is a difference between upper- and lowercase letters. So, variables called mynum, MYNUM, and

Mynum are all different.

• There are certain words called reserved words that cannot be used as variable names.

(8)

MATLAB is case-sensitive. That means that

there is a difference between upper- and

lowercase letters. So, variables called mynum, MYNUM, and Mynum are all different.

There are also certain words called reserved

(9)

Reserved or keywords of MATLAB

The reserved words, called keywords, are:

(10)

Additionally, variable names should always be

mnemonic, which means they should make some sense. For example, if the variable is storing the radius of a circle, a name such as “radius” would make sense as against a

(11)

Variables and assignment statements

One easy way to create a variable is to use an assignment statement. The format of an assignment statement is

variablename = expression

The variable is always on the left, followed by the assignment

operator, = (unlike in mathematics, the single equal sign does not

mean equality), followed by an expression. The expression is evaluated and then that value is stored in the variable.

For example, this is the way it would appear in the Command Window:>> mynum = 6

mynum =6

(12)

Since the equal sign is the assignment operator, and does not mean equality, the statement should be read as “mynum gets the value of 6” (not “mynum equals 6”).

Note that the variable name must always be on the left, and the expression on the right. An error will occur if these are reversed. • >> 6 = mynum

??? 6 = mynum |

Error: The expression to the left of the equals sign is nota valid target for an assignment.

(13)

Assignment

In MATLAB, variables must always have values

before they are used.

To create a variable in MATLAB, you declare it and

assign an initial value to it. This initial value could be 0, 1, 1000, or whatever value it is supposed to be.

Examples:>> x = 2;

(14)

Examples of expressions

rho = (1+sqrt(5))/2rho = 1.6180

(15)

Commands that relate to variables

The following commands relate to variables:

who shows variables that have been defined in this Command

Window

(this just shows the names of the variables)

whos shows variables that have been defined in this

Command Window (this shows more information on the variables, similar to what is in the Workspace Window)

clear clears out all variables so they no longer existclear variablename clears out a particular variable

If nothing appears when who or whos is entered, that means

(16)

Operators

Operators

• There are in general two kinds of operators: unary operators, which

operate on a single value or operand; and binary operators, which operate on two values or operands. The symbol “–”, for example, is both the unary operator for negation and the binary operator for subtraction.

• Here are some of the common operators that can be used with numeric expressions:

• + addition

• – negation, subtraction

* multiplication

/ division (divided by e.g. 10/5 is 2) • \ division (divided into e.g. 5\10 is 2)

(17)

Initializing, Incrementing, and Decrementing

Frequently, values of variables change. Putting the

first or initial value in a variable is called initializing

the variable.

Adding to a variable is called incrementing. For

example, the statement

mynum = mynum + 1

increments the variable mynum by 1. Likewise,

(18)

Operator precedence

The order from highest to lowest are:() parentheses

^ exponentiation– negation

(19)

MATLAB special variables

pi Value of π

eps Smallest incremental numberinf Infinity

NaN Not a number e.g. 0/0i and j, i = j = square root of -1

(20)

MATLAB relational operators

MATLAB supports six relational operators.Less Than <

Less Than or Equal to <=Greater Than >

Greater Than or Equal to >=Equal To ==

Not Equal To ~= (NOT; equivalent to != like in

(21)

MATLAB logical operators

MATLAB supports three logical operators.not ~ % highest precedence

(22)

When assignments are long, we can extend to

another line by using an ellipsis (…). For example,

initial_velocity = 0; acceleration = 9.8; time = 20;

(23)

Complex Numbers

MATLAB can work with complex numbers as easily as with real numbers. For example, to find the roots of the quadratic

polynomial x2 + 2x + 5 enter

>> a = 1; b = 2; c = 5;

>> x1 = ( -b + sqrt( b^2 - 4*a*c ) ) / (2*a)>> x2 = ( -b - sqrt( b^2 - 4*a*c ) ) / (2*a)The output is

-1.0000 + 2.0000iand

(24)

There are standard functions for obtaining the real

part, the imaginary part, and the complex

conjugate of a complex number or variable. For example,

>> x = 3 - 5i>> real(x)>> imag(x)>> conj(x)

(25)

1. Consider a triangle with sides a, b, and c and corresponding

angles ab, ac, and bc.

(a) Use the law of cosines, i.e.,c2 = a2 + b2 - 2ab cos ab;

to calculate c if a = 3.7, b = 5.7, and ab = 79⁰.(b) Then show c to its full accuracy.

(c) Use the law of sines, i.e.,

= ,

References

Related documents

This pocket reference summarizes Python types and state- ments, special method names, built-in functions and excep- tions, commonly used standard library modules, and

Level of control Minimal Some Extensive Required effort Built-in functions Functions on array data Directly invoke CUDA code.. 94 Directly invoke CUDA code

• MATLAB contains specific functions for reading and writing Microsoft Excel files. • To write a matrix to an Excel file, use

We can avoid the use of the chain rule in taking derivatives of certain functions by summarizing chain rule results... Generalizing the last example where, below, , refers to

M-files can be scripts that simply execute a series of MATLAB statements, or they can be functions that also accept input arguments and produce output.

4) Run the script to evaluate and plot the normal stress distribution in the region immediately around the right-hand model fault tip (this is the so-called ‘near-tip’ field).

That is, Unix has a special variable called PATH that contains a collection of directory names to search through for commands that are typed. To see what directories Unix is

For example, when certain words (such as proper and place names, or the so-called named entities) have small k and p values, then σ 2 is large, and µ is small: these