Presentation is loading. Please wait.

Presentation is loading. Please wait.

February 7, 2005 Lecture 7 - By Paul Lin 1 CPET 190 Lecture 7 Problem Solving with MATLAB Paul Lin

Similar presentations


Presentation on theme: "February 7, 2005 Lecture 7 - By Paul Lin 1 CPET 190 Lecture 7 Problem Solving with MATLAB Paul Lin"— Presentation transcript:

1 February 7, 2005 Lecture 7 - By Paul Lin 1 CPET 190 Lecture 7 Problem Solving with MATLAB Paul Lin http://www.etcs.ipfw.edu/~lin

2 February 7, 2005 Lecture 7 - By Paul Lin 2 Lecture 7: MATLAB Built-In Functions 7-1 Managing Variables and Workspace 7-2 MATLAB Commands for Working with Files and the Operating System 7-3 Controlling Command Window 7-4 Time and Date Functions 7-5 Special Variables and Constants

3 February 7, 2005 Lecture 7 - By Paul Lin 3 7-1 Managing Variables and Workspace Removing Unneeded Variables Removing Unneeded Variables who- List current variableswho- List current variables whos- List current variables, long formwhos- List current variables, long form clear- Clear variables and functionsclear- Clear variables and functions Creating New Variables Creating New Variables length- Length of vectorlength- Length of vector size- Size of matrixsize- Size of matrix Saving to and Retrieving from Disk Files Saving to and Retrieving from Disk Files load - Retrieve variables from disksload - Retrieve variables from disks save- Save workspace variables to disksave- Save workspace variables to disk pack- Consolidate workspace memorypack- Consolidate workspace memory Workspace display Workspace display disp- Display matrix or textdisp- Display matrix or text fprintffprintf

4 February 7, 2005 Lecture 7 - By Paul Lin 4 7-1 Managing Variables and Workspace (continue) Example 7-1: %cpet190_ex7_1.m F = 4000; T = 1/F; dt = 0.01*T; t = 0:dt:2*T; e1 = 5*sin(2*pi*F*t); len = length(e1) size_e1 = size(e1) e2 = zeros(size(e1)); num = rand(2); fprintf('The random number array is %g\n', num); fprintf('\n'); disp('The random numbers'); disp(num); whos save ex7_1 clear load ex7_1 whos

5 February 7, 2005 Lecture 7 - By Paul Lin 5 7-1 Managing Variables and Workspace Example 7-1: Output Example 7-1: Output

6 February 7, 2005 Lecture 7 - By Paul Lin 6 7-2 Working with Files and OS The commands for working with files and operating system The commands for working with files and operating system CommandPurpose pwdShow present working directory cdChange current working directory dirDirectory listing (Window OS) lsDirectory Listing (Unix/Linux OS) deleteDelete file diarySave text of MATLAB session !Execute OS commands

7 February 7, 2005 Lecture 7 - By Paul Lin 7 7-2 Working with Files and OS Example 7-2 Example 7-2 >> pwd ans = C:\Courses\CPET190\matlabex1 90 >> cd.. >> pwd ans = C:\Courses\CPET190 >> dir. Mlin_CPET190.. Exs QuizLectures MATLABExs Example 7-2 (cont.) Example 7-2 (cont.) >> diary >> diary off >> diary on >> !time The current time is: 11:31:05.02 Enter the new time: Enter the new time: Open diary file using MATLAB M-file editor, MS NOTEPAD, or MS Word help help

8 February 7, 2005 Lecture 7 - By Paul Lin 8 7-3 Controlling the Command Window clcClear command window homeSend cursor home (upper-left corner of command window) formatSet output format moreControl paged output in command window echoEcho on/off commands inside script files, for debugging purposes

9 February 7, 2005 Lecture 7 - By Paul Lin 9 7-4 Time and Date Functions FunctionPurpose dateCalendar clockSystem clock; accuracy 1/100th of a second nowCurrent date and time as serial date number datestrConvert a serial date number into the common date/time, save as a string cputimeHow many seconds the MATLAB session is active; accuracy 1/100 th of second etimeElapsed time function ticStart watch timer function tocStop watch timer function

10 February 7, 2005 Lecture 7 - By Paul Lin 10 7-4 Time and Date Functions Time and Date Functions Time and Date Functions Example 7-3: >> date ans = 03-Oct-2004 >> now ans = 7.3222e+005 7.3222e+005 >> datestr(now) ans = 03-Oct-2004 12:47:29 >> help datestr

11 February 7, 2005 Lecture 7 - By Paul Lin 11 7-4 Time and Date Functions (continue) CLOCK Current date and time as date vector. CLOCK returns a six element date vector containing the CLOCK 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 element The 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: >> clock ans = 1.0e+003 * 1.0e+003 * 2.0040 0.0100 0.0030 0.0120 0.0410 0.0071 2.0040 0.0100 0.0030 0.0120 0.0410 0.0071

12 February 7, 2005 Lecture 7 - By Paul Lin 12 7-4 Time and Date Functions (continue) Example 7-4 measuring program execution time (accuracy – 1/100 th of a second) using cputime function Example 7-4 measuring program execution time (accuracy – 1/100 th of a second) using cputime function t1_1 = cputime; for i =1:1000 num_array = inv(rand(30)); num_array = inv(rand(30));end t1_2 = cputime; time_1000 = t1_2 - t1_1 time_once = time_1000/1000 time_1000 = 0.3610 0.3610 time_once = 3.6100e-004 3.6100e-004

13 February 7, 2005 Lecture 7 - By Paul Lin 13 7-4 Time and Date Functions Example 7-5 measuring program execution time (accuracy – 1/100 th of a second) using tic and toc functions Example 7-5 measuring program execution time (accuracy – 1/100 th of a second) using tic and toc functions tic; for i =1:1000 num_array = inv(rand(30)); end toc elapsed_time = 0.3610 0.3610

14 February 7, 2005 Lecture 7 - By Paul Lin 14 7-5 Special Variables and Constants Example 7-6: AC circuit calculation using complex numbers. A RLC circuit is shown on this slide, find a) Total impedance Z b) Voltage and current across each components

15 February 7, 2005 Lecture 7 - By Paul Lin 15 7-5 Special Variables and Constants (continue) Example 7-6: Analysis: Domain knowledge XL = 2πfL, where L is the inductance in Henry, f is the frequency of ac source XL = 2πfL, where L is the inductance in Henry, f is the frequency of ac source XC = 1/(2 πfC), where C is the capacitance in Farard XC = 1/(2 πfC), where C is the capacitance in Farard Z = R + j(XL – XC) -- total impedance, where j shows the imaginary component of a complex number Z = R + j(XL – XC) -- total impedance, where j shows the imaginary component of a complex number I = E/Z, total current I = E/Z, total current VR = I*R, voltage drop across resistor VR = I*R, voltage drop across resistor VL = I*XL, voltage drop across the inductor VL = I*XL, voltage drop across the inductor VC = I*XC voltage drop across the capacitor VC = I*XC voltage drop across the capacitor

16 February 7, 2005 Lecture 7 - By Paul Lin 16 7-5 Special Variables and Constants (continue) Example 7-6: MATLAB Program %RLC_1.m f = 60; R = 8; % Peak value of the sine wave e = 10; XL = j*6; XC = -j*2; Z = R + (XL+XC) theta = angle(Z) % 0.4636 pi % 180 pi % ----- = ---- % theta_degree theta theta_degree = (180*theta)/pi % 26.5615 degree = 0.4636 pi mag_Z = abs(Z)

17 February 7, 2005 Lecture 7 - By Paul Lin 17 7-5 Special Variables and Constants (continue) Example 7-6: MATLAB Program (cont.) %RLC_1.m% I = e/Z I_thea_degree = angle(I) * (180)/pi I_mag = abs(I) VR = I*R VL = I*XL VC = I*XC VR + (VL + VC)% e = 10 volt VR = 8.0000 - 4.0000i 8.0000 - 4.0000i VL = 3.0000 + 6.0000i 3.0000 + 6.0000i VC = -1.0000 - 2.0000i -1.0000 - 2.0000i >> VR + (VL + VC) ans = 10 10

18 February 7, 2005 Lecture 7 - By Paul Lin 18 Summary Managing Variables and Workspace Managing Variables and Workspace MATLAB Commands for Working with Files and the Operating System MATLAB Commands for Working with Files and the Operating System Controlling Command Windows Controlling Command Windows Date and Time Functions Date and Time Functions Special Variables and Constants Special Variables and Constants

19 February 7, 2005 Lecture 7 - By Paul Lin 19 Question? Answers Email: lin@ipfw.edu


Download ppt "February 7, 2005 Lecture 7 - By Paul Lin 1 CPET 190 Lecture 7 Problem Solving with MATLAB Paul Lin"

Similar presentations


Ads by Google