matlab procedure

19
Dr R.P. Jenner, University of Greenwich, 2010/11 Introduction to Computer Modelling using Matlab 1 the UNIVERSITY of GREENWICH Programme : Foundation Year Course Title : Engineering Project Design & Implementation Course Unit : Electrical Principles

Upload: tompirima-bestman

Post on 28-Apr-2015

54 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Matlab Procedure

Dr R.P. Jenner, University of Greenwich, 2010/11

Introduction to Computer Modelling

using Matlab

1

theUNIVERSITYofGREENWICH

Programme : Foundation YearCourse Title : Engineering Project Design & ImplementationCourse Unit : Electrical PrinciplesCourse Tutor : Dr R.P. Jenner

Page 2: Matlab Procedure

Dr R.P. Jenner, University of Greenwich, 2010/11

IntroductionWhen an engineering system is described by a mathematical model (and normally a subsequent computer model) great flexibility is introduced into the engineering design process. The various parameters within the mathematical model can be manipulated by the design engineers in order to see how the system would behave under a wide variety of operating conditions. By this means, the proposed engineering system can be fully evaluated before we go to the time trouble and expense of actually manufacturing the engineering system. In the case of the design of your AC generators, they can be described by a mathematical equation developed by Faraday. Faraday’s equations contain information concerning the physical components that will form the AC generator that you will design; the relevant equation developed by Faraday is shown below…

Wherevind = induced AC voltage (V)N = number of turns of wireB = flux density (T)A = cross sectional area of the coil (cm2)ω = angular frequency in radians per second (rad/s) θ = phase angle (rad/s)

As you can see from the mathematical model of an AC generator shown above, it contains information concerning physical components that are under your control as the design engineer, specifically…

The strength of the magnets that you are using (B) The size of the coils that you manufacture (A) The number of turns of wire you place on your coils (N) The speed at which you rotate the turbine blade (ω)

The mathematical model allows us to experiment with our system to produce the best combination of the above parameters to give us the amount of AC voltage that we need. Once we have the optimum combination of the physical components of the generator, we can then physically build the generator directly from our mathematical model.

The problem with a mathematical model is that we have to perform many calculations in order to be able to see the output of the model. For example, in the above equation we would have to insert a phase angle and calculate the AC voltage. We would then have to change the phase angle and calculate the AC voltage again; this process would continue until we had calculated the voltage over the desired phase angle range. For example, suppose we wished to look at the induced voltage for one full turn of the turbine in 1o steps, as there are 360o in one full turn, we would have to calculate the voltage 360 times; this is very time consuming and prone to error. Rather than perform multiple calculations by hand, we will construct a computer model of the system and use the computer to perform the calculations for us; we will construct our computer model of the AC generator using Matlab.

Why Matlab?Matlab is an extremely powerful mathematical simulator which is extensively used by engineers and mathematicians in industry to model and simulate physical systems. Matlab is also extensively used by universities in research and also as a teaching tool to prepare students for industry. Matlab is in fact two computer packages in one, we have the standard Matlab

2

Page 3: Matlab Procedure

Dr R.P. Jenner, University of Greenwich, 2010/11

interface in which computer code is written to describe a certain system, the second interface is called Simulink which is a schematic capture programme, i.e., we drag and drop icons from a library and connect them together to form the system (we essentially draw the system); for this project we will be using the standard interface.

Matlab fundamentalsWhen we run Matlab, you will see several windows, the window that we are most interested in is the command window. The command window is where the results of our simulations will appear and is also where we can input computer code to construct a computer model. However, the command window is not particularly suitable to write our programmes as the code we enter is not saved and we will lose it when we close Matlab. Instead, we will write our computer code in a file called a .m file (Matlab file), we can save this file and load it and run it whenever we wish.

Create an m-fileTo create an m-file go to…

FILE → NEW → M-FILE

When you do this you will see something similar to that shown below…

Save the m-fileThe first thing we need to do is give the m-file a name and save it. Give the file a sensible name in order that you can easily identify it; the file name must not contain white spaces. If you want to separate words use an underscore.

To save your m-file, go to the top left of your m-file and go to…

FILE → SAVE AS

3

Page 4: Matlab Procedure

Dr R.P. Jenner, University of Greenwich, 2010/11

Enter your file name (no spaces) into the file name box, navigate to the folder you wish to save the file and then click ‘save’. The file type is automatically set.

Now that we have created and saved our file, we can now start to write our programme.

Clear the memory and clear the screenThe command window in Matlab contains memory; this means that when we run our programme, old data may be held in memory which may affect the operation of our programme. It is good practice to wipe the command window memory before we run our programme; the command that clears the command window memory is the ‘clear’ command.

The ‘clear’ command wipes the command window memory, however, the ‘clear’ command does not remove old data that is shown in the command window screen. To remove old data showing in the command window we must clear the screen, the command to do this is the ‘clc’ command.

The first two lines of our programme will therefore be clear and clc as shown below…

Adding comments to the codeWhen writing our computer programmes we must add comments to our code to explain what each line of code is doing. We must remember that other engineers may have to work with our code and they must be able to understand it, we therefore add comments in order that other people be able to understand our code. In Matlab, we add a comment by using the ‘%’ symbol, when we add the % symbol it will turn green and anything we write after it will be seen as a comment and Matlab will ignore it. Comments can be seen on the figure shown above.

4

Comments Run button

Page 5: Matlab Procedure

Dr R.P. Jenner, University of Greenwich, 2010/11

Running your programmeBefore the memory and screen are cleared, we must execute the commands by running our programme. In order to do this we click on the run button as can be seen on the figure shown above. When we click on the run button for the first time we will see the following…

This dialogue box is simply asking if we wish to change into the directory where our file is located…we do so click on ‘change directory’.

Displaying resultsWhen we run or computer programme, we may or may not want results to be written to the command window. Data such as the result of a calculation will normally be written to the command window, data such as a time-base to plot a graph would not normally be required to be written to the command window. We can very easily choose to show or hide data by the use of a semicolon (;), if we put a semicolon at the end of our line of computer code no data will be written to the command window. If we do not place a semicolon at the end of a line of code data will be written to the command window. For example…

X = 2 + 2; no result will appear in the command windowX = 2 + 2 the result will appear in the command window

We need to only display data that we need to see; unnecessary data clogs up the screen, slows our simulations down, and uses up memory…if you don’t need to see the data, use a ; to stop it being written to the command window.

Constructing a computer modelNow that we have created our m-file, saved it, changed into the file directory, and cleared the memory and screen, we can now develop our computer model. Matlab contains many thousands of commands that perform certain mathematical functions; our model will be comprised of some of these commands placed in the correct order to perform the mathematics that we want.

For project 3 we wish to create a computer model of our AC generator; this model is based on Faraday’s laws as shown on page 2. From Faraday’s law we know that we must have the following information…

N = number of turns of wireB = flux density (T)A = cross sectional area of the coil (cm2)ω = angular frequency in radians per second (rad/s) θ = phase angle (rad/s)

5

Page 6: Matlab Procedure

Dr R.P. Jenner, University of Greenwich, 2010/11

Of the required data, three of the list shown are simply numbers that can be directly inserted into the programme, these three values are the number of turns of wire (N), the strength of the magnets (B), and the area of the coil (A). There are two ways which we can enter this data, we can type it directly into our computer programme or we can get the user of the programme to enter it; we will start by entering it directly.

Example: construct a computer model to represent an AC generator that has a turbine turning at 500 rpm (revolutions per minute), a magnetic flux density of 400 mT (400 milli-Tesla), 100 turns of copper wire, and a coil area of 10cm2. The programme should show one full turn of the turbine.

The first thing we are going to do is enter the number of turns of wire, the area of the coil, and the strength of the magnetic field into our programme, the computer code is…

N = 100; % number of turns of wire on the coilA = 10e-4; % area of the coil in cm^2B = 400e-3; % strength of the magnetic field in Tesla

You will see from the above, the numbers have been assigned to a name, this allows use to use the name throughout our programme rather than having to keep typing the number. For example, we assigned the number 100 to the name N, thus N has the value of 100. wherever we type N in our programme the value 100 will be inserted.

You will notice that the number of turns of wire is simply a number, however, the area of the coil and the strength of the magnet are expressed in scientific notation.

For the area of the coil, Faraday’s law requires that the area is expressed in cm2, however, Matlab works using standard SI units (standard system of units) and assumes the number is in m2, we must therefore express cm2 in terms of m2…

1m2 = 100cm2

100cm2 = 10,000 square cm

1m2 = 10,000 square cm, therefore each square cm is 1/10,000 = 0.0001m2 or 1*10-4m2

As each square cm is equal to 1*10-4m2 and we have 10 square centimetres for our coil, we have 10*10-4m2. In Matlab, this is written as 10e-4.

For the strength of the magnetic field (B), this figure relates to the material used for the magnet and is normally shown on the manufacturers data sheet or can be looked-up. In this example, the strength of the magnet is 400mT, milli is 1*10-3, thus we have 400*10-3 Tesla; this is entered into Matlab as 400e-3.

The above discussion does not affect how we write our computer programme, but it does show that what we enter into our computer model is incredibly important and must be expressed in the correct number format and SI unit.

Our programme should now look like the following…

6

Page 7: Matlab Procedure

Dr R.P. Jenner, University of Greenwich, 2010/11

We have now entered the number of turns of wire, the strength of the magnets, and the area of the coil into our computer programme, there are two remaining parameters that must be entered before we can calculate the output voltage of our AC generator. We now need to enter the angular frequency (w) in radians per second and the phase angle (θ) in radians.

The angular frequency (w) is the speed of rotation of the turbine blade, we will physically measure the speed of rotation of the turbine blade in your constructed AC generator using an optical tachometer which will tell us the speed of rotation in revolutions per minute (rpm). However, Faraday’s law requires us to enter the angular frequency in radians per second. Thus we must use our computer programme to convert revolutions per minute to radians per second; fortunately, this is a fairly simple process and we can use the following equations…

Step 1: convert rpm to units of frequency in cycles per second (Hertz); we divide revs per minute by 60 to give revs per second, i.e., Hertz…

In order to perform this conversion in Matlab we must first enter the angular frequency in rpm into our computer programme, therefore…

rpm = 500; % angular frequency in revolutions per minute

Now we have entered the angular frequency in rpm, we can write the equation into Matlab…

w_Hz = rpm/60 % divide revs per minute by 60 to give revs per second, i.e, Hertz

Step 2: convert Hz to radians per secondNow that we have converted revs per minute to revs per second (cycles per second, i.e., Hz), we must convert Hz to radians; we will use the following formula to do this…

In Matlab we would write this as…

w = 2*pi*w_Hz % convert angular frequency from Hz to Radians per second

Our computer programme should now look like the following…

7

Page 8: Matlab Procedure

Dr R.P. Jenner, University of Greenwich, 2010/11

Now that we have worked out the angular frequency of the turbine in radians per second, we now only have one more parameter to specify, this is the phase angle (θ) in radians. The phase angle relates to the position of the turbine in relation to the magnets and coils, obviously as the turbine turns, the phase angle of the turbine changes. For example, if we turned the turbine one full cycle it would have travelled through 3600 i.e., one full circle. Before we start to move the turbine, the phase angle will be 00, as we start to move the turbine, the phase angle will increase from 00 through to 3600. The phase angle (θ) is therefore the only parameter in our computer model that changes. This means that every time the turbine moves, we have to calculate the induced voltage using Faraday’s equation.

For example, if we moved the turbine from 00 to 3600 in 10 steps, we would have to put 0 into the equation and calculate the induced AC voltage, we would then change the phase angle to 1 and recalculate, then change it to 2 and recalculate…this process would continue until we had changed the phase angle from 00 to 3600. It should be clear that the phase angle (θ) is not necessarily a single number but is a range of numbers.

The way in which we specify a range of numbers in Matlab is to specify a start, a step, and a stop in the following format…

range = start : step: stop

If we wished to specify the range 00 to 3600 in 10 steps, this would be…

range = 0 : 1 : 360

However, Faraday’s law requires that we express the phase angle in radians not degrees. Both degrees and radians are angular measurements and relate to the unit circle. Essentially, both degrees and radians specify how many steps we take in completing one revolution of a circle. When working in degrees, we know the circle is divided up into 360 equal steps. When we are working in radians, we have fewer steps to complete one circle, if fact, there are only 6.28 steps to complete a circle; 6.28 relates to 2π, thus there are 2π radians per unit circle.

Fortunately for us, Matlab works in radians by default, thus all we need to do is specify the range of phase angles in radians we wish to solve for. For this example we wish to work out the induced voltage for one revolution of the turbine, i.e., one revolution of a circle. We know that our starting point will be 0 radians, as we want to solve for one revolution we know the stop point will be 2π radians as there are 2π radians per unit circle.

We know the starting phase angle, and we also know the finishing phase angle; the question is…

How many steps should we take between 0 and 2π?

8

Page 9: Matlab Procedure

Dr R.P. Jenner, University of Greenwich, 2010/11

If we make the step too big, we will only perform a small amount of calculations and we will have little data and we will have an inaccurate model. For example, if we set the step to be 1 radian the following graph of the induced voltage would be produced…

As we can see, the graph produced is very jagged and is not very accurate; what we need to do is increase the amount of steps that we take as we go from 0 to 2π radians. This time, let us go from 0 to 2π radians to 0.1 radian steps thus changing the phase angle ten times more than we did previously; the results are shown below…

9

Page 10: Matlab Procedure

Dr R.P. Jenner, University of Greenwich, 2010/11

As we can see, the smaller step size has meant we have calculated the induced voltage many more times and have a much more accurate, smoother, set of data.

For our computer model, we can specify the phase angle range as follows…

theta = 0 : 0.1: 2*pi; % phase angle range from 0 radians to 2π radians in 0.1 radian steps

Our computer model is now…

We now have all data required for Faraday’s law and the model of the AC generator, and so we can now calculate the induced voltage for the range of phase angles specified. Faraday’s law is…

Induced voltage = NBAwcos(θ)

The equation simply contains multiplication and the calculation of the cosine of the phase angle θ; we use the ‘cos’ command in Matlab to do this for us. Thus now we have all required data, our computer model of Faraday’s law in Matlab is…

induced_voltage = N*B*A*w*cos(theta); % Faraday’s law calculating induced AC voltage

Our computer programme is now…

10

Page 11: Matlab Procedure

Dr R.P. Jenner, University of Greenwich, 2010/11

We have now successfully modelled an AC generator by the application of Faraday’s laws. All we have to do now is display the results of our model; we are going to do two things, plot a graph of the data, and display the calculated data and save it to a file.

Plotting the dataPlotting data within Matlab is very straight forward and is of the following format…

plot(x_axis_data,y_axis_data)

In our case, we calculated the induced voltage over a range of phase angles; when plotting in Matlab we specify the range over which performed calculations on the x-axis (phase angle (θ) in this case), and the magnitude of what we calculated on the y-axis (induced voltage in this case), thus for our plot, we will use the following Matlab code…

plot(theta, induced_voltage) % plot phase angle vs induced voltage

Although we have now created a graph of the AC waveform, the graph contains no title, labels on the x and y axes or any units; we must add this information to the graph in order we know exactly what we are looking at. This done, we will add a grid to the graph in order that it is easier for us to read off information. To do this we will use the ‘title’, ‘xlabel’, ‘ylabel’, and ‘grid’ commands; consider the following…

title('Induced Voltage (V) vs Phase Angle (Radians)');xlabel('Phase Angle (Radians)');ylabel('Induced Voltage (V)')grid

Our computer model is now…

Displaying & Saving the data

11

Page 12: Matlab Procedure

Dr R.P. Jenner, University of Greenwich, 2010/11

Although we have now produced a graph of the generated waveform, this is only of limited use to us. What we want to observe and store is the actual data calculated from Faraday’s equation, i.e., we want to see and save the phase angle data and the induced voltage data.

Displaying the data is straight forward in Matlab, essentially we need to construct a table of data; this is an easy process, we just need to insert the name of the data that we wish to put into the table in square brackets. If the data is shown in rows and we want it in columns we must transpose the data, this just means that we wish to see the data in columns rather than rows or vice versa; the command to transpose data is the ‘ command. In our example, we wish to see the phase angle and the induced voltage in two columns in order that we be easily able to see the data, the Matlab code to do this is…

data=[theta’ induced_voltage’] % two-column table of phase angle and induced voltage

When we run our programme, not only will a graph of the generated AC waveform appear, we will also now see two columns of data from which the graph was established. The data will appear in the ‘command window’ once the programme has been run. Consider the following (only a third of the data is shown to save space on this page)…

data = 0.0000 2.0944 0.1000 2.0839 0.2000 2.0526 0.3000 2.0009 0.4000 1.9291 0.5000 1.8380 0.6000 1.7286 0.7000 1.6019 0.8000 1.4592 0.9000 1.3019 1.0000 1.1316 1.1000 0.9500 1.2000 0.7589 1.3000 0.5602 1.4000 0.3560 1.5000 0.1482 1.6000 -0.0612 1.7000 -0.2699 1.8000 -0.4759 1.9000 -0.6771 2.0000 -0.8716

What we can see is that we have a table of the phase angle and induced voltage; we can see that the phase angle (θ) is ascending in steps of 0.1 radians as we specified in our programme. In the next column we can see the AC voltage induced at that particular phase angle; for example, we can see that at 0 radians we have an induced voltage of 2.0944V, at 1 radian we have an induced voltage of 1.1316V etc.

Although we now have the calculated data in the command window, we may wish to save the data that we have generated to a file in order that it is stored and can be retrieved whenever

12

Page 13: Matlab Procedure

Dr R.P. Jenner, University of Greenwich, 2010/11

we wish; this is particularly useful when we have a large amount of calculated data (indeed when we have very large amounts of data Matlab will not be able to display it on the screen thus we would have no choice but to save the data into a file).

Matlab has extensive file formats we can choose from; however, the choice of file will depend upon what we wish to do with the stored data. There are normally two choices as to what we want to do with the data…

1. load the saved data back into Matlab for further analysis2. load the saved data into another computer package such as Notepad or Excel etc

If we wish to store the data in a file and load it back into Matlab for further analysis, we need to create a .mat file; the format is as follows…

save AC_data.mat data % save the two-column calculated data to a mat file

AC_data.mat is the name of the file we wish to save; ‘data’ is the name of the data we wish to write to the file. When the programme is run, a file called AC_data will be written to the same directory as your computer programme. The .mat file cannot be opened with any other package other than Matlab. When you wish to load the saved file back into matlab we use the ‘load’ command; the format is simply load filename.

Our computer programme is now…

If you wish to save a file which can be read by Notepad, use the following…

save –ascii AC_data.mat data % save the two-column calculated data to a text mat fileTo save a file that can be loaded into Microsoft Excel use…

13

Page 14: Matlab Procedure

Dr R.P. Jenner, University of Greenwich, 2010/11

xlswrite('AC_data.xls',data) % create an Excel compatible file

If you wish to import an Excel file into Matlab use the ‘xlsread’ command.

Warning! When you run your programme, the specified file will be created in the specified location; if you run your programme again the file will be overwritten! If you do not wish the file to be overwritten, change the name of the file created.

Performing simulationsWe now have a computer model of an AC generator; we can now perform simulations observing how the AC waveform produced changes as we change the parameters. For example, we could perform a simulation using 100 turns of wire and observe the AC waveform produced. We could increase the number of turns of wire to 200 for example and see what effect this has upon the generated AC waveform; the computer model allows us to experiment with the system before we build it.

Important! The key to designing your AC generator will be the maximum rpm of the turbine blade that can be achieved from the water from the tank being passed over it. You must remember that the water is not pressurised and so we are dependant upon gravity to produce the force to turn the turbine. You must mathematically work out as accurately as you can, the maximum rpm that you will be able to achieve with the water source available. Once this figure has been established it can be put into your computer model and the rest of your AC generator system can be designed around it. For example, there is no point in putting 1000rpm into your computer model for example and designing your system…if the maximum rpm you can actually achieve is 250 rpm for example; your system will not produce the required AC voltage. You must establish the maximum rpm available to you before you design your system.

14