1 MATLAB 해설 수원대학교 전자공학과 Matlab 이란? l Matrix laboratory를 위한 행렬연산 프로그램 l 과학 공학 계산을 위한 프로그램 언어 l 데이터 처리 및 시뮬레이션 MATLAB의 대표적인 파일 l *.mat – Workspace의 데이터를 저장하는 파일 l *.mdl – Simulink를 저장한 파일 l *.m – Function, Script를 저장함 – 자체가 실행파일의 역할을 함 기본 명령 l help “함수 이름” l pwd – present working directory를 보여준다. l ls – list, 현 폴더의 파일들을 보여 준다. l who – 현재 workspace 상의 모든 변수를 보여 준다. l simulink – simulink의 실행 Matlab Variables Names l Begin with one of a–z or A–Z l Have remaining characters chosen from a–z, A–Z, 0–9, or _ l Have a maximum length of 31 characters l Should not be the name of a built-in variable, built-in function, or user-defined function – MySym, mysym %Matlab is case sensitive. Basic 내장 variables 및 숫자 l Ans %답 l Eps % 가장 작은수 l pi = π l Flops % 부동연산의 횟수 l i or j = sqrt(-1) l inf = 무한대 l NaN=숫자가 아님 l Realmin, realmax % 가장 작은(큰) 양 의 실수
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
MATLAB 해설
수원대학교
전자공학과
Matlab 이란?
lMatrix laboratory를위한행렬연산프로그램
l과학공학계산을위한프로그램언어
l데이터처리및시뮬레이션
MATLAB의 대표적인 파일
l *.mat
– Workspace의 데이터를 저장하는 파일
l *.mdl
– Simulink를 저장한 파일
l *.m
– Function, Script를 저장함
–자체가 실행파일의 역할을 함
기본 명령
l help “함수이름”l pwd
– present working directory를보여준다.
l ls– list, 현 폴더의파일들을보여 준다.
l who–현재 workspace 상의모든 변수를보여 준다.
l simulink– simulink의실행
Matlab Variables Names
l Begin with one of a–z or A–Zl Have remaining characters chosen from
a–z, A–Z, 0–9, or _
l Have a maximum length of 31 characters
l Should not be the name of a built-in variable, built-in function, or user-defined function
– MySym, mysym %Matlab is case sensitive.
Basic 내장 variables 및 숫자
l Ans %답l Eps % 가장작은수l pi = πl Flops % 부동연산의횟수l i or j = sqrt(-1)
l inf = 무한대
l NaN=숫자가 아님l Realmin, realmax % 가장 작은(큰) 양의 실수
2
데이터의 format (1)
l format short ↲ % 기본적인수치표현양식! l X=[4/3 1.2345e-6] ↲l
l X = l 1.3333 0.0000 l
l format short e ↲l X ↲l X = l 1.3333e+000 1.2345e-006 l
간단한 계산기능l 1+2-8
– ans=-5
l ans/2– ans=-2.5
l a=10l b=2l c=a/bl pil sin(ans/6)l log(256)l log10(256)l log2(256)l a = 5; b = sin(a), c = cosh(a)
기본 명령
l함수 찾기
– lookfor cosine
l도움말
– help cos
– helpwin(‘cos’)– doc plot
데이터의 format (2)
l format long ↲l X ↲
l X =
l 1.33333333333333 0.00000123450000
l format bank ↲ % 소수 두 번째 자리까지 표시. l X ↲
l X =
l 1.33 0.00 l format rat ↲ % 분수식 표현! l X ↲
l X =
l 4/3 1/810045 l
데이터의 format (3)
l format hex ↲ % 16진법에 의한표현! l X ↲
l X =
l 3ff5555555555555 3eb4b6231abfd271 l
l format compact ↲l % 변수의 값을출력하는 경우발생하는 많은 blank(공백)l % 지워준다. 그래서더많은정보를 screen 또는 window에l % 표현할 수있도록해준다! l X ↲l X = l 1.3333 0.0000
l D = load(’pdxTemp.dat’); m = D(:,1); T = D(:,2:4);
l plot(m,t(:,1),’ro’,m,T(:,2),’k+’,m,T(:,3),’b-’);l xlabel(’Month’);l ylabel(’Temperature ({}^\circ F)’);l title(’Monthly average temperature at PDX’);l axis([1 12 20 100]);
l legend(’High’,’Low’,’Average’,2);
l Line_Color =get(gca,'colororder')
l Line_Type =get(gca,'Linestyleorder')
l set(0,'DefaultAxesColororder',[0 0 0],'DefaultAxesLineStyleOrder','-|-.|--|:')
l set(gca, 'xgrid', 'on', 'gridlinestyle', '-.')
l set(gca,'xtick',[0:0.4:2 2.5 3 3.5 4 4.5 5])
12
l t_label=str2mat('I','II','III','IV','V','VI','VII','VIII','IX','X','XI','XII')
l set(gca,'xticklabel',t_label)
l set(gco,'color','r','Linewidth',15)
l set(gca,'xlim', [min(t) max(t)],'ylim',[-1.5 1.5])
l set(gca,'nextplot','add','xlim',[min(t) max(t)]) ↲ %hold on
l set(gca,'nextplot',‘replcae','xlim',[min(t) max(t)]) ↲ %hold off
special symbols
l h=text(300,.25*exp(-.005*300),...
l '\bullet\leftarrow\fontname{times}0.25{\ite}^{-0.005{\itt}} .at {\itt}=300');
[표3-2] 특수문자표
Plot Marker
13
Plot Lines 종합
Menu의 사용
l line_color=['r','g','b']; l str='y'; l
l t=-2*pi:1/100:2*pi; l y=sin(t); l
l while(1) l if str=='y' l k=menu('선의 색을 결정','red','green','blue'); l plot(t,y,line_color(k)) l R=input('계속하려면 ''y''를 type! :','s'); l str=lower(R); l else l break; l end l end
questdlg 사용
l str=questdlg('계속하기를원합니까?','선택사항','Yes','No','No');
messagebox
l msgbox(message,title,'icon')
l icon– 'error', 'help', 'warn'
File I/O(FPRINTF, SSCANF, FGETL, FGETS,
FREAD, INPUT)
lOpening and closing filesl example : fid =fopen('pen.dat','r')l 'r' : readingl 'w' : wrightingl 'a' : appendingl 'r+' : reading and wrightingl status = fclose(fid);l status = fclose('all');
– ( 0 : successful, -1 : unsuccessful )
14
File I/O
l Reading Binary Data Filesl fread : reads binary data files
l example : fid = fopen('penny.dat', 'r');l A = fread(fid);l status = fclose(fid);
Controlling Position in a File
l Writing Formatted Text Files and Stringsl %e for exponential notation
l %f for fixed point notation
l %g which automatically selects the shorter of %e and %f
l Reading Formatted Text Files and Strings
l %s to match a string
l %d to match a decimal number
l %f to match a floating point value
Matlab Debugger
l dbstop : set breakpoint
l dbclear : remove breakpoint
l dbcont : resume execution
l dbstack : list who called whom
l dbstatus : list all break point
l dbstep : execute one or more lines
l dbtype : list M-file with line numbers
l dbup : change local workspace context
l dbquit : quit debug mode
Example
l dbstop in mfile
l dbstop in mfile at linenumber
l dbtype mfile 1:10 % type from line 1 to line 10
l dbstack % display function call stack
l dbclear all; dbclear in mfile at 4
l dbstatus % lists all breakpoints
Data Symbolic math
l sym– Construct symbolic numbers, variables and objects.
– S = SYM(A) constructs an object S, of class 'sym', from A. If the input argument is a string, the result is a symbolic number or variable. If the input argument is a numeric scalar or matrix, the result is a symbolic representation of the given numeric values.