Upload
lindsay-hunt
View
213
Download
0
Embed Size (px)
Citation preview
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 11
CPET 190 CPET 190
Lecture 7Lecture 7
Problem Solving with MATLABProblem Solving with MATLAB
Paul LinPaul Lin
http://www.etcs.ipfw.edu/~linhttp://www.etcs.ipfw.edu/~lin
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 22
Lecture 7: MATLAB Built-In FunctionsLecture 7: MATLAB Built-In Functions
7-1 Managing Variables and Workspace7-1 Managing Variables and Workspace
7-2 MATLAB Commands for Working with 7-2 MATLAB Commands for Working with Files and the Operating SystemFiles and the Operating System
7-3 Controlling Command Window7-3 Controlling Command Window
7-4 Time and Date Functions7-4 Time and Date Functions
7-5 Special Variables and Constants7-5 Special Variables and Constants
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 33
7-1 Managing Variables and 7-1 Managing Variables and WorkspaceWorkspace
Removing Unneeded VariablesRemoving Unneeded Variables• whowho - List current variables- List current variables• whoswhos - List current variables, long form- List current variables, long form• clearclear - Clear variables and functions- Clear variables and functions
Creating New VariablesCreating New Variables• lengthlength - Length of vector- Length of vector• sizesize - Size of matrix- Size of matrix
Saving to and Retrieving from Disk Files Saving to and Retrieving from Disk Files • load load - Retrieve variables from disks- Retrieve variables from disks• savesave - Save workspace variables to disk- Save workspace variables to disk• packpack - Consolidate workspace memory- Consolidate workspace memory
Workspace displayWorkspace display• dispdisp - Display matrix or text- Display matrix or text• fprintffprintf
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 44
7-1 Managing Variables and 7-1 Managing Variables and Workspace Workspace (continue)(continue)
Example 7-1: Example 7-1:
%cpet190_ex7_1.m%cpet190_ex7_1.mF = 4000;F = 4000;T = 1/F;T = 1/F;dt = 0.01*T;dt = 0.01*T;t = 0:dt:2*T;t = 0:dt:2*T;e1 = 5*sin(2*pi*F*t);e1 = 5*sin(2*pi*F*t);len = length(e1)len = length(e1)size_e1 = size(e1)size_e1 = size(e1)e2 = zeros(size(e1));e2 = zeros(size(e1));num = rand(2);num = rand(2);
fprintffprintf('The random number ('The random number array is %g\n', num);array is %g\n', num);fprintffprintf('\n');('\n');dispdisp('The random numbers');('The random numbers');dispdisp(num);(num);whoswhossave ex7_1save ex7_1clearclearload ex7_1load ex7_1whoswhos
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 55
7-1 Managing Variables and 7-1 Managing Variables and WorkspaceWorkspace
Example 7-1: OutputExample 7-1: Output
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 66
7-2 Working with Files and OS7-2 Working with Files and OS
The commands for working with files and The commands for working with files and operating systemoperating system
CommandCommand PurposePurpose
pwdpwd Show present working directoryShow present working directory
cdcd Change current working directoryChange current working directory
dirdir Directory listing (Window OS)Directory listing (Window OS)
lsls Directory Listing (Unix/Linux OS)Directory Listing (Unix/Linux OS)
deletedelete Delete fileDelete file
diarydiary Save text of MATLAB sessionSave text of MATLAB session
!! Execute OS commandsExecute OS commands
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 77
7-2 Working with Files and OS7-2 Working with Files and OS
Example 7-2Example 7-2
>> >> pwdpwdans =ans =C:\Courses\CPET190\C:\Courses\CPET190\
matlabex190matlabex190>> >> cd ..cd ..>> >> pwdpwdans =ans =C:\Courses\CPET190C:\Courses\CPET190>> >> dirdir. . Mlin_CPET190 Mlin_CPET190
.. .. ExsExsQuizQuiz LecturesLecturesMATLABExsMATLABExs
Example 7-2 (cont.)Example 7-2 (cont.)
>> >> diarydiary
>> diary off>> diary off
>> diary on>> diary on
>> >> !time!time
The current time is: The current time is: 11:31:05.02 11:31:05.02
Enter the new time: Enter the new time:
Open diary file using Open diary file using MATLAB M-file editor, MATLAB M-file editor, MS NOTEPAD, or MS MS NOTEPAD, or MS WordWord
helphelp
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 88
7-3 Controlling the Command 7-3 Controlling the Command WindowWindow
clcclc Clear command windowClear command windowhomehome Send cursor home (upper-left corner Send cursor home (upper-left corner
of command window)of command window)formatformat Set output formatSet output formatmoremore Control paged output in command Control paged output in command
windowwindowechoecho Echo on/off commands inside script Echo on/off commands inside script
files, for debugging purposesfiles, for debugging purposes
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 99
7-4 Time and Date Functions7-4 Time and Date Functions
FunctionFunction PurposePurposedatedate CalendarCalendarclockclock System clock; accuracy 1/100th of a System clock; accuracy 1/100th of a
secondsecondnownow Current date and time as serial date Current date and time as serial date
numbernumberdatestrdatestr Convert a serial date number into the Convert a serial date number into the
common date/time, save as a string common date/time, save as a string cputimecputime How many seconds the MATLAB How many seconds the MATLAB
session is active; accuracy 1/100 th of session is active; accuracy 1/100 th of secondsecond
etimeetime Elapsed time functionElapsed time functiontictic Start watch timer functionStart watch timer functiontoctoc Stop watch timer functionStop watch timer function
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 1010
7-4 Time and Date Functions7-4 Time and Date Functions
Time and Date FunctionsTime and Date Functions
Example 7-3:Example 7-3:>>>> datedateans =ans =03-Oct-200403-Oct-2004>> >> nownowans =ans = 7.3222e+0057.3222e+005>> >> datestr(now)datestr(now)ans =ans =03-Oct-2004 12:47:2903-Oct-2004 12:47:29>> >> help datestrhelp datestr
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 1111
7-4 Time and Date Functions 7-4 Time and Date Functions (continue)(continue)
CLOCK Current date and time as date vector.CLOCK Current date and time as date vector. CLOCK returns a six element date vector containing theCLOCK returns a six element date vector containing the current time and date in decimal form:current time and date in decimal form: CLOCK = [year month day hour minute seconds]CLOCK = [year month day hour minute seconds] The first five elements are integers. The seconds elementThe first five elements are integers. The seconds element is accurate to several digits beyond the decimal point.is accurate to several digits beyond the decimal point. FIX(CLOCK) rounds to integer display format.FIX(CLOCK) rounds to integer display format.
Example:Example:>>>> clockclockans =ans = 1.0e+003 *1.0e+003 * 2.0040 0.0100 0.0030 0.0120 0.0410 0.00712.0040 0.0100 0.0030 0.0120 0.0410 0.0071
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 1212
7-4 Time and Date Functions 7-4 Time and Date Functions (continue)(continue)
Example 7-4 measuring program execution time Example 7-4 measuring program execution time (accuracy – 1/100 th of a second) using cputime (accuracy – 1/100 th of a second) using cputime functionfunction
t1_1 = cputime;t1_1 = cputime;for i =1:1000for i =1:1000 num_array = num_array = inv(rand(30));inv(rand(30));endendt1_2 = cputime;t1_2 = cputime;time_1000 = t1_2 - t1_1time_1000 = t1_2 - t1_1time_once = time_1000/1000time_once = time_1000/1000
time_1000 =time_1000 = 0.36100.3610
time_once =time_once = 3.6100e-0043.6100e-004
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 1313
7-4 Time and Date Functions7-4 Time and Date Functions
Example 7-5 measuring program execution time Example 7-5 measuring program execution time (accuracy – 1/100 th of a second) using tic and toc (accuracy – 1/100 th of a second) using tic and toc functionsfunctions
tic;for i =1:1000 num_array = inv(rand(30));endtoc
elapsed_time =elapsed_time = 0.36100.3610
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 1414
7-5 Special Variables and Constants7-5 Special Variables and Constants
Example 7-6: AC circuit Example 7-6: AC circuit calculation using calculation using complex numbers.complex numbers.
A RLC circuit is shown on A RLC circuit is shown on this slide, findthis slide, find
a)a) Total impedance ZTotal impedance Z
b)b) Voltage and current Voltage and current across each components across each components
10 V 60HzSine wave
R = 8 ohms
XL = j 6 ohms
Xc = - j2 ohms
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 1515
7-5 Special Variables and Constants 7-5 Special Variables and Constants (continue)(continue)
Example 7-6: Example 7-6: Analysis: Domain knowledgeAnalysis: Domain knowledge XL = 2XL = 2ππfLfL, where L is the , where L is the
inductance in Henry, f is the inductance in Henry, f is the frequency of ac sourcefrequency of ac source
XC = 1/(2 XC = 1/(2 ππfC)fC), where C is the , where C is the capacitance in Farardcapacitance in Farard
Z = R + j(XL – XC) Z = R + j(XL – XC) -- total -- total impedance, where j shows impedance, where j shows the imaginary component of the imaginary component of a complex number a complex number
I = E/ZI = E/Z, total current, total current VR = I*RVR = I*R, voltage drop across , voltage drop across
resistorresistor VL = I*XLVL = I*XL, voltage drop , voltage drop
across the inductoracross the inductor VC = I*XCVC = I*XC voltage drop voltage drop
across the capacitoracross the capacitor
10 V 60HzSine wave
R = 8 ohms
XL = j 6 ohms
Xc = - j2 ohms
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 1616
7-5 Special Variables and Constants 7-5 Special Variables and Constants (continue)(continue)
Example 7-6: MATLAB Example 7-6: MATLAB Program Program
%RLC_1.m%RLC_1.mf = 60; R = 8;f = 60; R = 8;% Peak value of the sine wave% Peak value of the sine wavee = 10;e = 10;XL = j*6; XC = -j*2;XL = j*6; XC = -j*2;Z = R + (XL+XC)Z = R + (XL+XC)theta = angle(Z)theta = angle(Z) % 0.4636 pi% 0.4636 pi% 180 pi% 180 pi% ----- = ----% ----- = ----% theta_degree theta% theta_degree thetatheta_degree = (180*theta)/pitheta_degree = (180*theta)/pi% 26.5615 degree = 0.4636 pi% 26.5615 degree = 0.4636 pimag_Z = abs(Z)mag_Z = abs(Z)
Real Axis
Imaginary Axis
J
R = 8 ohms
XL = j6 ohms
XC = -j2 ohms
Theta
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 1717
7-5 Special Variables and Constants 7-5 Special Variables and Constants (continue)(continue)
Example 7-6:Example 7-6: MATLAB Program (cont.)MATLAB Program (cont.)%RLC_1.m %RLC_1.m %%I = e/ZI = e/ZI_thea_degree = angle(I) * I_thea_degree = angle(I) *
(180)/pi(180)/piI_mag = abs(I)I_mag = abs(I)VR = I*RVR = I*RVL = I*XLVL = I*XLVC = I*XCVC = I*XCVR + (VL + VC)% e = 10 voltVR + (VL + VC)% e = 10 volt
VR =VR = 8.0000 - 4.0000i8.0000 - 4.0000iVL =VL = 3.0000 + 6.0000i3.0000 + 6.0000iVC =VC = -1.0000 - 2.0000i-1.0000 - 2.0000i>> VR + (VL + VC)>> VR + (VL + VC)ans =ans = 1010
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 1818
SummarySummary
Managing Variables and WorkspaceManaging Variables and Workspace MATLAB Commands for Working with MATLAB Commands for Working with
Files and the Operating SystemFiles and the Operating System Controlling Command WindowsControlling Command Windows Date and Time FunctionsDate and Time Functions Special Variables and ConstantsSpecial Variables and Constants
February 7, 2005February 7, 2005 Lecture 7 - By Paul LinLecture 7 - By Paul Lin 1919
Question?Question?
AnswersAnswers
Email: [email protected]: [email protected]