Sunteți pe pagina 1din 6

Electrmagnetics Fields and Waves

Spring 2004
Lab 1

Ahmed Junaid Omer Department of Electronics Air University

1.

What is MATLAB

MATLAB is a product of the MathWorks, Inc. and is an advanced interactive software package specially designed for scientific and engineering computations. The MATLAB environment integrates graphics illustrations with precise numerical calculations, and is a powerful, easy-to-use, and comprehensive tool for performing all kinds of computations and scientific data visualizations. MATLAB has proven to be a very flexible and usable tool or solving problems in applied mathematics, physics, chemistry, engineering, medicine, finance, and other application areas which deals with complicated numerical calculations. MATLAB is also an excellent pedagogical tool when teaching engineering, mathematics and numerical analysis. The program is widely used at universities and colleges all around the world, and it is popular for industrial use as well. We will be using MATLAB to understand and visualize the physical processes in electromagnetics. The topics covered will be scalar and vector algebra, divergence, Curl and electromagnetics field propagation in different media.

2.

Getting started

Here is a sample session with MATLAB. Text in bold is what you type, ordinary text is what the computer "types." You should read this example, then imitate it at the computer.
>> a = [ 1 2; 2 1 ] a = 1 2 >> a*a ans = 5 4 >> quit 4 5 2 1

In this example you defined matrix a and computed its square ("a times a"). Finally (having done enough work for one day) you quit MATLAB.

Matlab Tutorial Matrices


To enter the matrix
1 2 3 4

and store it in a variable a, do this:


>> a = [ 1 2; 3 4 ]

Do this now: define the matrix a. Do the same with the examples below: work out each of them with MATLAB . Learn by doing! To redisplay the matrix, just type its name:
>> a

Once you know how to enter and display matrices, it is easy to compute with them. First we will square the matrix a :
>> a * a

Wasn't that easy? Now we'll try something a little harder. First we define a matrix b:
>> b = [ 1 2; 0 1 ]

Then we compute the product ab:


>> a*b

Finally, we compute the product in the other order:


>> b*a

Notice that the two products are different: matrix multiplication is noncommmutative. Of course, we can also add matrices:
>> a + b

Now let's store the result of this addition so that we can use it later:
>> s = a + b

Matrices can sometimes be inverted:


>> inv(s)

To check that this is correct, we compute the product of s and its inverse:
>> s * inv(s)

The result is the unit, or identity matrix. We can also write the computation as
>> s/s

We can also write


>> >> s\s inv(s) * s

which is the same as

To see that these operations, left and right division, are really different, we do the following:
>> a/b >> a\b

Not all matrices can be inverted, or used as the denominator in matrix division:
>> c = [ 1 1; 1 1 ] >> inv(c);

A matrix can be inverted if and only if its determinant is nonzero:


>> det(a) >> det(c)

Loops
MATLAB has two commands, for and while, for repeated execution of statements. These add the flexibility to execute statements once or several times under logical control. Let us generate a table of function y=1+1/x evaluated in the interval [-2,-0.75] in steps of 0.25. Store the x and y values in the vectors r and s, respectively, and display the results.
r = []; s=[]; for x = -2.0:0.25:-0.75 y=1+1/x; r=[r x]; s=[s y]; end [r; s]'

The semicolon ; is vital here. If we write these statements without semicolon then what happens ? Try it ? Remark: you have just learned to write a kind of loop, a so-called for loop. This is an easy way to command the machine, in just a few words, to do much repetitive work.

Graphing Functions of one variable


To make a graph of y = sin(t) on the interval t = 0 to t = 10 we do the following:
>> t = 0:.3:10; >> y = sin(t); >> plot(t,y)

Here is how we graph the fuction z(x,y) = x exp( - x^2 - y^2):


>> [x,y] = meshdom(-2:.2:2, -2:.2:2); >> z = x .* exp(-x.^2 - y.^2); >> mesh(z)

3.

MATLAB Command files

Instead of writing statements of MATLAB commands at the MATLAB prompter you can write them in a text file, created with an editor. These commands are executed by MATLAB when the user writes the file name and its arguments, if any. MATLAB reads the commands from the file instead of the terminal. When the last command in the file is executed, MATLAB can read commands from the terminal again. An M-file is a file of the kind: filename.m It must have he suffix .m. An M-file contains a number of consecutive MATLAB commands and it may refer to other M-files It may even be recursive, that is to say, refer to itself. Try this
function p = factorial (nn) % Computes the factorial of nn if nn == 0

p = 1; else p = nn*factorial(nn-1); end The M-file is named factorial.m and a call to the function is made by factorial(4)

4.

Some Interesting links

1.http://www.math.ufl.edu/help/matlab-tutorial/index.html#SEC5 (University of Florida, Math department) 2.http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/learn_matl ab.shtml. (From the makers of MATLAB)

S-ar putea să vă placă și