Download presentation
Presentation is loading. Please wait.
1
Problem Solving with MATLAB
CPET 190 Lecture 7 Problem Solving with MATLAB Paul Lin 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 February 7, 2005 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 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 February 7, 2005 Lecture 7 - By Paul Lin
5
7-1 Managing Variables and Workspace
Example 7-1: Output 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 Command Purpose 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 February 7, 2005 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 .. C:\Courses\CPET190 >> dir Mlin_CPET190 Exs Quiz Lectures MATLABExs February 7, 2005 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 February 7, 2005 Lecture 7 - By Paul Lin
9
7-4 Time and Date Functions
Function Purpose 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 February 7, 2005 Lecture 7 - By Paul Lin
10
7-4 Time and Date Functions
Example 7-3: >> date ans = 03-Oct-2004 >> now 7.3222e+005 >> datestr(now) 03-Oct :47:29 >> help datestr 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 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 * 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 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 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 tic; for i =1:1000 num_array = inv(rand(30)); end toc elapsed_time = 0.3610 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 Total impedance Z Voltage and current across each components 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 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 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) % pi % pi % = ---- % theta_degree theta theta_degree = (180*theta)/pi % degree = pi mag_Z = abs(Z) 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 = i VL = i VC = i >> VR + (VL + VC) ans = 10 February 7, 2005 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 February 7, 2005 Lecture 7 - By Paul Lin
19
Question? Answers February 7, 2005 Lecture 7 - By Paul Lin
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.