Upload
thetis
View
63
Download
14
Embed Size (px)
DESCRIPTION
بسم الله الرّحمن الرّحيم. آموزش نرم افزار MATLAB. علی دانش گروه رباتیک دانشگاه پیام نور. چند نکته. امکان کامنت چندخطی (بلوکی) در نسخه 7 %{ %} امکانات دیباگ کردن امکان Cell Mode در Script نویسی (نسخه 7) دموهای جالب و مفید demo. توابع تو در تو ( nested function ). - PowerPoint PPT Presentation
Citation preview
آموزشنرم افزار
MATLABدانش علی
نور پیام دانشگاه رباتیک گروه
حيم الر� حمن الر� الله بسم
نکته چند
) نسخه ) در بلوکی چندخطی کامنت 7امکان%{
%} کردن دیباگ امکانات امکانCell Mode درScript ( نسخه (7نویسی مفید و جالب دموهای
demo
تو ) در تو (nested functionتوابع
ها داده گذاری اشتراک سفارشی توابع ساختن داخلی حالت داشتن با سفارشی توابع ساختن
ها داده گذاشتن اشتراک به
>> type taxDemo.mfunction y = taxDemo(income)% Calculate the tax on income:
AdjustedIncome = income - 6000;y = computeTax;function y = computeTax
% Can see the variable 'AdjustedIncome'
y = 0.28 * AdjustedIncome;end
end
سفارشی توابع ساختن
>> type makefcn.m
function fcn = makefcn(a, b, c)
% Return handle to nested function:
fcn = @parabola;
function y = parabola(x)
% Can see the variables 'a','b', and 'c'
y = a * x .^ 2 + b .* x + c;
end
end
] سفارشی] توابع ادامه
f = makefcn)3, 2, 10(; g = makefcn)0, 5, 25(;
دو ( handleاین متفاوت ) ضرایب متفاوت تابع دو بهرا کدام هر مقدار توان می که کنند می اشاره
دیگر توابع به پارامتر عنوان به یا و کرد محاسبه. کرد منتقل
y = f)2( % = 26 y = g)2( % = 35 minimum = fminbnd)f, -5, 5(; % = -0.3333
] سفارشی] توابع ادامه
ezplot)f(; % Plot f over a range of x
hold on;
plot)2, f)2(, 'd'(; % Plot a marker at )2, f)2((
plot)minimum, f)minimum(, 's'(;
text)minimum, f)minimum( - 2, 'Minimum'(;
h = ezplot)g(;
set)h, 'color', 'red'( % Plot g over a range of x
plot)2, g)2(, 'rd'(; % Plot a marker at )2, g)2((
hold off;
با سفارشی توابع ساختنداخلی حالت داشتن
>> type makecounter.mfunction countfcn = makecounter(initvalue)
currentCount = initvalue;countfcn = @getCounter;function count = getCounter
% Increments the 'currentCount‘currentCount = currentCount + 1;count = currentCount;
endend
] با] سفارشی توابع ادامهداخلی حالت
>> counter1 = makecounter)0(;
>> counter2 = makecounter)10(;
>> counter1 % = 1
>> counter1 % = 2
>> counter2 % = 11
>> counter1 % = 3
>> counter2 % = 12
با متنی فایلهای خواندندلخواه فرمت
load % Variables from .MAT file fread).( % StrRead).( for string fscanf).( % SScanf).( for string fgets).( % newline included fgetl).( % newline NOT included textread).( % Fixed Format textscan).( % Variable Format
).(textscanتابع
فایل فرمت تنظیم خواندن برای متنی فایل کردن باز مقدماتی خطوط خواندن بلوکها خواندن متنی فایل بستن شده خوانده بلوکهای تعداد ها داده بررسی
متنی: فایل test80211.txtمثال
از تشکیل 4بعد داده بلوک یک از فایل مقدمه، خط: باشد می زیر فرمت به که است شده
توضیحات خط دو پارامترm شکل به ها داده p * mجدول
قرار سلولی آرایه در شده خوانده های داده تمام. شود می داده
] مثال] ادامه
fid = fopen)'test80211.txt', 'r'(; % Open text file
% Read strings delimited by a carriage return
InputText = textscan)fid, '%s', 4, 'delimiter', '\n'(;
Intro = InputText{1};
disp)Intro(;
– ] بلوک] خواندن مثال ادامه
: بلوک هر خواندن عنوان خواندن جدول نام خواندن سرستون خواندن ها داده خواندن
– ] کد] مثال ادامهBlock = 1; % Initialize block indexwhile )~feof)fid(( % For each block...
% Read header lineInputText = textscan)fid, '%s', 2, 'delimiter', '\n'(;HeaderLines{Block, 1} = InputText{1};InputText = textscan)fid, 'Num SNR=%f'(; % Read parameter valueNumCols = InputText{1};% Read data block:FormatString = repmat)'%f', 1, NumCols(; % Create format stringInputText = textscan)fid, FormatString, 'delimiter', ','(;% Convert to numerical array from cell:Data{Block, 1} = cell2mat)InputText(;[NumRows, NumCols] = size)Data{Block}(; % Size of table% Read and discard EOB marker )'EOF' in this case(:eob = textscan)fid, '%s', 1, 'delimiter', '\n'(;Block = Block + 1; % Increment block index
end
– ] کد] مثال 2ادامه
fclose)fid(;
NrOfBlocks = Block - 1 % How many blocks ?
% Display Block #9 :
Block = 9;
disp)HeaderLines{Block}(;
disp)['SNR' sprintf)' %d', Data{Block, 1})1, 2:end((](
format short e % Use exponential format
disp)' '(;
disp)Data{Block, 1})2:end, 2:end((;
'* Indoor0' '* SNR Vs test No'
SNR -7 -6 9.0600e-007 6.7100e-007 3.1700e-007 3.5400e-007 2.8600e-007 1.9600e-007 1.4800e-007 7.3400e-007 3.9500e-008 9.6600e-007 7.9600e-007 7.8300e-007 4.0000e-007 8.8100e-007 3.0100e-007 2.9700e-007
بعدی دو نمایشنمودار
XY plot
plotتابع
x = 0:0.05:5;
Y = sin)x .^ 2(;
plot)x, y(;
barتابع
x = -2.9:0.2:2.9;
bar)x, exp)-x .* x((;
stairsتابع
x = 0:0.25:10;
stairs)x, sin)x((;
errorbarتابع
x = -2:0.1:2;
y = erf)x(;
e = rand)size)x(( / 10;
errorbar)x, y, e(;
polarتابع
t = 0:.01:2*pi;
polar)t, abs)sin)2 * t( .* cos)2 * t(((;
stemتابع
x = 0:0.1:4;
y = sin)x .^ 2( .* exp)-x(;
stem)x, y(
فوریه – سری رسم مثال
t = 0:.1:10;
y = sin)t(;
plot)t, y(;
y = sin)t( + sin)3 * t( / 3 + sin)5 * t( / 5 + sin)7 * t( / 7 + sin)9 * t( / 9;
plot)t, y(;
– ] سری] رسم مثال ادامهفوریه
t = 0:.02:3.14;y = zeros)10, length)t((;x = zeros)size)t((;for k=1:2:19 x = x + sin)k * t( / k; y))k + 1( / 2, :( = x;endplot)y)1:2:9, :('(title)'The building of a square wave: Gibbs''
effect'(
– ] سری] رسم مثال ادامهفوریه
surf)y(;
shading interp
axis off ij
بعدی سه نمایشنمودار
XYZ plot
بعدی سه رسم توابعz=peaks)25(;%%%mesh)z(;colormap)hsv(surf)z(;colormap)jet(;surfl)z(;shading interp;colormap)pink(;contour)z,16(;colormap)hsv(
تصویر مقدمه
و تصاویرماتریسها
تصویر انواع
Black & White )B/W( Grayscale Colorful
Color Index )PALETTE( Color Value
RGB )additive( CMYK )subtractive( HSI )HSV( )Hue Saturation Intensity( Ia*b* …
).(colormapو ).(imageتابع
X = spiral)8(;
image)X(;
colormap)gray(;
%%%
colormap)hsv(;
%%%
colormap)hot(;
43 44 45 46 47 48 49 5042 21 22 23 24 25 26 5141 20 7 8 9 10 27 5240 19 6 1 2 11 28 5339 18 5 4 3 12 29 5438 17 16 15 14 13 30 5537 36 35 34 33 32 31 5664 63 62 61 60 59 58 57
).(rgbplotتابع
: رنگ نگاشت نمودار مشاهده برایrgbplot)S(
تصویر نوشتن و خواندن توابع
imread