CPET 190 - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

CPET 190

Description:

pack - Consolidate workspace memory. Workspace display. disp ... CLOCK returns a six element date vector containing the. current time and date in decimal form: ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 20
Provided by: melis76
Category:
Tags: cpet

less

Transcript and Presenter's Notes

Title: CPET 190


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

4
7-1 Managing Variables and Workspace (continue)
  • Example 7-1
  • cpet190_ex7_1.m
  • F 4000
  • T 1/F
  • dt 0.01T
  • t 0dt2T
  • e1 5sin(2piFt)
  • 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
7-1 Managing Variables and Workspace
  • Example 7-1 Output

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

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

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

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

10
7-4 Time and Date Functions
  • Time and Date Functions
  • Example 7-3
  • gtgt date
  • ans
  • 03-Oct-2004
  • gtgt now
  • ans
  • 7.3222e005
  • gtgt datestr(now)
  • ans
  • 03-Oct-2004 124729
  • gtgt help datestr

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
  • gtgt clock
  • ans
  • 1.0e003
  • 2.0040 0.0100 0.0030 0.0120
    0.0410 0.0071

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 11000 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
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 11000 num_array
inv(rand(30)) end toc
elapsed_time 0.3610
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

15
7-5 Special Variables and Constants (continue)
  • Example 7-6
  • Analysis Domain knowledge
  • XL 2pfL, where L is the inductance in Henry, f
    is the frequency of ac source
  • XC 1/(2 pfC), 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 IR, voltage drop across resistor
  • VL IXL, voltage drop across the inductor
  • VC IXC voltage drop across the capacitor

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 j6 XC -j2
  • Z R (XLXC)
  • theta angle(Z) 0.4636 pi
  • 180 pi
  • ----- ----
  • theta_degree theta
  • theta_degree (180theta)/pi
  • 26.5615 degree 0.4636 pi
  • mag_Z abs(Z)

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 IR
  • VL IXL
  • VC IXC
  • VR (VL VC) e 10 volt
  • VR
  • 8.0000 - 4.0000i
  • VL
  • 3.0000 6.0000i
  • VC
  • -1.0000 - 2.0000i
  • gtgt VR (VL VC)
  • ans
  • 10

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

19
Question?
  • Answers
  • Email lin_at_ipfw.edu
Write a Comment
User Comments (0)
About PowerShow.com