Docsity

Basic MATLAB - Experiment 1 - DC Circuit Analysis | ECE 103, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
ECE103. Spring 2012
Experiment #1
Student name(s) and lab section:
Page 1 of 8
01/16/12
Experiment #1
Basic MATLAB
®
(1)
Objective: You will be introduced to basic use of MATLAB
Introduction
We will introduce the very basic concepts using a powerful software package called
MATLAB
®
. This is a high performance language that integrates computation,
visualization and programming. MATLAB stands for matrix laboratory. The program
easily handles matrixes and vectors and has a powerful graphic package to display
magnitudes in 2 and 3 dimensions.
MATLAB language is specially oriented to calculations with vectors and matrixes. To
perform calculations with these elements is straightforward. It is especially useful for
algorithm development, data visualization, data analysis, and numeric computation.
Using MATLAB you can solve technical computing problems faster than with traditional
programming languages.
You can use MATLAB in applications like signal and image processing,
communications, control design, test and measurement, etc.. Add-on toolboxes
(collections of special-purpose MATLAB functions, available separately) are also
available to solve particular classes of problems.
Starting/Quitting
To start MATLAB, click on the ‘Start’ button on the left bottom of the screen, and then
click on ‘All Programs’, then ‘Math Software’, then ‘MATLAB’. A window will pop up
that will consist of three smaller windows. On the right there will be a big window
entitled ‘Command Window’. On the left there will be two windows, one entitled
‘Workspace’ and another one ‘Command History’.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Basic MATLAB - Experiment 1 - DC Circuit Analysis | ECE 103 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Experiment # Basic MATLAB®^ (1)

Objective: You will be introduced to basic use of MATLAB

Introduction We will introduce the very basic concepts using a powerful software package called MATLAB®. This is a high performance language that integrates computation, visualization and programming. MATLAB stands for matrix laboratory. The program easily handles matrixes and vectors and has a powerful graphic package to display magnitudes in 2 and 3 dimensions.

MATLAB language is specially oriented to calculations with vectors and matrixes. To perform calculations with these elements is straightforward. It is especially useful for algorithm development, data visualization, data analysis, and numeric computation. Using MATLAB you can solve technical computing problems faster than with traditional programming languages.

You can use MATLAB in applications like signal and image processing, communications, control design, test and measurement, etc.. Add-on toolboxes (collections of special-purpose MATLAB functions, available separately) are also available to solve particular classes of problems.

Starting/Quitting To start MATLAB, click on the ‘Start’ button on the left bottom of the screen, and then click on ‘All Programs’, then ‘Math Software’, then ‘MATLAB’. A window will pop up that will consist of three smaller windows. On the right there will be a big window entitled ‘Command Window’. On the left there will be two windows, one entitled ‘Workspace’ and another one ‘Command History’.

To use MATLAB, you will mostly be typing in the ‘Command window’ but you also create files in an editor and save them. Click on the Command window. Its outline will become dark (that’s how you know that you can type into that window). A cursor will start blinking on a line right after ‘>>’ (this is called a prompt). You can start typing your statements.

MATLAB as a calculator

MATLAB operations are designed to work with matrixes. Special cases are 1x1 matrixes (scalars) or matrixes with only one row or column (vectors). For example the instruction

5*

MATLAB will give us

ans =

15

The variable “ans” stores the result of the last calculation. It will be overwrite in the next calculation. Notice that there is a new entry in the top left window entitled ‘Workspace’. There is now an entry “ans” there of size 1x1. “ans” is a variable. This means that it’s a string of text that has a value (number) assigned to it. To see this, type

ans

at the prompt and then hit enter. As you can see MATLAB again returns ans = 15, i.e. it remembers that ans holds a value of 15. ans is a special name for a variable in MATLAB. It is assigned the value of the answer to the expression that you type at the prompt. In similar manner we can perform other mathematical calculations like addition, subtraction, division etc.

Creating own variables to store values in Matlab

You can create your own variables. For example, type in

x=

Now MATLAB has another entry in the ‘Workspace’ window called x. Now if you type x, MATLAB will know that its value is 10. For example, type

x+

MATLAB will give you the correct answer 15.

Cc=conj(c)

Matrixes To enter a matrix simply type in the command window the matrix elements separated by spaces, and each row separated by “;”. For example we can enter a row vector with the following command

a=[1 2 3 4 5 6 7 8 9]

and a column vector with

b=[1;2;3;4;5;6;7;8;9]

In the wokspace window it will be listed these two variables with the respective assigned values, a is a matrix with 1 row, and 9 columns; b is a matrix with 1 column and 9 rows. The operation ab is a matrix multiplication that will give a 1x1 (scalar) matrix. The operation ba instead will give as result a 9x9 square matrix. In the example S= ab gives a number and M=ba gives a 9x9 square matrix.

MATLAB is specially suited to operate with matrixes. For example we can create a square matrix composing 4 M matrixes as being blocks. The instruction MM=[M M;M M] generates a new 18x18 square matrix where each quadrant is the square matrix M. All operations in MATLAB are understood as matrix operations (for example * stands for matrix product, + is matrix addition etc.). However if we want to operate element to element, the instruction is “.*” of “.+”, this is the operator preceded by period. The instruction det(M) will produce the determinant of matrix M. For example

DM=det(M)

will store the value of the determinant of matrix M in the variable DM.

Matlab as a Mathematical tool MATLAB allows you to define (and evaluate) your own functions as well. For example, lets define a function f(x) = x^2 +1. To do this, simply type in

f = x.^2+

Suppose x has a value of 10, the answer is 101. You can now change the value of x. For example, type

x=

and enter, then type

f = x.^2+

again. You should now get 26.

However, you don’t want to type the expression for f(x) every time you want to change the value of x. You might want to define a function f(x) for a range of values of x. To do this in MATLAB we need to make x be a range of values. For example, suppose we want to make x to go from 1 to 10. To do this in MATLAB, you type

x = 1:

You will get

x = 1 2 3 4 5 6 7 8 9 10

as a result. Now, x is a list of values from 1 to 10. If we now type

f = x.^2+

we will get 10 values of f for each value of x. In other words, we have defined f as a function of x. Right now the difference between two consecutive values of x is 1. To change this, we put the step between two consecutive values between the maximum and minimum values. For example type

x=1:0.5:

You will get

x = Columns 1 through 8 1.0000 1.5000 2.0000 2.5000 3.0000 3. 4.0000 4.

Columns 9 through 16 5.0000 5.5000 6.0000 6.5000 7.0000 7. 8.0000 8.

Columns 17 through 19 9.0000 9.5000 10.

Now x still ranges from 1 to 10, but now it takes on 19 values with a step of 0.5. Since we redefined x, we now need to redefine f(x) as well. This means that we must again type f = x.^2+1. f(x) now has 19 values as well.

w = 3*x^2 -

and x ranges from 1:20. In Matlab we will write these as

Fone = x.^

Ftwo = 2.*x + 10

Fthree = 3.*x.^2 - 100

Use following command

plot( x,Fone,x,Ftwo,x,Fthree)

Solve the following exercises

1- Exercise (25 points) Find the Cartesian representation for the following complex numbers and calculate the modulus

  • 1 3 4 i Z e

π

  • 2 2 6 i Z e

π

(^3 ) 3 3

i Z e

π

  • Z 4 (^) = 2 ei^ π
  • Z 5 (^) = 2 e − i^ π

2- Exercise (25 points)

a) Consider the complex number Z = 2 + j 0.2. Plot in the complex plane Z, Z^2 , Z^3 and Z1/

3- Exercise (25 points) Find the highest and lowest temperatures in Fort Collins during last month (you can find this information for example in http://www.weather.com/), and plot them in a single graph.

4- Exercise (25 points) Enter the following matrixes:

1 0 8 1 2 3 7 9 7 7 9 8 4 5 6 6 5 8 6 5 4 7 8 8 7 6 5

A B C

Check the following rules of linear algebra: a- Is matrix addition commutative? Compute A+B and compare with B+A b- Is matrix addition associative? Compute (A+B)+C and compare with A+(B+C) c- Is multiplication with a scalar distributive? Compute 3(A+B) and compare with 3A+3B d- Is multiplication with a matrix distributive? Compute A(B+C) and compare with AB+AC