Matlab report HOR XIAN EN PDF

Title Matlab report HOR XIAN EN
Course Financial Markets and Investments
Institution Ekonomikas un kulturas augstskola
Pages 31
File Size 1.4 MB
File Type PDF
Total Downloads 21
Total Views 147

Summary

Kebanyakan negara di dunia yang mempunyai komposisi etnik yang
pelbagai sering kali berhadapan dengan situasi ketegangan etnik, isu kuasa
politik etnik, persaingan etnik, malah ada yang memuncak dengan konflik
fizikal yang lebih radikal sehingga menyebabkan kehilangan nyawa dan


Description

EEE228: SIGNAL & SYSTEMS LAB SESSION 01 INTRODUCTION TO MATLAB

NAME: HOR XIAN EN NO. MATRIX: 145792 ENGINEERING COURSE: ELECTRONIC ENGINEERING

INTRODUCTION: In this assignment, we are going to learn MATLAB. MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming environment. Furthermore, MATLAB is a modern programming language environment; it has sophisticated data structures, contains built-in editing and debugging tools, and supports object-oriented programming. These factors make MATLAB an excellent tool for teaching and research. MATLAB has many advantages compared to conventional computer language for solving technical problems. MATLAB is an interactive system whose basic data element is an array that does not require dimensioning. The software package has been commercially available since 1984 and is now considered as a standard tool at most universities and industries worldwide. It has powerful built-in routines that enable a very wide variety of computations. It also has easy to use graphics commands that make the visualization of results immediately available. Specific applications are collected in packages referred to as toolbox. There are toolboxes for signal processing, symbolic computation, control theory, simulation, power system engineering, optimization and several other fields of applied science and engineering. MATLAB is beneficial in performing numerical analysis. Numerical analysis relies on approximation rather than the precision we see in symbolic math. Performing certain building construction tasks is impossible without applying numerical analysis, and astronomy seems to require heavy use of it as well. We probably won’t see a carpenter applying numerical analysis, but we will see architects who might need to do so. MATLAB is likely to be used to explore new theories. When applied to science, MATLAB helps us perform “what if” analysis that helps us confirm the viability of a theory. Of course, science is used in many different ways. For example, a person might be involved in the health industry and using science to find a cure for cancer or the Ebola virus. A computer scientist might look for a new way to use computer technology to aid those with accessibility needs. Computer scientists rely heavily on math to perform tasks. MATLAB, with its rich toolbox, can be used to rapidly prototype an algorithm before committing the development resources to implementing the algorithm in another language, such as C++ or Java. Programmers commonly depend on MATLAB to enhance their productivity. When creating an application, they must ensure that the output is valid. However, verification is just one use. Computer scientists deal with a nearly inexhaustible supply of unknowns that could potentially benefit from the use of MATLAB.

1

OBJECTIVES: (a) To get acquainted with MATLAB’s environment (b) To use MATLAB as a calculator (c) To learn to create MATLAB variables and basic plots (d) To learn to initialize and manipulate vectors, matrices

PRE-LAB THEORY: What is MATLAB? MATLAB is a software program that allows you to do data manipulation and visualization, calculations, math and programming. It can be used to do very simple as well as very sophisticated tasks. It can solve large systems of equations efficiently and it is therefore useful for solving differential equations and optimization problems. It also provides excellent means for data visualization and has symbolic capabilities. Whilst simple problems can be solved interactively with MATLAB, its real power shows when given calculations that are cumbersome or extremely repetitive to do by hand! Where can we use MATLAB? MATLAB is recognized as an interactive program for numerical linear algebra and matrix computation. In industries, MATLAB is used for research and to solve practical engineering and mathematical problems. Also, in automatic control theory, statistics and digital signal processing (Time-Series Analysis) one can use MATLAB. The following tool boxes make it useful in soft computing at various industrial and scientific areas: (i) Neural Networks

(ii) Optimization

(iii) Genetic Algorithms

(iv) Wavelets

(v) Fuzzy Logic

(vi) Control systems

(vi) Signal processing

(vii) Communication Systems

Starting MATLAB After downloading the MATLAB software, you can enter MATLAB by double-clicking on the MATLAB shortcut icon on the Window. After starting MATLAB, the MATLAB Desktop will appear. The default threepart window looks similar to the figure below. The Desktop is also known as an Integrated Development Environment (IDE). Clicking the Start button in the lower left-hand corner will display a list of MATLAB tools, shortcuts, and documentation.

2

Figure 1.1 GUI view of MATLAB

Using MATLAB as a Calculator: As an example of a simple interactive calculation, just type the expression you want to evaluate. Let’s start at the very beginning. For example, in order to calculate the following expression 1+2*3, one needs to type it at the prompt command (>>) as follows: >> 1+2*3 ans= 7 Lab Task: Try the following and record your observations: >>3+7.5 >>18/4 >>3*7 >>3^2

3

Observations: MATLAB is easy to be used in calculation for some mathematical equations which involving addition, subtraction, multiplication and power of base number. If a wrong command is given such as using wrong symbols, we couldn’t get right answers and we need to clear the command window by entering ‘clc’ to the command to clear screen.

Mathematical Functions: The following table lists some commonly used standard MATLAB functions: Function ex

MATLAB Syntax exp(x) sqrt (x)

√x

Ln x Log10 (x) Cos x Sin x Tan x Cos-1 x Sin-1 x Tan-1 x

log(x) log10(x) cos(x) sin(x) tan(x) acos(x) asin(x) atan(x) Table 1.1 Standard MATLAB functions

Example 1: y= e-a sin(x) + 10

√y

for a=5, x=2, and y=8 is computed by

4

>>a=5; x=2; y=8; >>y=exp (-a)*sin(x) +10*sqrt(y) y= 28.2904

Example 2: To calculate sin (pi/4) and e 10 >>sin(pi/4) ans= 0.7071

>>exp(10) ans=2.2026e+004

Variables, Expressions & Statements: Variable names must be a single word containing no space and up to 31 characters. Variables names are case sensitive and a variable name must start with a letter. Punctuation characters are not allowed. MATLAB’s statements are of the form: >> variables = expression or >> expression

5

Expressions are composed of operators, function and variable names. After evaluation the value is assigned to the variable and displayed. If the variable name and = sign are omitted, a variable ans (for answer) is automatically created and the result is assigned to it. Rules of Precedence: Expressions are evaluated from left to right with exponential operation having the highest precedence, followed by multiplication and division having equal precedence, followed by addition and subtracting having equal precedence. Parentheses can be used to alter this ordering in which case these rules of precedence are applied within each set of parentheses starting with the innermost set and proceeding outward. The most recent values assigned to the variables you used in the current session are available. For example, if you type a at the prompt you get the output as: >> a a=7 The display of numerical values can have different format as we see below: >> e= 1/3 e = 0.3333 >> format long (long decimal format) >> e e = 0.33333333333333 >> format short e (short exponential format) >> e e = 3.3333 e-01 >>format long e (long exponential format) e = 3.33333333333333 e-04 >>format ( default format) >>e e = 0.333

6

Entering Matrices: You can enter matrices in various different ways: • Enter an explicit list of elements. • Load matrices from external data files. • Generate matrices using built-in functions. • Create matrices with your own functions and save them in files MATLAB works with essentially only one kind of objects, i.e. a rectangular numerical matrix with possibly complex entries. All variables represent matrices. If you want to store a matrix 1 2 3 in a variable a in the MATLAB’s current memory, please type the following:

7

456 789 >> a = [1 2 3; 4 5 6; 7 8 9] a= 123 456 789

The rows are separated by semicolons and elements are separated by space or by comma. That is, the above matrix can also be stored by the following command. >> a = [1,2,3;4,5,6;7,8,9]

or by the statement >> a = [ 123 456 789]

8

The semicolon at the end of a command suppresses the output. The matrix a is transposed and is stored in b by the following command >> b = a’ b= 147 258 369

The following matrix operations are available in MATLAB: Function + * ^ ‘ \ /

Task Addition Subtraction Multiplication Power Transpose Left Division Right Division Table 1.2 Matrix Operations

Matrix Initialisation and Addressing:

9

>>A=[1 2 3 4;5 6 7 8] A= 1234 5678 >>A( : , : ) Ans= 1234 5678 >>A(1,2) Ans= 2 >>A(1, :) Ans= 1 2 3 4 >>A(: ,[1,3]) Ans= 13 57 >>A(: ,end) Ans= 4 8 Selected portion of one matrix can be assigned to a new matrix >>B= A(: , 3 :end) B= 34 78

To select the elements along main diagonal,

10

>>diag(B) Ans= 3 8

11

Changing and deleting matrix elements: >>A=1:5 A= 1 2 3 4 5 >>A(2) = 6 A= 1 6 3 4 5 >>A([1 3]) = 0 A= 0 6 0 4 5

12

Manipulating Matrix: >>A=[1 3 4; 5 7 8] A= 134 578 For transpose, >>A’ Ans= 15 37 48 >>fliplr (A) Ans= 431 875 >>flipud(A) Ans= 578 134

13

Matrix Operation: >>A=[1 3 4 ; 5 7 8] A= 134 578 >>2+A Ans= 356 7 9 10

Element-by-element operations vs. Matrix Operations: Matrix multiplication has a special procedure, and it is different from simple element-to-element multiplication. This is unlike addition operation, where two matrices cab be added by simple element-byelement addition, this is adding corresponding elements of the two matrices in question. Thus, we have a special set of operators that distinguish Matrix operations from element-by-element operations. When you intend to perform matrix operation, you simply use, * for multiplication, / for division, ^ for exponentiation and so on. But if you intend to perform element-by-element operations, you have to use, .* for multiplication, ./ for division and .^ for exponentiation. Example, >>A=[2 3 4;5 6 7;2 1 0]; >>B=[1 2 3;5 6 2;0 0 5]; >>A*B Ans=

14

17 22 32 35 46 62 7 10 8 >>A.*B Ans= 2 6 12 25 36 14 000

15

Basic Plotting: MATLAB has an excellent set of graphics tools. Plotting a given data set or results of computation is possible with a very few commands. Trying to understand mathematical functions with graphics is an enjoyable and very ancient way of learning mathematics. Being able to plot mathematical functions and data freely is the most important step, and this section is written to assist you to do just that. The MATLAB command used to plot the graph is “plot(x,y)”. For example, if the vector x= (1; 2; 3; 4; 5; 6) and y= (3; -1; 2; 4; 5; 1) >> x= [1 2 3 4 5 6]; >> y= [3 -1 2 4 5 1]; >>plot(x,y)

16

Now try the following: >>x = 0: pi/100: 2*pi; >>y= sin (x); >>plot (x,y)

17

Adding titles and axis labels: MATLAB enables you to add axis labels and titles. For example, using the graph from the previous example, add x- and y-axis labels. >> xlabel (‘x = 0:2\pi’); >>ylabel (‘sine of x’); >>title (‘Plot of the Sine Function’)

Multiple data sets in one plot: Multiple (x;y) pairs arguments create multiple graphs with a single call to plot. For example, these statements plot three plots related functions of x: y1=2 cos(x), y2=cos(x), and y3=0.5*cos (x) in the interval 0 ≤ x ≤ 2π >> x= 0:pi/100:2*pi; >>y1= 2*cos(x); >>y2= cos (x); >>y3= 0.5*cos (x); >>plot (x, y1, ‘- -‘,x, y2, ‘- - ‘, x, y3, ‘:’) >>xlabel (‘0 \leq x \leq 2\pi’) >>ylabel (‘Cosine functions’) >>legend (‘2*cos(x)’, ‘cos(x)’, ‘0.5*cos(x)’) >>axis ([0 2*pi -3 3])

18

MATLAB Programming: To make a file in MATLAB, click on “New” in the file menu in menu bar. It will open a new file. After writing the code, if we want to save this file (called m-file), it will be saved in “bin” folder of MATLAB by default. Such files are called “M-files” because they have an extension of “.m” in its filename. There are two types of M-files: Script Files and Function Files. % This is a sample m-file a = [1,2,3;0,1,1;1,2,3] b =a’ c = a+b d = inv(c) Save this file as “rkg.m” Then in order to execute this file, type >> rkg

19

Write the output: The outputs shown what is written in file. Matric a is [1,2,3;0,1,1;1,2,3] The rows are separated by semicolons and elements are separated by comma. Matric b is the transpose of matric a while matric c is the addition of matric a and matric b. Matric d is the 3*3 inverse matric of c. The % symbol indicates that the rest of the line is a comment. MATLAB will ignore the rest of the line. However, the first comment lines which document the m-file are available to the online help facility and will be displayed. An M-file can also reference other M-files, including referencing itself recursively. Function Files: Function files provide extensibility to MATLAB. You can create new functions specific to your problem which will then have the same status as other MATLAB functions. Variables in a function file are by default local. However, you can declare a variable to be global if you wish. Example: Function y = prod (a,b) y = a*b; Save this file with filename prod.m then type on the MATLAB prompt >> prod (3,4)

20

ans = 12 In MATLAB to get input from user the syntax rule is: >>variable name = input (‘variable’); Example: >>a=input (‘ Enter a number ‘); And on command prompt if we run this program it will be displayed like: >> Enter a number If we type 2 at the cursor then we get the output as a=2 Control Flow: MATLAB offers the following decision making or control flow structures: 1. If- Else –End Construction 2. For loops 3. While loops 4. Switch-Case constructions etc. Script Files: A script file consists of a sequence of normal MATLAB statements. If the file has the filename, say, rkg.m, then the MATLAB command >> rkg will cause the statements in the file to be executed. Variables in a script file are global and will change the value of variables of the same name in the environment of the current MATLAB session. Script files are often used to enter data into a large matrix; in such a file, entry errors can be easily edited out. The load Function: The load function reads binary files containing matrices generated by earlier MATLAB sessions, or reads text files containing numeric data. The text file should be organized as a rectangular table of numbers, separated by blanks, with one row per line, and an equal number of elements in each row. For example, outside of MATLAB, create a text file containing these four lines: 16.0 3.0 2.0 13.0 5.0 10.0 11.0 8.0 9.0 6.0 7.0 12.0 4.0 15.0 14.0 1.0

21

Save the file as magik.dat in the current directory. The statement load magik.dat reads the file and creates a variable, magik, containing the example matrix. An easy way to read data into MATLAB from many text or binary formats is to use the Import Wizard.

22

EXERCISE: 1. Given that x=-5+9i and y=6-2i, use MATLAB to show that x+y=1+7i, xy= -12+64i and x/y= -1.2 + 1.1i

2. Use MATLAB to compute: 6(351.4 )+140.35

23

3. Use MATLAB to plot the functions y=4√(6x+1) and z=5e0.3x – 2x over the interval 0...


Similar Free PDFs