1 / 19

CPET 190

CPET 190. Lecture 7 Problem Solving with MATLAB Paul Lin http://www.etcs.ipfw.edu/~lin. 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

Download Presentation

CPET 190

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPET 190 Lecture 7 Problem Solving with MATLAB Paul Lin http://www.etcs.ipfw.edu/~lin 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 Lecture 7 - By Paul Lin

  3. 7-1 Managing Variables and Workspace • Removing Unneeded Variables • who - List current variables • whos - List current variables, long form • clear - Clear variables and functions • Creating New Variables • length - Length of vector • size - Size of matrix • Saving to and Retrieving from Disk Files • load - Retrieve variables from disks • save - Save workspace variables to disk • pack - Consolidate workspace memory • Workspace display • disp - Display matrix or text • fprintf 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 Lecture 7 - By Paul Lin

  5. 7-1 Managing Variables and Workspace • Example 7-1: Output Lecture 7 - By Paul Lin

  6. 7-2 Working with Files and OS • The commands for working with files and operating system CommandPurpose pwd Show present working directory cd Change current working directory dir Directory listing (Window OS) ls Directory Listing (Unix/Linux OS) delete Delete file diary Save text of MATLAB session ! Execute OS commands Lecture 7 - By Paul Lin

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

  8. 7-3 Controlling the Command Window clc Clear command window home Send cursor home (upper-left corner of command window) format Set output format more Control paged output in command window echo Echo on/off commands inside script files, for debugging purposes Lecture 7 - By Paul Lin

  9. 7-4 Time and Date Functions FunctionPurpose date Calendar clock System clock; accuracy 1/100th of a second now Current date and time as serial date number datestr Convert a serial date number into the common date/time, save as a string cputime How many seconds the MATLAB session is active; accuracy 1/100 th of second etime Elapsed time function tic Start watch timer function toc Stop watch timer function Lecture 7 - By Paul Lin

  10. 7-4 Time and Date Functions • Time and Date Functions Example 7-3: >>date ans = 03-Oct-2004 >> now ans = 7.3222e+005 >> datestr(now) ans = 03-Oct-2004 12:47:29 >> help datestr 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 current time and date in decimal form: CLOCK = [year month day hour minute seconds] The first five elements are integers. The seconds element is accurate to several digits beyond the decimal point. FIX(CLOCK) rounds to integer display format. Example: >>clock ans = 1.0e+003 * 2.0040 0.0100 0.0030 0.0120 0.0410 0.0071 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 t1_1 = cputime; for i =1:1000 num_array = inv(rand(30)); end t1_2 = cputime; time_1000 = t1_2 - t1_1 time_once = time_1000/1000 time_1000 = 0.3610 time_once = 3.6100e-004 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 tic; for i =1:1000 num_array = inv(rand(30)); end toc elapsed_time = 0.3610 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 • Total impedance Z • Voltage and current across each components 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 • 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 • I = E/Z, total current • VR = I*R, voltage drop across resistor • VL = I*XL, voltage drop across the inductor • VC = I*XC voltage drop across the capacitor 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) 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 VL = 3.0000 + 6.0000i VC = -1.0000 - 2.0000i >> VR + (VL + VC) ans = 10 Lecture 7 - By Paul Lin

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

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

More Related