# Looking At Matlab For Electromagnetics Biology Essay

Published: Last Edited:

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

MATLAB is a software program that is widely available for digital computers at a large number of universities and on a large variety of machines. As will be noted in this text, we will make extensive use of it. The two and three dimensional plotting capabilities will be exploited since a picture or a graph can usually aid in the physical interpretation of an equation. Herein, we will briefly present an introduction of several germane features of this program that will be useful for electromagnetic theory. Various functions such as trig functions appear in a MATLAB library that can be easily called and used. The user can customize and add to this list by writing a program in a ".m" (dot m) file. Several examples and MATLAB programs will be included throughout this text. Additional programs are included in Appendix E. Matrix operations will not be examined since their application will receive minimal attention in this text We assume that the reader is able to call MATLAB and have the familiar MATLAB prompt ">>" appear on the screen. Typing the words "help topic" after the prompt brings on-screen help to the user.

As an example, we type

>> x = 3

x =

3 (1.80)

## >>

The computer has assigned a value for the variable x that it will remember until it is changed or until we exit the program. It is ready for the next input. Let us type y = 4 and press the return key. MATLAB returns

>> y = 4

y =

4 (1.81)

## >>

Mathematical operations of these two numbers follow and we write a mathematical operation at the prompt. In the table given below, the following three lines will appear after we push the return key.

Subtraction

Multiplication

Division

>> z = x + y

>> z = x - y

>> z = x * y

>> z = x / y

z =

z =

z =

z =

7

- 1

12

0.7500

## >>

Note the four place accuracy in the last column. This can be changed by the user.

As written, these operations appear to "waste" a considerable amount of computer screen since they immediately cause the computer to react. We can conserve the screen by typing the semicolon ";" after each operation. For example, typing the following sequence after the prompt

>> x = 3; y = 4; z = x + y

z =

7 (1.82)

## >>

combines several lines. The semicolon will be very useful in a lengthy calculation if we do not wish to display intermediate results. Another useful tool to remember is the symbol "%" since anything typed on the line after it will receive no attention by the computer. It is a convenient way to add comments to a program or to an operation.

A vector can be specified in MATLAB by stating its three components. We will use a capital letter to identify a vector in using MATLAB. Lower case letters will be reserved for scalar variables or constants. This is not required but it does add clarity to the work. For example, a vector A = 1ux + 2uy + 3uz is written as

>> A = [1 2 3]

A =

1 2 3 (1.83)

## >>

We must insert a space between the components of the vectors that are numbers. A second vector B = 2ux + 3uy + 4uz is written

>> B

= [2 3 4]

B =

2 3 4 (1.84)

## >>

Having stored the two vectors A and B in the computer, we can perform the following mathematical operations. The vectors can be added C = A + B by typing

>> C = A + B

C =

3 5 7 (1.85)

## >>

The vector is interpreted as C = 3 ux + 5 uy + 7 uz.

The two vectors can also be subtracted D = A - B

>> D = A - B

D =

-1 -1 -1 (1.86)

## >>

The vector is written as D = -1 ux + -1 uy + -1 uz

It is also possible to perform the scalar and the vector products of the two vectors. MATLAB denotes these products with the terms dot and cross respectively. The scalar product c = A â€¢ B is given by

>> c = dot(A,B)

c =

20 (1.87)

## >>

We, of course, will obtain the same result if we had typed

>> c = dot(B,A)

c =

20 (1.88)

## >>

The vector product C = A x B is given by

>> C = cross(A,B)

C =

-1 2 -1 (1.89)

## >>

We write the vector C = - 1ux + 2 uy - 1 uz. For these two vectors, we find that

D = B x A = - C as will be observed after typing

>> D = cross(B,A)

D =

1 -2 1 (1.90)

## >>

which we write as D = + 1ux - 2 uy + 1 uz. In addition to these mathematical operations, MATLAB provides extensive two and three dimensional graphical plotting routines. The data to be plotted can be generated internally in a program or it can be imported from an external program. The command "fplot" specifies and plots a known function. Labels and titles using different fonts and font sizes and styles can be placed on the graphs and the plots can be distinguished with different symbols. We will present several examples here in order to illustrate the variety of two dimensional plots that are available. Additional graphs can be placed on one plot with the "hold" command. Either axis can have a logarithmic scale. In addition, the command "subplot" permits us to place more than one graph on a page, either vertically or horizontally displaced. The command "subplot (1, 2, 1)" states that there are to be two graphs next to each other and this command will be used to select the left one. Other commands that follow detail the characteristics of that particular graph. The command "subplot (2, 1, 1)" states that there are also to be two graphs, one above the other, and this command will be used to select the top one.

Let us construct a series of graphs.

a) Plot five numbers - bar graph

>> subplot(2,2,1);

>> a = [2 4 6 8 10]

>> bar(a)

>> xlabel('number') (1.91)

>> ylabel('a')

>> title('(a) Bar graph')

## >>

b) Plot one set of numbers versus another set of numbers - symbol

>> subplot(2,2,2);

>> a = [2 4 6 8 10];

>> b = [5 4 3 2 1];

>> plot(a,b,'*');

>> xlabel('a'); (1.92)

>> ylabel('b');

>> title('(b) Plot of two numbers');

>> grid

## >>

c) Plot a known function between prescribed limits

>> subplot(2,2,3);

>> fplot('sin',[0 4*pi,]);

>> xlabel('theta');

>> ylabel('Y'); (1.93)

>> title('(c) A sine wave')

## >>

d) Plot a known function between prescribed limits - increments

>> subplot(2,2,4);

>>x=[0:0.5:3];

>>y=exp(x);

>>plot(x,y,'o');

>> xlabel('X');

>> ylabel('Y'); (1.94)

>> title('(d) Exponential')

>>grid

## >>

The graphs, shown in Figure 1-24 have labels on their axes and a title.

Figure 1-24. Two dimensional graphs. (a) Bar graph. (b) Plot of a versus b. (c) Functional plot of a sine wave. (d) Plot of an exponential at discrete points.

We can customize a graph by changing the characteristics of the line. This is done with an operation called the "handle." For example, let us (a) first plot two cycles of a sine wave using 100 points. We have broken the line between 0 and 4Ï€ into 100 points. These values, as computed in (1.95), are stored in the computer. We will then modify the graph: (b) by changing the line thickness, and (c) by changing the line style.

In the above discussion, we have been forced to individually write a short program using the MATLAB language. Each program was individually written and debugged to our satisfaction. MATLAB contains an extensive library of programs that are built into it. For example, in (1.93), we wrote

"y = sin (t);"

respectively. The "sin" function is incorporated into this library.

The question then arises, "Can a program written by us which we expect to use again also be included in the library?" The answer is "Yes" and the process is given the name of creating a ".m" (dot m) file with its unique name, say "custom.m" The creation of the "custom.m" file involves a text editor, the form of which depends upon the local computer or work station to which the user has access. Once the file is created, it becomes a part of our library. In order to use this file, all we have to do is type the word "custom" after the prompt ">>" and the file is activated at that point. These files are frequently shared over the internet.

Let us also create a "dot m" file and call it "si.m". Hence, whenever we type "si" after the MATLAB prompt >>, these three graphs will appear. Rather than insert a title in the graphs, we insert the letters (a), (b) and (c). This is accomplished with the command "s='(a)'." The command "text" locates the letter on the graph. the command "s(2)" sequences the letter. Variations of this labeling are included in Appendix E. The MATLAB program "si.m" is

clear

clg

s='(a)'; (1.95)

t=linspace(0,4*pi,100);

y=sin(t);

subplot(1,3,1)

hL=line(t,y);

text(15,1,s) (1.96)

s(2)=setstr(s(2)+1);

subplot(1,3,2)

hL=line(t,y);

set(hL,'linewidth',3); (1.97)

text(15,1,s)

s(2)=setstr(s(2)+1);

subplot(1,3,3)

hL=line(t,y);

set(hL,'linestyle','--') (1.98)

text(15,1,s)

s(2)=setstr(s(2)+1);

The results are shown in Figure 1-25.

Figure 1-25. Two dimensional graphs. (a) Sine wave. (b) Change line thickness. (c) Change linestyle. The axes can be labeled as in Figure 1-24.

In addition to plotting the graphs in Cartesian coordinates, we can also plot them in polar coordinates. Since we have the numbers from the previous figure already stored in the computer, we just will have to create the plot. A word of caution must, however, be invoked. If we were to write the MATLAB command "polar (t, y)", only one half of the figure would appear. This is because y = sin(x) is negative in the range Ï€ < x < 2Ï€. This dilemma can be removed by finding the absolute value of y. The style of the line can also be modified. The results are shown in Figureb1-26. In Chapter 7, an additional technique to create a polar graph will be suggested.

Polar plot

>> x = abs (y);

>> subplot (1,2,1)

>> polar (t, x) (1.99)

>> text(-.5,-1.4,'(a) polar plot')

## >>

Change linestyle

>> subplot (1,2,2)

>> polar (t, x, '--') (1.100)

>> tex(-.9,-1.4,'(b) change linestyle')

## >>

Figure 1-26. Examples of a polar plot. (a) Normal linestyle. (b) Dashed line.

We can also customize a graph by adding text to it, changing fonts, or changing the size of the type. This is also done with an operation called the "handle." For example, let us (a) first plot a Gaussian signal y = exp [-q2]. In MATLAB, we write this as y = exp (-t.^2). The period that separates the t and the ^ is crucial. It causes each value of t to be separately squared. We will obtain a sequence of values. If we divide one function by another, a period must follow the term in the numerator. (b) We will add text to the graph and change the size of the font in the labels on the axes. One of the labels will use the symbol font. The MATLAB program is given below.

>> t = linspace (-2,2,100);

>> y = exp (-t.^2);

>> plot (t, y)

>> t = -1.8; y = 0.8; (1.101)

>> hT = text (t, y, 'Gaussian')

>> xlabel (' q','fontsize', 18, 'fontname', 'symbol')

>> ylabel ('y', 'fontsize', 18)

## >>

The resulting graph with the customized text and axis labels is shown in Figure

1-27. Note that q is slightly displaced from the center of the graph for clarity.

Figure 1-27. Graph with added text and customized axis labels.

Three dimensional graphs can also be created with MATLAB. The plots can be customized and the resulting figures can be viewed from different locations. The default view is (-37.5, 30). Each graph can become a subplot in a large figure as was done previously. We illustrate the program by finding the potential V caused by a dipole charge which we will examine in the next chapter. This potential is proportional to

(1.102)

where the charges Q1 = - Q2 = Q and the distances R1 and R2 are the distances between the charge location and the point of observation. The distance R1 is computed from . In MATLAB, this is written as

R1 = ((x - x1).^2 +(y - y1).^2).^1/2 (1.103)

The dot "." that appears before the circumflex symbol "^" indicates that an action will be taken on each individual item. We will compute the potential at equal intervals in the xy plane. In order to avoid the singularity of computing the potential at the location of the charges, we place the charges at

(x1 = 0, y1 = + 1/2) and (x2 = 0, y2 = - 1/2).

The MATLAB program is (Note the periods in (1.104))

>> [x,y] = meshgrid (-2:.2:2,-2:.2:2.);

>>R1= (x.^2 + (y - .5).^2).^.5;

>>R2= (x.^2 + (y + .5).^2).^.5; (1.104)

>>V = (1./R1) - (1./R2);

Plot potential - default mesh view

>>subplot (2, 2, 1)

>>mesh (x, y, V)

>>xlabel ('X') (1.105)

>>ylabel ('Y')

>>zlabel ('V')

>>title ('(a)')

## >>

Plot potential - change view

>>subplot (2, 2, 2)

>>mesh (x, y, V)

>>xlabel ('X') (1.106)

>>ylabel ('Y')

>>zlabel ('V')

>>title ('(b)')

## >>

Plot potential - changed view - equipotential contours

>>subplot (2, 2, 3)

>>meshc (x, y, V)

>>xlabel ('X') (1.107)

>>ylabel ('Y')

>>zlabel ('V')

>>title ('(c)')

## >>

Plot potential - changed view - default surface

>>subplot (2, 2, 4)

>>surf (x, y, V)

>>xlabel ('X') (1.108)

>>ylabel ('Y')

>>zlabel ('V')

>>title ('(d)')

## >>

The graphs, shown in Figure 1-28 have labels on their axes and a title.

Figure 1-28. Three dimensional plots. (a) Default mesh plot. (b) Viewing location changed. (c) Contour plot beneath default plot. (d) Default surface plot.

We will make extensive use of MATLAB in this text in order to generate the figures. The programs that were used to create the figures are included with the figures in several cases. Additional lengthy programs are given in Appendix E. All programs can be customized and modified by the reader and can become ".m" files. We will encounter the numerical solution of differential equations in Chapter 3.

Conclusion

The electromagnetic fields that will be described in the rest of this book will make use of vectors and the various differential operations that have been given in this chapter. The use of generalized orthogonal coordinates has faciliÂ­tated the stating of vector operations in different coordinate systems. In addition, the two theorems that allowed us to convert a surface integral into a closed line integral (Stokes' theorem) or a volume inteÂ­gral into a closed surface integral (divergence theorem) will be very important in gaining an apÂ­preciation of these fields. They will also be employed in later derivations to actually develop the basic laws of electroÂ­magnetic theory from the equaÂ­tions that arise from experiÂ­mental obÂ­servations. We have initially interpreted several applications of vecÂ­tors using fluids. This was done since most of us have gone to the beach at one time or have seen mechanical systems. At this stage, electric and magnetic fields may seem rather opaque.

Problems

1. Find the vector that connects the two opposite corners of a cube whose volume is a3. One corner of the cube is located at the center of a Cartesian coordinate system. Write this vector also in terms of the magnitude times a unit vector.

2. Find the vector B from the origin to the opposite corner that lies in the xy plane.

3. Given two vectors A = 3ux + 4uy + 5uz and B = -5ux + 4uy - 3uz, find

A + B and A - B.

4. Sketch a vector field defined by A = y2ux - xuy. The length of the vectors in the field should be proportional to the field at that point. Find the magniÂ­tude of this vector at the point (5,7).

5. Find the scalar product of the two vectors defined by A = 3ux + 4uy + 5uz and B = -5ux + 4uy - 3uz. Determine the angle between these two vectors.

6. Find the projection of a vector from the origin to a point defined at (1,2,3) on the vector from the origin to a point defined at (2,1,6). Find the angle between these two vectors.

7. Express the vector field A = 3ux + 4uy + 5uz in cylindrical coordiÂ­nates.

8 Express the vector A = 3ur + 4uj + 5uz that is in cylindrical coordinates into Cartesian coordinates.

9. Express the vector field A = 3ux + 4uy + 5uz in spherical coordiÂ­nates.

10 Express the vector A = 3ur + 4uj + 5uq that is in spherical coordinates into Cartesian coordinates.

11. Find the vector product (A x B) of the two vectors defined by A = 3ux + 4uy + 5uz and B = -5ux + 4uy - 3uz. Show that the vector found from

(B x A) is in the opposite direction.

12. For the vectors A = 3ux + 4uy + 5uz, B = -5ux + 4uy - 3uz, and C = 2ux +3uy + 4uz; show that A x (B x C) = B(A â€¢ C) - C(A â€¢ B).

13. For the vectors A = 3ur + 4uj and B = 4ur + 3uj, and C = 5ur + 5uj show that A x (B x C) = B(A â€¢ C) - C(A â€¢ B).

14. Find the area of the parallelogram using vector notation. Compare your result with that found graphically.

15. Show that we can use the vector definitions A â€¢ B = 0 and A x B = 0 to exÂ­press that two vectors are perpendicular and parallel to each other.

16. Let A = -2ux + 3uy + 4uz; B = 7ux + 1uy + 2uz; and C = -1ux + 2uy + 4uz. Find (a) A x B. (b) (A x B) â€¢ C. (c) A â€¢ (B x C).

17. Calculate the work required to move a mass m against a force field

F = 5ux + 7uy

along the indicated diÂ­rect path from point a to point b.

18. Calculate the work required to move a mass m against a force field

F = yux + xuy along the path abc and along the path adc. Is this field conÂ­servative?

19 Calculate the work required to move a mass m against a force field

F = rur + juj along the path abc.

20 Calculate the work required to move a mass m against a force field F = juj if the radius of the circle is a and 0 â‰¤ j â‰¤ 2Ï€.

21. Calculate the closed surface inteÂ­gral A â€¢ ds if A = xux + yuy and the surface is the surface of a cube.

22. Evaluate the closed surface integral of the vector A = xyz ux + xyz uy + xyz uz over the cubical surface shown in Problem 21.

23 Evaluate the closed surface integral of the vector A = 3 ur over the spherical surface that has a radius a.

24 Find the surface area of a cylindriÂ­cal surface by setting up and evaluatÂ­ing the integrals.

25. A hill can be modeled with the equation H = 10 - x2 - 3y2 where H is the elevation of the hill. Find the path that a frictionless ball would take in order that it experiÂ­enced the greatÂ­est change of elevation in the shortest change of horizontal position. Assume that the motion of the ball is unconstrained.

26. For the hill described in problem 25, find the angle q with reÂ­spect to the x axis that the ball makes as it rolls down the hill at the point (2,1).

27. Find the gradient of the function H = x2yz and also the direcÂ­tional derivative of H specified by the unit vector u = (ux + uy + uz)/(3)1/3 at the point (1, 2, 3).

28. By direct differentiation show that where

and ' denotes differentiation with respect to the variables x', y', and z'.

29. Calculate the divergence of the vector A = x3y sin (Ï€z) ux + xy sin (Ï€z) uy + x2y2z2uz at the point (1,1,1).

30 Show that the divergence theorem is valid for a cube located at the center of a Cartesian coordinate system for a vector A = xux + 2uy.

31. Show that the divergence theorem is valid for a sphere of radius a located at the center of a coordinate system for a vector A = r ur

32. The water that flows in a channel with sides at x = 0 and x = a has a veÂ­locity distribution . The botÂ­tom of the river is at z = 0. A small paddle wheel with its axis parallel to the z axis is inÂ­serted into the channel and is free to roÂ­tate. Find the relative rates of rotation at the points

## .

Will the paddle wheel rotate if its axis is parallel to the x axis or the y axis?

33. Evaluate the line integral of the vector function A = xux + x2yuy + xyzuz around the square contour C. Integrate x A over the surÂ­face bounded by C. Show that this examÂ­ple satisfies Stokes' theorem.

34. Show that x A = 0 if A = uj (1/r) in cylindrical coordinates.

35 Show that x A = 0 if A = ur r2 in spherical coordinates.

36. In rectangular coordinates, verify that â€¢ x A = 0 where

A = x2y2z2[ux + uy + uz]

by carrying out the detailed differentiations.

37. In rectangular coordinates, verify that x ø = 0 where

ø = 3x2y + 4z2x

by carrying out the detailed differentiations.

38. In rectangular coordinates, verify that x øA = (ø) x A + ø x A where A = x2y2z2[ux + uy + uz] and ø = 3x2y + 4z2x

by carrying out the detailed differentiations.

39. In rectangular coordinates, verify that â€¢ (øA) = A â€¢ ø + ø â€¢ A where A = x2y2z2[ux + uy + uz] and ø = 3x2y + 4z2x

by carrying out the detailed differentiations.

40. By direct differentiation, show that at all points where

r â‰  0 where .

41. Express the signal v = 100 cos (120 Ï€ t - 45o) in phasor notation.

42. Given a phasor V = 10 + j 5. Find the sinusoidal signal this represents if the frequency = 60 Hz.

43. Find the phasor notation of v = cos [120 Ï€t - 60o] - sin [120 Ï€t].

44. Find the current in the circuit if v = 10 cos (120 Ï€t)

45. Repeat problem 44 with v = 10 cos (120 Ï€t + 45o).

46. Using MATLAB, write a program to convert degrees C to degrees F. Plot the results.

47. Using MATLAB, write a program to convert a yard stick to a meter stick. Plot the results.

48. Using MATLAB, add the two vectors A = 3ux + 4uy + 5uz and B = 5ux + 4uy + 3uz.

49. Using MATLAB, subtract the two vectors A = 3ux + 4uy + 5uz and B = 5ux + 4uy + 3uz.

50. Using MATLAB, find A â€¢ B where A = 3ux + 4uy + 5uz and B = 5ux + 4uy + 3uz

51. Using MATLAB, find A x B where A = 3ux + 4uy + 5uz and B = 5ux + 4uy + 3uz

52. Using MATLAB, plot y= exp(-x) on a linear and a semilog graph.

53. Using MATLAB, plot two cycles of y= cos(x) on a linear and a polar graph.

54. Using MATLAB, plot y= (x) exp(-x) on a linear graph for -3 â‰¤ x â‰¤ 3..

55. Using MATLAB, plot y= (x) exp(-x) on a linear graph for -3 â‰¤ x â‰¤ 3. Add the plot of y = x2 with a different linestyle to this graph..