INC 151 Electrical Engineering So3ware Prac6ce Lecture #2 - - PowerPoint PPT Presentation

inc 151 electrical engineering so3ware prac6ce
SMART_READER_LITE
LIVE PREVIEW

INC 151 Electrical Engineering So3ware Prac6ce Lecture #2 - - PowerPoint PPT Presentation

INC 151 Electrical Engineering So3ware Prac6ce Lecture #2 Scrip M-file&M-file func6ons Assoc. Prof. Dr. Benjamas PanomruHanarug benjamas.pan@kmuH.ac.th


slide-1
SLIDE 1

INC ¡151 ¡Electrical ¡Engineering ¡ So3ware ¡Prac6ce ¡

Lecture ¡#2 ¡Scrip ¡M-­‑file&M-­‑file ¡func6ons ¡ ¡

  • Assoc. ¡Prof. ¡Dr. ¡Benjamas ¡PanomruHanarug ¡

benjamas.pan@kmuH.ac.th ¡

1 ¡ INC ¡151 ¡ BP ¡

slide-2
SLIDE 2

Colon ¡symbol ¡(:) ¡

  • Generate ¡a ¡row ¡vector ¡with ¡unit ¡increment ¡
  • Example: ¡t1 ¡= ¡1:6 ¡

¡t2 ¡= ¡3:-­‑0.5:1 ¡ ¡t3 ¡= ¡[(0:2:10); ¡(5:-­‑0.2:4)]; ¡

2 ¡ INC ¡151 ¡ BP ¡

slide-3
SLIDE 3

Line ¡space ¡ ¡

  • Linspace(i_value, ¡f_value, ¡np) ¡
  • Logspace(i_value, ¡f_value, ¡np) ¡
  • Example: ¡t4=linspace(2,6,8) ¡

¡ ¡ ¡ ¡ ¡t5 ¡= ¡t3(:,4) ¡

3 ¡ INC ¡151 ¡ BP ¡

slide-4
SLIDE 4

Mathema6cal ¡func6ons ¡

  • Abs(x), ¡acos(x), ¡angle(x), ¡asin(x), ¡atan(x), ¡

ceil(x), ¡conj(x), ¡cos(x), ¡exp(x), ¡fix(x), ¡floor(x), ¡ imag(x), ¡log(x), ¡log10(x), ¡real(x), ¡rem(x), ¡ round(x), ¡sin(x), ¡sqrt(x), ¡tan(x) ¡

  • Pi, ¡i, ¡j, ¡inf, ¡NaN, ¡ans ¡ ¡

4 ¡ INC ¡151 ¡ BP ¡

slide-5
SLIDE 5

M-­‑files ¡

  • Script ¡files: ¡ ¡

– Use ¡text ¡editor ¡to ¡write ¡a ¡script ¡ – file ¡can ¡be ¡invokes ¡by ¡entering ¡the ¡name ¡of ¡the ¡m-­‑ file, ¡without ¡extension ¡

  • Func6on ¡files: ¡

– used ¡to ¡create ¡new ¡Matlab ¡func6ons ¡ – Variables ¡are ¡local, ¡not ¡globally ¡operate ¡on ¡the ¡ workspace ¡ – Func6on ¡variable(s) ¡= ¡func6on_name(arguments) ¡

5 ¡ INC ¡151 ¡ BP ¡

slide-6
SLIDE 6

Func6on ¡file ¡example ¡

¡write ¡a ¡func6on ¡file ¡to ¡solve ¡the ¡equivalent ¡ resistance ¡of ¡series-­‑connected ¡resistors, ¡R1, ¡ R2, ¡R3,…,Rn ¡ ¡ ¡func6on ¡req ¡= ¡equiv_sr(r) ¡ ¡ ¡req ¡= ¡sum(r); ¡ ¡usage: ¡a ¡= ¡[10 ¡20 ¡15 ¡16 ¡5]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Rseries ¡= ¡equiv_sr(a) ¡

6 ¡ INC ¡151 ¡ BP ¡

slide-7
SLIDE 7

Rules ¡for ¡wri6ng ¡m-­‑file ¡func6ons ¡

  • The ¡word ¡“func6on” ¡appears ¡as ¡the ¡first ¡word ¡in ¡a ¡

func6on ¡file ¡

  • % ¡sign ¡shows ¡how ¡the ¡func6on ¡is ¡used ¡and ¡

explana6ons ¡for ¡usage ¡

  • Mul6ple ¡inputs/outputs ¡are ¡allowed ¡
  • If ¡a ¡func6on ¡returns ¡more ¡than ¡one ¡value, ¡all ¡values ¡

should ¡be ¡returned ¡as ¡a ¡vector, ¡i.e., ¡

¡ ¡Func6on ¡[mean, ¡variance] ¡= ¡data_in(x) ¡

  • For ¡mul6ple ¡inputs, ¡the ¡func6on ¡statement ¡must ¡list ¡

the ¡input ¡arguments ¡

¡ ¡Func6on ¡[mean, ¡variance] ¡= ¡data(x,n) ¡

7 ¡ INC ¡151 ¡ BP ¡

slide-8
SLIDE 8

Exercises ¡

  • The ¡voltage ¡across ¡a ¡resistance ¡is ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

and ¡the ¡power ¡dissipated ¡in ¡resistor ¡R ¡is ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡. ¡If ¡R=10 ¡Ohms ¡and ¡the ¡current ¡is ¡ ¡ ¡ ¡ ¡ ¡ increased ¡from ¡0 ¡to ¡10A ¡with ¡increments ¡of ¡1A. ¡ Write ¡a ¡func6on ¡named ¡‘exer1.m’ ¡to ¡calculate ¡ current, ¡voltage, ¡and ¡power ¡dissipa6on. ¡ ¡ ¡

  • Write ¡a ¡func6on ¡file ¡named ¡‘exer2.m’ ¡to ¡

calculate ¡the ¡equivalent ¡resistance ¡of ¡n ¡ parallel ¡connected ¡resistors ¡

8 ¡ INC ¡151 ¡ BP ¡

) ( ) ( t Ri t v = ) ( ) (

2 t

Ri t P =