22 views

Uploaded by 김상권

- c8-15interessante Voltage Sags and Swell Psim
- Gui
- ac anlysis
- MATLAB Tutorial
- syllabus_en_21_04_09
- Chapter 13 m-files.docx
- APPENDIX D - MATLAB’S GUI TOOLS TUTORIAL
- Lab06 Buck Converter
- New Control Teq for Upqc111
- Matlab - Thevenin Torque Speed
- File handling,GUIs and Neural networks in matlab-an introduction
- Gui With Matlab
- feedback control system
- Matlab GUI Tutorial
- SU-Stepper Motor System
- 15 Seater Commuter Aircraft
- Matlab Coding for NR Method of an IEEE 9 Bus System
- power system stability
- Book - Aircraft Flight Dynamics, Control and Simulation Using Matlab and Simulink - Singgih Satrio Wibowo - 2007
- Power Systems With MATLAB

You are on page 1of 40

Numerical Computing with .

Byoung-Jo CHOI, PhD University of Incheon

References

Mastering MATLAB 7, Duane Hanselman and Bruce Littlefield, Pearson/Prentice Hall, 2005 Numerical Computing with MATLAB, Cleve Moler, MathWorks MATLAB7, , 2009 MATLAB: An Introduction with Applications, Amos Gilat, John Wiley & Sons, Inc., 2004

Graphics and GUIs with MATLAB, 3rd Ed, Patrick Marchand and O. Thomas Holland, Chapman & Hall/CRC, 2003

Script M-Files

Numerical Computing with . MATLAB for Scientists and Engineers

Write simple script m-files using the editor, Get user inputs and print the formatted results, Give explanations on your scripts using comments, Use cell mode for efficient coding and evaluation, Create a simple dialogue window, Save and Load data to/from MATLAB data file, text file as well as Excel files Use timer to perform repeated action

Text file comprised of a series of MATLAB commands The file name ends with .m, hence m-file. MATLAB interprets the lines in a script m-file. Example

calc_price.m

1: 2: 3: 4: 5:

% Calculate the total price nItem = input('Enter the number of items:'); uPrice = input('Enter the unit price:'); tPrice = nItem * uPrice; fprintf('The total price is %d.\n', tPrice );

5

Using Toolbar

Using Menubar

Popup Menu

10

beep echo on

echo off

pause

pause(5) sec

waitforbuttonpress

Act silently. Default mode. number input

price = input('Enter the Unit Price: '); fprintf('The price is %d.\n', price * 20 );

name = input('Enter your name: ','s'); disp(name); keyboard

string input

Gives control to keyboard. Debug Mode Go into k>> mode Type R-E-T-U-R-N (5 characters) to exit.

11

greetings_input.m

% Get user inputs using command line name = input('Your name: ','s'); age = input('Your age: '); fprintf( ['Hello, %s!' ... ' You will be %d years old next year\n'], ... name, age+1); %% fprintf( 'Press key to continue..'); pause today1 = date; fprintf( '\nToday is %s.\n', today1 );

12

greetings_dlg.m %% Get user inputs using dialog prompt = {'Your name', 'Your age:'}; dlg_title = 'Greetings'; num_lines = 1; def = {'Sam','21'}; answer = inputdlg (prompt,dlg_title,num_lines,def); name = answer{1}; age = str2num (answer{2}); msg = sprintf( 'Hello, %s! You will be %d years old next year\n', name, age+1); h = msgbox (msg, 'Greetings'); uiwait (h) today1 = date; msg = sprintf( '\nToday is %s.\n', today1 ); h = msgbox (msg, 'Greetings');

13

errordlg

helpdlg

questdlg

listdlg

warndlg

various_dlgs.m

14

Comments

Line comments

% % % % This m-file demonstrates filtering operation of FIR designed for removing a tone noise. Refer to Book1 for the exact algorithm Three 2-R plot will be drawn.

Block comments

%{ This m-file demonstrates filtering operation of FIR designed for removing a tone noise. Refer to Book1 for the exact algorithm }% Useful for commenting out a block of code temporarily for debugging.

15

Commenting Out

Ctrl+R for commenting out the selection Ctrl+T for un-commenting out the selection

16

Code Cells

%% Initializing Data Structure Fs = 1440; % Sampling frequency Ts = 1 / Fs; % Sampling Time F0 = 2.4e3; % Carrier frequency %% Generate Time Domain Signal t = 0:Ts:2; s = sin(2*pi*F0*t);

%% Plot the Signal plot(t,s); Code Cell 1

Code Cell 2

Code Cell 3

17

18

Ctrl

Shift

Enter

Evaluate the cell and advance to the next cell. Evaluate the current cell.

+

Ctrl

Enter

19

plot_cosine.m

Change the value near the cursor and execute the cell.

20

disp

disp(name of a variable) or disp('text as string')

disp_demo.m

n = [8 1 6] disp(n) % show the values of n disp('Magic Numbers') % just text disp(['The numbers are: ' num2str(n)]) % text and No's >> disp_demo n = 8 1 6 8 1 6 Magic Numbers The numbers are: 8

6

21

fprintf

fprintf('text') or fprintf('format',arg1, arg2,..)

fprintf_demo.m

n = [8 1 6]; fprintf( '%2d %2d %2d\n', n ); fprintf('Magic Numbers\nDo Exist!\n') % just text fprintf('The numbers are %d, %d and %d.\n', n) >> fprintf_demo 8 1 6 Magic Numbers Do Exist! The numbers are 8, 1 and 6. \n \t %d %x %f %*d new line horizontal tab decimal integer hexadecimal floating point field width, ..

22

times2_table.m

n = (1:9)'; times2 = [ 2*ones(9,1) n 2*n ]; fprintf('%d x %d = %2d\n', times2') 2 2 2 2 2 2 2 2 x x x x x x x x 2 3 4 5 6 7 8 9 = = = = = = = = 4 6 8 10 12 14 16 18

2 3 6

2 4 8

2 5 10

2 6 12

2 7 14

2 8 16

2 9 18

23

format_demo.m

fprintf('%4d %d %5.1f %-5.1f %+5.1f\n', ... 2, 2, pi, pi, pi ); fprintf('%4d %d %5.1f %-5.1f %+5.1f\n', ... -2, -2, -pi, -pi, -pi ); >> format_demo 2 2 3.1 3.1 -2 -2 -3.1 -3.1

+3.1 -3.1

2 2

2 2

3 -

. 3

1 . 1

. -

1 3 . 1

+ 3 -

. 3

1 . 1

24

times2_table_file.m

n = (1:9)'; times2 = [ 2*ones(9,1) n 2*n ]; fid = fopen('times2.txt','w'); fprintf(fid, '%d x %d = %2d\n', times2'); fclose(fid);

fclose()

times2.txt

25

save and load into/from MATLAB data file save save mydata

save mydata var1 var2 ... save mydata var3 -append save ascii mydata.txt var1

load

26

xlsread

a = xlsread('simple.xlsx',-1)

27

xlsread

a = xlsread('simple.xlsx')

a = xlsread('simple.xlsx','Sheet1','A3:B4')

28

xlswrite

xlswrite_demo.m

% Excel write demo % Write to the first sheet beginning from A1 xlswrite('magic.xlsx', magic(4)); % Write to a new sheet, 'Magic5', beginning from A1 xlswrite('magic.xlsx', magic(5), 'Magic5'); % Write to 'Sheet2' beginning from A1 xlswrite('magic.xlsx', magic(6), 2); % Write to 'Sheet3' beginning from B2 xlswrite('magic.xlsx', magic(7), 3, 'B2');

29

Timer

repeated_hello.m

StartDelay

say_hello.m

Period

Period

Period

Try timer_demo.m!!

30

Timer Demo

timer_demo.m % Timer demonstration ans = inputdlg('Period in seconds', ... 'Greeting Man Timer',1,{'3'}); period = str2double(ans{1}); t = timer('TimerFcn','say_hello','StartDelay',1, ... 'ExecutionMode','fixedDelay','Period', period); start(t); %% Listen to the voice for a while. h = msgbox('Do you want to stop the timer?' , ... 'Stop Timer'); uiwait(h); stop(t) delete(t)

31

MATLAB

matlabrc.m finish.m q='Sure?'; b=questdlg(q,'Exit Request','Yes','No','No'); switch b case 'No; quit cancel; end

edit startupsav.m

32

Write a script file, 'ifactor.m', which gets a number from user and prints the number as a product of the prime factors. (Hint: factor)

>> ifactor Prime Factoring v1.0 Enter a positive integer:30 30 = 1 x 2 x 3 x 5 >> ifactor Prime Factoring v1.0 Enter a positive integer:40 40 = 1 x 2 x 2 x 2 x 5

33

Solution 1

Script

ifactor.m

34

Write a script file, 'ifactor2.m', which gets a number from user using a dialog box and prints the number as a product of the prime factors at a message box. [Hint: inputdlg(), msgbox()]

35

Solution 2

ifactor2.m

36

Write a script file, 'icalc.m', which prints the mean and the variance of the data in 'marks.xlsx'. [Hint: mean(), var()]

37

Solution 3

Script

icalc.m

38

Notes

39

Notes

40

- c8-15interessante Voltage Sags and Swell PsimUploaded byjefao_MMA
- GuiUploaded bydhanushka
- ac anlysisUploaded bymursri
- MATLAB TutorialUploaded bykarisnda
- syllabus_en_21_04_09Uploaded bySoumil Sarkar
- Chapter 13 m-files.docxUploaded bysantosh_raju
- APPENDIX D - MATLAB’S GUI TOOLS TUTORIALUploaded byRifqi Ibnu Sofyan
- Lab06 Buck ConverterUploaded byHassan Farouk
- New Control Teq for Upqc111Uploaded byIndra Saki
- Matlab - Thevenin Torque SpeedUploaded byOkiPetrus Hutauruk LumbanBaringin
- File handling,GUIs and Neural networks in matlab-an introductionUploaded bypowerextreme
- Gui With MatlabUploaded bygyuvaraj10
- feedback control systemUploaded byMuhammad Saeed
- Matlab GUI TutorialUploaded byBaher Mawlawi
- SU-Stepper Motor SystemUploaded byarijkeii
- 15 Seater Commuter AircraftUploaded byBala Abinesh
- Matlab Coding for NR Method of an IEEE 9 Bus SystemUploaded byNandhini Botta
- power system stabilityUploaded bykjfen
- Book - Aircraft Flight Dynamics, Control and Simulation Using Matlab and Simulink - Singgih Satrio Wibowo - 2007Uploaded bySinggih Satrio Wibowo
- Power Systems With MATLABUploaded byJuan Alex Arequipa Checa
- Understanding Generator Power System StabilizerUploaded byRamakrishna
- 8411Uploaded bySharmiladevy Prasanna
- M.E.(Power Electronics and DrivesUploaded bykrishnareddy_chintala
- 4989Uploaded byKamariah Sahalihudin
- MATLAB-Based Transient Stability Analysis of a Power SystemUploaded bysamarahmad20084798
- Transient Stability Analysis of Multi-machine SystemUploaded byMd Khalid Akhtar
- LOAD FLOWUploaded byRaja Shekher
- 5-Unsymmetrical Fault AnalysisUploaded byvirenpandya

- jvm-paramsUploaded byEzequiel Gaggioli
- Lmis SpecialUploaded byJames Dube
- iot essay to uploadUploaded byapi-346907346
- Grails SpringUploaded bynaaneninna
- CH2Uploaded byAmirul Mohamad
- PCS-978_X_Instruction Manual_EN_Overseas General_X_R2.05_(EN_YJBH5100.0086.0206).pdfUploaded byDoan Bao
- 39040038 Datasync Doku v21 EnUploaded byOmid Gh
- Generate OpenVPN CertificateUploaded byIndra Gunawa
- E-Commerce ArchitectureUploaded byVinay Korekar
- Rg Certified Development Lifecycle and Deployment DesignerUploaded byPeter Rodriguez
- Linux Servers ConfigurationsUploaded byFredrick Omondi Jaber
- USB Installation GuideUploaded bygammax2x
- DIY Inkjet PrinterUploaded byOrhan Şimşek
- 1 MFC2000-3A High-speed Bus Power Transfer System Users'ManualUploaded byImaad Al-Mutawakkil
- Combo FixUploaded byDuane Hall
- Amplitude Modulation (Part2)Uploaded byRyan Anthony Andal
- wipro.com+1Networking LAN WANL1.1Uploaded byJatin Garg
- Gratisexam.com Oracle.pass4sure.1z0 807.v2015!04!08.by.pedro.104qUploaded byBajjiVara
- Introduction to Electronics ComponentsUploaded byIsradani Mj
- Protecting Against Reverse Polarity Methods ExaminedUploaded bytongai
- COBOL_CDC[1]Uploaded byLalith Kumar
- Mercury Install Man 1Uploaded bydjrob95
- JBoss Enterprise Application Platform 6.2 Installation Guide en USUploaded bychakrikolla
- Information Technology 2006 Sem IIIUploaded byJinu Madhavan
- Microsoft VB6 TutorialsUploaded byPartha S.Saha
- atm_prodUploaded byChakravarthi Chittajallu
- GNS3Uploaded byxuankhanh333
- IG41M-M7S G41D3Uploaded byFlavianoSilva
- Hardware Basics Inside the Box 1204645844884954 4Uploaded byJess Cuenco
- Why-BMS-Not-PLCUploaded byBashar Hasham