Examples

download Examples

of 32

description

MATLAB examples

Transcript of Examples

Importing data

Matlab Programming for Engineers

Introduction to Matlab Matlab Basics Vectors and Matrices Loops Plots MATLAB examples 1Program :

>> r=10;>> i=0:2:10;>> v=i.*r;>> p=(i.^2)*r;>> sol=[p v i]

Output :sol =

Columns 1 through 6

0 40 160 360 640 1000

Columns 7 through 12

0 20 40 60 80 100

Columns 13 through 18

0 2 4 6 8 107/29/2015 3:12:50 PM2MATLAB PRESENTATIONUse MATLAB in Electrical Circuits :

7/29/2015 3:12:50 PMMATLAB PRESENTATION

47/29/2015 3:12:50 PMMATLAB PRESENTATION

57/29/2015 3:12:50 PMMATLAB PRESENTATIONSolution usng MATLAB :Z = [40 -10 -30; -10 30 -5; -30 -5 65]; V = [10 0 0]'; % solve for the loop currents I = inv(Z)*V; % current through RB is calculated IRB = I(3) - I(2); fprintf('the current through R is %f Amps \n',IRB) % the power supplied by source is calculated PS = I(1)*10; fprintf('the power supplied by 10V source is %f watts \n',PS)67/29/2015 3:12:50 PMMATLAB PRESENTATIONMATLAB answers are :

the current through R is 0.037037 Amps the power supplied by 10V source is 4.753086 watts77/29/2015 3:12:50 PMMATLAB PRESENTATIONVectors and Matrices Arithmetic operations Matrices Example:Solve for V1 and V2

10j10-j51.50o2-90o7/29/2015 3:12:50 PM8MATLAB PRESENTATIONExample (cont)(0.1 + j0.2)V1 j0.2V2 = -j2 - j0.2V1 + j0.1V2 = 1.5Vectors and Matrices Arithmetic operations Matrices

=

Axy=Example (cont)Vectors and Matrices Arithmetic operations Matrices >>> A=[(0.1+0.2j) -0.2j;-0.2j 0.1j]A = 0.1000+ 0.2000i 0- 0.2000i 0- 0.2000i 0+ 0.1000i>>> y=[-2j;1.5]y = 0- 2.0000i 1.5000>>> x=A\yx = 14.0000+ 8.0000i 28.0000+ 1.0000i>>> * A\B is the matrix division of A into B, which is roughly the same as INV(A)*B *Example (cont)Vectors and Matrices Arithmetic operations Matrices >>> V1= abs(x(1,:))V1 = 16.1245

>>> V1ang= angle(x(1,:))V1ang = 0.5191

V1 = 16.1229.7o V

7/29/2015 3:12:50 PM12MATLAB PRESENTATIONHOW TO IMPORT LOAD DATASNO se re Resistance Reactance1 1 2 0.0005 0.00122 2 3 0.0005 0.00123 3 4 0.0015 0.00364 4 5 0.0251 0.02945 5 6 0.3660 0.1864

7/29/2015 3:12:50 PM13MATLAB PRESENTATIONldata=[ 1 1 2 0.0005 0.00122 2 3 0.0005 0.00123 3 4 0.0015 0.00364 4 5 0.0251 0.02945 5 6 0.3660 0.1864];7/29/2015 3:12:50 PM14MATLAB PRESENTATIONclear allldata=[ 1 1 2 0.0005 0.00122 2 3 0.0005 0.00123 3 4 0.0015 0.00364 4 5 0.0251 0.02945 5 6 0.3660 0.1864];nb=6;nl=nb-1;se=ldata(:,2);re=ldata(:,3);r=ldata(:,4);x=ldata(:,5)for i=1:nl z(i)=r(i)+j*x(i);endfor i=1:5disp(z(i))end

7/29/2015 3:12:50 PM15MATLAB PRESENTATIONIf R = 10 Ohms and the current is increased from 0 to 10 A with increments of 2A, write a MATLAB program to generate a table of current, voltage and power dissipation.7/29/2015 3:12:50 PM16MATLAB PRESENTATIONM-files : script and function files (script)Example RLC circuitExercise 1:Write an mfile to plot Z, Xc and XLversus frequency for R =10, C = 100 uF, L = 0.01 H. +VR = 10CL

eg4.meg5_exercise1.m7/29/2015 3:12:50 PM17MATLAB PRESENTATIONM-files : script and function files (script)Example RLC circuitTotal impedance is given by:

When

7/29/2015 3:12:50 PM18MATLAB PRESENTATIONM-files : script and function files (script)Example RLC circuit

eg4.meg5_exercise1.m7/29/2015 3:12:50 PM19MATLAB PRESENTATIONM-files : script and function files (script)For a given values of C and L, plot the following versus the frequency a)the total impedance , Xc and XLphase angle of the total impedancefor 100 < < 2000Example RLC circuit+VR = 10CL

eg6.m7/29/2015 3:12:50 PM20MATLAB PRESENTATIONM-files : script and function files (script)Example RLC circuit

eg6.m7/29/2015 3:12:50 PM21MATLAB PRESENTATIONSimulinkModel simplified representation of a system e.g. using mathematical equationWe simulate a model to study the behavior of a system need to verify that our model is correct expect resultsKnowing how to use Simulink or MATLAB does not mean that you know how to model a system7/29/2015 3:12:50 PM22MATLAB PRESENTATIONSimulinkProblem: We need to simulate the resonant circuit and display the current waveform as we change the frequency dynamically.+v(t) = 5 sin ti10 100 uF0.01 HVaries from 0 to 2000 rad/sObserve the current. What do we expect ?The amplitude of the current waveform will become maximum at resonant frequency, i.e. at = 1000 rad/s

7/29/2015 3:12:50 PM23MATLAB PRESENTATION

SimulinkHow to model our resonant circuit ?+v(t) = 5 sin ti10 100 uF0.01 H

Writing KVL around the loop,7/29/2015 3:12:50 PM24MATLAB PRESENTATIONSimulink

Differentiate wrt time and re-arrange:Taking Laplace transform:

7/29/2015 3:12:50 PM25MATLAB PRESENTATIONSimulinkThus the current can be obtained from the voltage:

VI7/29/2015 3:12:50 PM26MATLAB PRESENTATIONSimulinkStart Simulink by typing simulink at Matlab promptSimulink library and untitled windows appearIt is here where we construct our model.It is where we obtain the blocks to construct our model7/29/2015 3:12:50 PM27MATLAB PRESENTATIONSimulinkConstructing the model using Simulink:Drag and drop block from the Simulink library window to the untitled window

7/29/2015 3:12:50 PM28MATLAB PRESENTATIONSimulinkConstructing the model using Simulink:

7/29/2015 3:12:50 PM29MATLAB PRESENTATIONSimulinkWe need to vary the frequency and observe the current

eg8_sim.mdlFrom initial problem definition, the input is 5sin(t).You should be able to decipher why the input works, but you do not need to create your own input subsystems of this form.7/29/2015 3:12:50 PM30MATLAB PRESENTATIONSimulink

7/29/2015 3:12:50 PM31MATLAB PRESENTATIONSimulinkThe waveform can be displayed using scope similar to the scope in the lab

eg9_sim.mdl7/29/2015 3:12:51 PM32MATLAB PRESENTATION