inc 151 electrical engineering so3ware prac6ce
play

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


  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 ¡ INC ¡151 ¡ BP ¡ 1 ¡

  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)]; ¡ INC ¡151 ¡ BP ¡ 2 ¡

  3. Line ¡space ¡ ¡ • Linspace(i_value, ¡f_value, ¡np) ¡ • Logspace(i_value, ¡f_value, ¡np) ¡ • Example: ¡t4=linspace(2,6,8) ¡ ¡ ¡ ¡ ¡ ¡t5 ¡= ¡t3(:,4) ¡ INC ¡151 ¡ BP ¡ 3 ¡

  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 ¡ ¡ INC ¡151 ¡ BP ¡ 4 ¡

  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) ¡ INC ¡151 ¡ BP ¡ 5 ¡

  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) ¡ INC ¡151 ¡ BP ¡ 6 ¡

  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) ¡ INC ¡151 ¡ BP ¡ 7 ¡

  8. Exercises ¡ • The ¡voltage ¡across ¡a ¡resistance ¡is ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ v ( t ) Ri ( t ) = and ¡the ¡power ¡dissipated ¡in ¡resistor ¡R ¡is ¡ ¡ 2 t ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡. ¡If ¡R=10 ¡Ohms ¡and ¡the ¡current ¡is ¡ ¡ ¡ ¡ ¡ ¡ P ( t ) Ri ( ) = 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 ¡ INC ¡151 ¡ BP ¡ 8 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend