matlab present

184
MATLAB`s Introductions By S.Mohsen Moosavi Nezhad

Upload: hhpnoman

Post on 03-Jul-2015

2.006 views

Category:

Education


3 download

TRANSCRIPT

Page 1: Matlab present

MATLAB`sIntroductions

By

S.Mohsen Moosavi Nezhad

Page 2: Matlab present

Matlab is a commercial

What is MATLAB?

"Matr ix Laboratory"

Page 3: Matlab present

What is MATLAB?• high-performance software

– Computation– Visualization– Easy-to-use environment.

• high-level language – Data types– Functions– Control flow statements– Input/output– Graphics– Object-oriented programming capabilities

Page 4: Matlab present

MATLAB Parts• Developed Environment• Programming Language• Graphics• Toolboxes• Application Program

Interface

Page 5: Matlab present

Installation GuideI nstal lation

Guide

Step 1: Start the Installer

Page 6: Matlab present

Installation GuideI nstal lation

Guide

Step 2: Enter Identification and License Information

Page 7: Matlab present

Installation GuideI nstal lation

Guide

Step 3: Review the Software Licensing Agreement

Page 8: Matlab present

Installation GuideI nstal lation

Guide

Step 4: Choose Installation Type

Page 9: Matlab present

Installation GuideI nstal lation

Guide

Step 5: Specify Installation Folder and Products

Page 10: Matlab present

Installation GuideI nstal lation

Guide

Step 6: Specify Installation Options (Custom Only)

Page 11: Matlab present

Installation GuideI nstal lation

Guide

Step 7: Confirm Your Choices

Page 12: Matlab present

Installation GuideI nstal lation

Guide

Step 8: Read Product Configuration Notes

Page 13: Matlab present

Installation GuideI nstal lation

Guide

Step 9: Complete the Installation

Page 14: Matlab present

Display WindowsDisplay Windows

Page 15: Matlab present

Show help

Page 16: Matlab present
Page 17: Matlab present
Page 18: Matlab present
Page 19: Matlab present
Page 20: Matlab present

Application of MATLAB in Engineering

• Image Processing• Image Acquisition• Signal processing• Simulinc• wavelet

• Data Acquisition • Partial Differential

Equations• Curve Fitting• Symbolic Mathematics• Plotting

Page 21: Matlab present

Application of MATLAB in Material Engineering

• Image Processing

– Phase Fraction– Grain Size– Nodular Grafit– Coating Thickness– Inclusions– Edit Image

Page 22: Matlab present

Phase Fraction

ساختماني فولدCK45

كربن 57 * درصد0.46=0.008

Page 23: Matlab present

Grain Size

نيوم لومي آ

G=7

Page 24: Matlab present

Nodular Grafit

10

10

20

.درصد كرويت گرافيتهاي كرويدرصد و رنگ80رنگ آبي تعداد ذرات با كرويت بيش از

درصد را نشان65 قرمز تعداد ذرات با كرويت كمتر از .ميدهند

Page 25: Matlab present

Coating Thickness

Page 26: Matlab present

Image Acquisition

اتصال به ميكروسكپ و دريافت: به عنوان مثال تصاوير از ريزساختار مورد نظر

Page 27: Matlab present

Data Acquisition

اتصال به ترموكوپل و دريافت داده ها: به عنوان مثال

Page 28: Matlab present

Curve Fitting & Plotting

Effect of temperature on the reduction kinetics of uncatalyzed concentrate:graphite:lime mixture.

Page 29: Matlab present

Partial Differential Equations

magnetic flux density

مس ي ويژه رسانايي% 100

ضريب تروايي نسبت به خل 1

فركانس هرتز50

10 شعاع استوانه ي مسي سانتي متر

اي، پوسته ثر ا از لي ا مثزمان ي كه جريان

از يك ناوب متاس توانه ي طويل

با سطح مقطع مسي دايروي، عبور ميكن د مورد بررسي

.قرار مي گيرد

Page 30: Matlab present

Toolboxes

Collections of functions to solve problems of several appl ications.

Page 31: Matlab present

Variables• Variable names:

– Must start with a letter– May contain only letters, digits, and the underscore “_”– Matlab is case sensitive, i.e. one & OnE are different

variables.– Matlab only recognizes the first 31 characters in a

variable name.

• Assignment statement:– Variable = number;– Variable = expression;

• Example:>> tutorial = 1234;>> tutorial = 1234tutorial = 1234

NOTE: when a semi-colon ”;” is placed at the end of each command, the result is not displayed.

Page 32: Matlab present

MATLAB BASICSMATLAB BASICSInitializing with Shortcut Expressions

first: increment: last• Colon operator: a shortcut notation used to

initialize arrays with thousands of elements>> x = 1 : 2 : 10;>> angles = (0.01 : 0.01 : 1) * pi;

• Transpose operator: (′) swaps the rows and columns of an array

>> f = [1:4]′;>> g = 1:4; >> h = [ g′ g′ ];

h=

1 12 23 34 4

Page 33: Matlab present

كار در كلس

Page 34: Matlab present

M A TL A B B A SI CSM A TL A B B A SI CS

و به وسيله تركيب first: increment: last ماتريس زير را به كمك عبارت

ماتريس ها توليد كنيد 5 6 7 83 7 11 151 0 -1 -2

Page 35: Matlab present

A=[5:83:4:181:-1:-2];

M A TL A B B A SI CSM A TL A B B A SI CS

A=[ 5:8 ; 3:4:18 ; 1:-1:-2]A =

5 6 7 8 3 7 11 15 1 0 -1 -2

or

Page 36: Matlab present

Changing the data format>> value = 12.345678901234567;

format short → 12.3457format long → 12.34567890123457format short e → 1.2346e+001format long e → 1.234567890123457e+001format short g → 12.346format long g → 12.3456789012346format rat → 1000/81

MATLAB BASICSMATLAB BASICS

Page 37: Matlab present

Calculations at the Command Line

» a = 2;

» b = 5;

» a^b

ans =

32

» x = 5/2*pi;

» y = sin(x)

y =

1

» z = asin(y)

z =

1.5708

Results assigned to “ans” if name not specified

() parentheses for function inputs

Semicolon suppresses screen output

MATLAB as a calculator Assigning Variables

A Note about Workspace:Numbers stored in double-precision floating point format

» -5/(4.8+5.32)^2ans = -0.0488» (3+4i)*(3-4i)ans = 25» cos(pi/2)ans = 6.1230e-017» exp(acos(1))ans = 1

Page 38: Matlab present

The Matrix in MATLAB

4 10 1 6 2

8 1.2 9 4 25

7.2 5 7 1 11

0 0.5 4 5 56

23 83 13 0 10

1

2

Rows (m) 3

4

5

Columns(n)

1 2 3 4 51 6 11 16 21

2 7 12 17 22

3 8 13 18 23

4 9 14 19 24

5 10 15 20 25

A = A (2,4)

A (17)

Rectangular Matrix:Scalar: 1-by-1 arrayVector: m-by-1 array

1-by-n arrayMatrix: m-by-n array

Page 39: Matlab present

» a=[1 2;3 4]

a =

1 2

3 4

» b=[-2.8, sqrt(-7), (3+5+6)*3/4]

b =

-2.8000 0 + 2.6458i 10.5000

» b(2,5) = 23

b =

-2.8000 0 + 2.6458i 10.5000 0 0

0 0 0 0 23.0000

•Any MATLAB expression can be entered as a matrix element

•Matrices must be rectangular.) set undefined variables to zero)

Entering Numeric Arrays

Row separatorsemicolon (;)

Column separatorspace / comma (,)

Use square brackets [ ]

Page 40: Matlab present

Array Subscripting /Indexing

4 10 1 6 2

8 1.2 9 4 25

7.2 5 7 1 11

0 0.5 4 5 56

23 83 13 0 10

1

2

3

4

5

1 2 3 4 51 6 11 16 21

2 7 12 17 22

3 8 13 18 23

4 9 14 19 24

5 10 15 20 25

A =

A(3,1)A(3)

A(1:5,5)A(:,5) A(21:25)

A(4:5,2:3)A([9 14;10 15])

A(1:end,end) A(:,end)A(21:end)’

Page 41: Matlab present

Colon OperatorColon Operator

C = [1,2,5; -1,0,1; 3,2,-1; 0,1,4]

F = C(:, 2:3) = [2,5; 0,1; 2,-1; 1,4]

Page 42: Matlab present

−=

−−

=123

101E

410

123

101

521

C

Colon OperatorColon Operator• Creating new matrices from an existing

matrix

C = [1,2,5; -1,0,1; 3,2,-1; 0,1,4]

E = C(2:3,:) = [-1 0 1; 3 2 -1]

Page 43: Matlab present

Colon OperatorColon Operator• Creating new matrices from an existing

matrixC = [1,2,5; -1,0,1; 3,2,-1; 0,1,4]

G = C(3:4,1:2) = [3,2; 0,1]

=

−−

=10

23G

410

123

101

521

C

Page 44: Matlab present

Matrix Multiplication» a = [1 2 3 4; 5 6 7 8];

» b = ones(4,3);

» c = a*b

c =

10 10 10 26 26 26

[2x4]

[4x3]

[2x4]*[4x3] [2x3]

a(2nd row).b(3rd column)

» a = [1 2 3 4; 5 6 7 8];

» b = [1:4; 1:4];

» c = a.*b

c =

1 4 9 16 5 12 21 32 c(2,4) = a(2,4)*b(2,4)

Array Multiplication

Page 45: Matlab present

Multidimensional Arrays

» A = pascal(4);» A(:,:,2) = magic(4)A(:,:,1) = 1 1 1 1 1 2 3 4 1 3 6 10 1 4 10 20A(:,:,2) = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1» A(:,:,9) = diag([1 1 1

1]);

1 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

Page N

Page 1

0 0 0 0

0 0 0 0

0 0 0 0

0 0 0 0

16 2 3 13

5 11 10 8

9 7 6 12

4 14 15 1

1 1 1 1

1 2 3 4

1 3 6 10

1 4 10 20

The first references array dimension 1, the row.

The second references dimension 2, the column.

The third references dimension 3, The page.

Page 46: Matlab present

A(2,1,2) A(3:4,2:3,1)b=A(3,4,5)b=0c=A(3,3,9)c=1

» A = pascal(4);» A(:,:,2) = magic(4)A(:,:,1) = 1 1 1 1 1 2 3 4 1 3 6 10 1 4 10 20A(:,:,2) = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1» A(:,:,9) =

diag(ones(1,4));

Multidimensional Arrays

Page 47: Matlab present

كار در كلس

Page 48: Matlab present

M A TL A B B A SI CSM A TL A B B A SI CS

.ماتريس زير را به ساده ترين و کوتاه ترين شکل ممکن تعريف کنيد

0 0 0 0

0 0 0 0

0 0 0 0

0 0 0 0

1 2 3 4

1 3 5 7

1 1 1 1

5 4 3 2

1 2 3 4

1 3 5 7

1 1 1 1

5 4 3 2

Page 49: Matlab present

M A TL A B B A SI CSM A TL A B B A SI CS

A(1,:,1)=1:4;A(2,:,1)=1:2:7;A(3,:,1)=1;A(4,:,1)=5:-1:2;A(:,:,3)=A(:,:,1)

0 0 0 0

0 0 0 0

0 0 0 0

0 0 0 0

1 2 3 4

1 3 5 7

1 1 1 1

5 4 3 2

1 2 3 4

1 3 5 7

1 1 1 1

5 4 3 2

Page 50: Matlab present

Matrix Manipulation Functions• zeros: Create an array of all zeros

• ones: Create an array of all ones

• eye: Identity Matrix

• rand: Uniformly distributed random numbers

• diag: Diagonal matrices and diagonal of a matrix

• size: Return array dimensions • fliplr: Flip matrices left-right

• flipud: Flip matrices up and down

• repmat: Replicate and tile a matrix

>> zeros(2,2,4)

ans(:,:,1) = 0 0 0 0ans(:,:,2) = 0 0 0 0ans(:,:,3) = 0 0 0 0ans(:,:,4) = 0 0 0 0

>> ones([2,2,4])

ans(:,:,1) = 1 1 1 1ans(:,:,2) = 1 1 1 1ans(:,:,3) = 1 1 1 1ans(:,:,4) = 1 1 1 1

>> eye(5,7)ans = 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0

>> diag(4,2)ans = 0 0 4 0 0 0 0 0 0

>> zz=ones([2,2,4]);>> a=size(zz);

>> a=[ 1 2 ; 3 4];>> fliplr(a)ans = 2 1 4 3

Page 51: Matlab present

Matrix Manipulation Functions• transpose (’): Transpose matrix • rot90: rotate matrix 90

• tril: Lower triangular part of a matrix

• triu: Upper triangular part of a matrix

• cross: Vector cross product

• dot: Vector dot product

• det: Matrix determinant

• inv: Matrix inverse

• eig: Evaluate eigenvalues and eigenvectors

• rank: Rank of matrix

Page 52: Matlab present

M A TL A B B A SI CSM A TL A B B A SI CS

sumتابع

.جمع مولفه هاي يك ماتريس را محاسبه كرده و برميگرداند حاصلB = sum(A); returns sums along different dimensions of an array.

>> a=[1 2 3 ; 7 5 10]

B = sum(A,dim) sums along the dimension of A specified by scalar dim

>> sum(a)ans = 8 7 13

>> a(:,:,2)=2;>> sum(a,1)

ans(:,:,1) = 8 7 13

ans(:,:,2) = 4 4 4

>> sum(a,2)ans(:,:,1) = 6 22

ans(:,:,2) = 6 6

Page 53: Matlab present

M A TL A B B A SI CSM A TL A B B A SI CS

diffتابع

.تفريق مولفه هاي يك ماتريس را محاسبه كرده و برميگرداند حاصلY = diff(X); calculates differences between adjacent elements of X.

>> z

z(:,:,1) =

6 7 10

z(:,:,2) =

3 4 5

>> a=[1 3 5;7 10 15];>> a(:,:,2)=[1:3;4:2:8];

>> z=diff(a,1);

[X(2)-X(1) X(3)-X(2) ... X(n)-X(n-1)]

Page 54: Matlab present

Y = diff(X,n,dim) is the nth difference function calculated along

the dimension specified by scalar dim.z=diff(a,2);

Y = diff(X,n) applies diff recursively n times, resulting in the

nth difference.

>> z

z(:,:,1) =

1 3

z(:,:,2) =

1 1

>> z=diff(a,2,2)z(:,:,1) = 0 2z(:,:,2) = 0 0

M A TL A B B A SI CSM A TL A B B A SI CS

diffتابع

Page 55: Matlab present

كار در كلس

Page 56: Matlab present

M A TL A B B A SI CSM A TL A B B A SI CS

، به كمك توابع[pi/2 0]را در بازه sin(x)انتگرال عددي تابع

diff و sumمحاسبه كنيد.

.استفاده كنيد 0.001با گام : جهت بدست آوردن نتايج صحيح از عملگر

Page 57: Matlab present

M A TL A B B A SI CSM A TL A B B A SI CS

x=0 : 0.001 : pi/2;y=sin(x);Integ=sum(y(1:end-1).*diff(x))Integ= 0.9987Derivatives=diff(y)./diff(x);

Page 58: Matlab present

Data Types

Page 59: Matlab present

Numeric-Integer

Page 60: Matlab present

Character Arrays (Strings)• Created using single quote delimiter (')

• Each character is a separate matrix element (18 bits of memory per character)

• Indexing same as for numeric arrays

» str = ‘M Moosavi'

str =

M Moosavi

» str2 = 'Isn''t MATLAB great?'

str2 =

Isn't MATLAB great?

1x9 vectorstr = M o o s a v iM

Page 61: Matlab present

» str ='Hi there,';

» str1='Everyone!';

» new_str=[str, ' ', str1]

new_str =Hi there, Everyone! » str2 = 'Isn''t MATLAB great?';

» new_str2=[new_str; str2]new_str2 =Hi there, Everyone!Isn't MATLAB great?

1x19 vector

1x9 vectors

String Array Concatenation

Using [ ] operator:Each row must be same length

Row separator:semicolon (;)

Column separator:space / comma (,)

For strings of different length:• STRVCAT• char

» new_str3 = strvcat(str, str2)new_str3 =Hi there, Isn't MATLAB great?

2x19 matrix

2x19 matrix(zero padded)

Page 62: Matlab present

Working with String Arrays• String Comparisons

– strcmp: compare whole strings– strncmp: compare first ‘N’ characters– findstr: finds substring within a larger string– Strcat: link two or more strings together

• Converting between numeric & string arrays:– num2str: convert from numeric to string array– str2num: convert from string to numeric array

Page 63: Matlab present

a=strcmp ('('Yes', 'Noa= 0b=strcmp('Yes', 'Yes') b= 1

Working with String Arraysa='alireza';b=‘reza';comp=findstr(a,b)comp = 4comp=findtr(a,'ah')comp = []

a='alireza';b='ali';comp=strncmp(a,b,3)comp = 1comp=strncmp(a,b,4)comp = 0

a = 'hello 'b = 'goodbye'strcat(a, b)ans =hellogoodbye

Page 64: Matlab present

Structures

Arrays with named data containers called fields.» patient.name='John Doe';» patient.billing = 127.00;» patient.test= [79 75 73; 180 178 177.5; 220 210 205];

•Also, Build structure arrays using the struct function.

•Array of structures» patient(2).name='Katty Thomson';» Patient(2).billing = 100.00;» Patient(2).test= [69 25 33; 120 128 177.5; 220

210 205];

Page 65: Matlab present

Cell Arrays Array for which the elements are cells and can.hold other MATLAB arrays of different types

» A(1,1) = {[1 4 3;0 5 8;7 2 9]};» A(1,2) = {'Anne Smith'};» A(2,1) = {3+7i};» A(2,2) = {-pi:pi/10:pi};

•Using braces {} to point to elements of cell array

•Using celldisp function to display cell array

Page 66: Matlab present

if expression1 statements1elseif expression2 statements2else statements3end

Loop Controls

مثالscore=[18 17 12 20]i=2; if score(i)>18 grade=‘A’elseif score(i)>16 grade=‘B’elseif score(i)>14 grade=‘C’else grade=‘D’end

if-endحلقه

Page 67: Matlab present

for index = start : increment : end statementsend

كنترلي هاي :حلقه

سري يك مكرر انجام به از ي ن كه يد آ مي پيش بسيار بع توا نوشتن در

استفاده كنترلي هاي حلقه از موارد گونه اين در ميباشد دستورات از

شود پردازيم . مي مي كنترلي حلقه چند معرفي به جا اين .در

Loop Controls

for-end حلقه مثالfor a=1:2:5;x=0; for j = 1:5 x=x+a(j); endend

Page 68: Matlab present

F unctions in

M A TL A B

Page 69: Matlab present

F unctions in MATLAB

ب ه مجموع ه اي از دس تورات گفت ه م ي شود كهتاب ع

فرايندي خاص را بر روي داده هاي ورودي انجام داده و

.مي فرستد matlabخروجي را به

workspace functioninput output

Page 70: Matlab present

F unctions in MATLAB

ا كلمه كليدي • ب ابع .آغاز ميشود functionهر ت

سير معيني• ستي در م ند باي ني باش بل فراخوا كه قا ين ا بع براي توا

.ذخيره شوند

.فراخواني يك تابع از طريق نام آن صورت ميگيرد •

تواند در بدنه خود ديگر توابع را فراخواني كند• .يك تابع مي

تواند خود محتوي چند تابع باشد• .هر تابع مي

با پسوند • كه ند سوب ميشو ني مح يل هاي مت بع فا ذخيره .mتوا

.مي گردند

Page 71: Matlab present

F unctions in MATLAB

تواند شامل چند متغير ورودي و يا خروجي باشد• .هر تابع مي

ا ايجاد ميشوند در صورتي• ي بع تعريف و متغير هايي كه در بدنه تا

كه جزء متغيرهاي خروجي نباشند در فضاي كاري قابل شناسايي

به صورت ذيل تعريف مي.نيستند بع تا

.شود

function [out1, out2, ...] = funname(in1,

in2, ...) …out1,out2,متغيرهاي ورودي ومتغيرهاي …in1,in2,متغير هاي

باشند .متغيرهاي خروجي مي

Page 72: Matlab present

F unctions in MATLABكه بتوان در ين ا تابع matlabبراي

نوش ت كاف ي است از منوي

file نه سپس گزينه newگزي و

mfile انتخاب نمود در اين. را

صورت ص فحه سفيدي باز

تن تابع كه امكان نوش ميشود

.را فراهم مي آورد

Page 73: Matlab present

F unctions in MATLABايد ان شد ب مل نوشته به طور كا بع كه تا ين ا از پس

.كرد saveرا

Page 74: Matlab present

F unctions in MATLAB

switch switch_expr case case_expr statement, ..., statement case {case_expr1, case_expr2,...} statement, ..., statement otherwise statement, ..., statementend

Loop Controls

switch-case-end عبارت مثالmethod = 'Bilinear';

switch lower(method)

case {'linear','bilinear'}

disp('Method is linear')

case 'cubic'

disp('Method is cubic')

otherwise

disp('Unknown method.')

end

Page 75: Matlab present

F unctions in MATLAB

:مثالوسپس كرده دريافت را عدد يك كه بنويسيم تابعي خواهيم مي

function n = factorial (k).محاسبه كند for فاكتوريل آن را به كمك حلقه % The function [n] = factorial(k) calculates and% returns the value of k factorial. If k is negative,% an error message is returned.if (k < 0) n = 'Error, negative argument’;elseif k<2 n=1;else n = 1; for j = 2:k

n = n * j; endend

تابع توضيحات

دستورات تابع

Page 76: Matlab present

F unctions in MATLAB

>> a=factorial(5)

a =

120

>> a=factorial(-1)

a =

Error, negative argument

بع در ير قابل matlabپس از اجراي تا صورت ز به يج ا ت ن

.مشاهده است

Page 77: Matlab present

كار در كلس

Page 78: Matlab present

F unctions in MATLABرشته 1. يك وانتگرال مشتق تابع يك باشد قرار اگر

و ورودي متغير چنند داراي ايند ب نمايند تعيينن را

باشد را .خروجني ابع ت اينن امكان صنورت در

تابع وانتگرال مشتننق آننن له ي بوسنن و ننه نوشت

(exp(x^2 يد كن محاسبه را

دريافت 3. را برداري ماتريس يك كه بنويسيد تابعي

رديف چك كو به بزرگ از آنرا هاي داده و كرده

بفرستد خروجي در و .نموده

Page 79: Matlab present

F unctions in MATLAB

function [dif integ] = difint (func)

if isstr(func)~=1

disp(‘please input a string function’)

return

end

dif=diff(func);

integ=int(func);

Page 80: Matlab present

F unctions in MATLAB

يك خروجي يا و ورودي هاي متغير تعداد كه يد آ مي پيش اوقات گاهي

تابع يف تعر هنگام جه نتي در ند ميك ير تغي بع ا ت عملكرد به سته واب بع تا

ميشويم مشكل بع . دچار تا مثال عنوان متغير plotبه يك داراي ند توا مي

طور به سيم تر نحوه و نمودار نوع به سته واب كه ين ا ا ي و شد با ورودي

باشد داشته ورودي متغير يك . نامحدود داراي ند ميتوا بع تا ين ا ين همچن

باشد نداشته خروجي متغير اصل يا باشد خروجي مواقع . متغير ين ا در

توابنع خروجي varargin , varargout از وينا ورودي متغينر عنوان به

شود مي .استفاده

varargin , varargout , nargin , nargout توابع

Page 81: Matlab present

F unctions in MATLAB

متغيرهاي vararginمتغينر كليه و كرده عمنل ابنع ت ورودي عنوان به

ميدهد قرار خود در را ورودي . ورودي حاوي تابع هر براي متغير ين ا

است بع تا همان ورودي . هاي تعداد با بر برا آن طول و سلولي آن نوع

ميباشد تابع .هايكليه varargoutمتغينر حاوي و كرده عمنل ابنع ت خروجني عنوان به

اسنت ابنع ت خروجني ا . متغيرهايني ب بر برا آنن طول و سنلولي آنن نوع

ميباشد تابع هاي خروجي .تعدادير و nargin , nargoutمتغ خروجي متغيرهاي تعداد محتوي يب ترت به

هستند است . ورودي عددي آنها نوع و يك برابر نها آ .طول

varargin , varargout , nargin , nargout توابع

Page 82: Matlab present

مثالfunction myplot(x,varargin)plot(x,varargin{:})

collects all the inputs starting with the second input into the variable varargin. myplot uses the comma-separated list syntax varargin{:} to pass the optional parameters to plot. The call

myplot(sin(0:.1:1),'color',[.5 .7 .3],'linestyle',':')

results in varargin being a 1-by-4 cell array containing the values 'color', [.5 .7 .3], 'linestyle', and ':'.

Functions in MATLABvarargin , varargout , nargin , nargout توابع

Page 83: Matlab present

مثالfunction [s,varargout] = mysize(x)nout = max(nargout,1)-1;s = size(x);for k=1:nout, varargout(k) = {s(k)}; end

returns the size vector and, optionally, individual sizes. So [s,rows,cols] = mysize(rand(4,5));Returns s = [4 5], rows = 4, cols = 5.

Functions in MATLABvarargin , varargout , nargin , nargout توابع

Page 84: Matlab present

رسم نمودار در

MATLAB

Page 85: Matlab present

محسوب MATLABرسم نمودار يكي از قابليت هاي مهم

به دو MATLABتوابع رسم نمودار در . مي گردد

:دسته تقسيم ميشوند

توابع ماتريسي •

توابع سمبليك•

MATLABدر رسم نمودار

Page 86: Matlab present

توابعي كه با ماتريس ها كار ميكنند و متغير ورودي . آنها ماتريس ها هستند

توابع

ماتريسي

:عبارتند ازMATLAB تعدادي از توابع ماتريسيplot3,plot, bar,bar3,pie,pie3,contour

MATLABدر رسم نمودار

Page 87: Matlab present

توابعي كه متغيرهاي ورودي آنها به صورت سمبوليك.يا رشته اي مي باشند

:عبارتند ازMATLAB تعدادي از توابع سمبليك ezplot,ezcontour ,ezsurf.

توابع

سمبوليك

MATLABدر رسم نمودار

Page 88: Matlab present

bar-bar3h-hist-area-pie3-rose

MATLABدر رسم نمودار

Page 89: Matlab present

contourf-colorbar-plot3-waterfall-contour3-mesh-surf

MATLABدر رسم نمودار

Page 90: Matlab present

دو رسمبعديPlotتابع

رسم نمودار هاي دوبعدي محسوب مي شودو امكانات زيادي را براي ويرايش تصوير در

.خروجي فراهم مي آورد

MATLABدر رسم نمودار

Page 91: Matlab present

تابع plotنحوه ي استفاده از

MATLABدر رسم نمودار

(Handle = plot(X1,Y1,LineSpec,'PropertyName',PropertyValue;

ماتري س هاي برداري هم اندازه X1,Y1در عبارت بال ، هستند كه قرار است به صورت يك به يك بر روي محورهاي

X,Y رسم شوند.يك رشت ه اس ت ك ه درآن ميتوان LineSpecمتغي ر

-به عنوان مثال مارك ر م ي(خص وصيات رن گ خ ط، نوع مارك ر

،(- و نوع خط) وي ا س تاره باش دتوان د ب ه ص ورت مرب ع .، را تعيين نمود.-)، : ، --

PropertyName تمامي خواص شيئ شامل ’Line‘ ميگردد ك ه قرار اس ت ت ا ب ا مقادي ر عددي ي ا رشته اي

PropertyValue مقداردهي شوند). . PropertyName .)مي شود هميشه به صورت رشته ظاهر

Page 92: Matlab present

;x = -pi:pi/10:piمثالy=sin(x);h=plot(x,y, '-.r','linewidth',3);

MATLABدر رسم نمودار

Page 93: Matlab present

x = -pi:pi/10:pi;y=sin(x);h=plot(x,y, '-.r','linewidth',3);

title('Sample Plot','fontsize',14);xlabel('X values','fontsize',14);ylabel('Y values','fontsize',14);

legend('Y data')

MATLABدر رسم نمودار

Page 94: Matlab present

كار در كلس

Page 95: Matlab present

Linewidth=2Color=red Title=‘(y= exponential(x‘

Xlabel=‘x‘

Ylabel=‘(exp(x‘

=Marker type’*‘

. رسم نماييد 0.1با دقت [-10 10]را در بازه ي y=exp(x)نمودار تابع

MATLABدر رسم نمودار

Page 96: Matlab present

>> x=-10:.1:10;

>> y=exp(x);

>> a=plot(x,y,‘pr','linewidth',2)

>> Title('y= exponential(x)')

>> Ylabel('exp(x)')

>> Xlabel('x')

MATLABدر رسم نمودار

Page 97: Matlab present

تنظيم جه تset,get از تواب عSet, get توابع .خص وصيات اشياء استفاده ميگردد

در اولين آرگومان وروديset تاب ع خود شماره شي ء مورد نظر رادريافت كرده و در ورودي هاي ديگرخود خواص و مقادير آنها را تنظيم

.مينمايدمقدار عددي يا رشته اي getتابع

.متناظر با يك خاصيت را بر ميگرداند

set(gca,'fontsize',12,…

'FontWeight','bold’);

fs=get(gca, 'fontsize’)

fs=

12

MATLABدر رسم نمودار

Page 98: Matlab present

MATLABدر رسم نمودار

Page 99: Matlab present

يک در منحني چند همزمان رسمنمودار

x=0:.1:1;

y=exp(x);

z=exp(-x);

plot(x,y,'-.r',x,z,'—gp’)

زمان در يك نمودار ميتوان از طور هم جهت رسم چند منحني بهمثال. همچون مثال زير استفاده نمودplot تابع

MATLABدر رسم نمودار

Page 100: Matlab present

تابع مهم :معرفي چند

:hold استفاده نمودار يك روي بر منحني چند رسم جهت تابع ازاين

.ميشود

subplot: از اين تابع جهت رسم چند نمودار در يكfigureاستفاده

.ميشود

ginput:اين تابع براي داده برداري دستي از نمودار استفاده ميشود.

grid:جهت شطرنج بندي نمودار از ان استفاده ميشود.

Semilogx,semilogy:محورهايx,y را به صورت لگاريتمي در نظر

.ميگيرد

loglog:هر دو محور را به صورت لگاريتمي رسم ميكند.

Linspace :از اين تابع براي گرفتن تعداد معيني عدد در يك بازه ي

.معين استفاده مي شود

MATLABدر رسم نمودار

Page 101: Matlab present

;(x = linspace (0, 10, 100

;y = 2*x.^2 + 7*x + 9

:plotting the polynomial %

;(figure (1

;(subplot (2,1,1), plot (x,y

;('title ('Polynomial, linear/linear scale

;ylabel ('y'), grid

;(subplot (2,1,2), semilogx (x,y

;('title ('Polynomial, log/linear scale

;ylabel ('y'), grid

MATLABدر رسم نمودار

Page 102: Matlab present

ezplot متغي ر ورودي خود را ب ه ص ورت س مبليك و يادر عبارت زير نحوه ي كار. رشته اي دريافت مي نمايد

.بيان شده است ezplot تابع

و نحوه ي استفاده از آنezplot تابع

h=ezplot(‘tan(x)’);

Handle = ezplot(‘function’)

MATLABدر رسم نمودار

Page 103: Matlab present

H=ezplot('y-tan(x)',[0 3*pi/2 -2*pi 2*pi])

Handle=ezplot(‘function',[xmin xmax ymin ymax])

MATLABدر رسم نمودار

Page 104: Matlab present

تنظيم خصوصيات منحنيامكان دستيابي به منحن ي يا دس ته منحني H در مثال قب ل عدد

هاي رسم شده را فراهم مي آورد وبه كمك آن ميتوان تغييرات.لزم را بر روي منحني هاي رسم شده اعمال نمود

;(’--','set(H(1),'color','r','linestyle

;('.-','set(H(2),'linewidth',3,'linestyle

MATLABدر رسم نمودار

Page 105: Matlab present

سه رسمبراي ترس يم منحن ي هاي سه MATLABتواب ع زيادي در بعدي

بعدي ب ه كار ميرون د ك ه پ ر كاربردتري ن آنه ا تابعezplot3,plot3 اي ن تواب ع همانن د توابع.محس وب ميشوند

ezplot,plotعم ل م ي كنن د ب ا اي ن تفاوت ك ه براي رسم;Handle = plot3(X1,Y1,LineSpec,'PropertyName',PropertyValue).منحني هاي سه بعدي پيش بيني شده اند

Handle = ezplot3( ‘funcX’,’funcY’,’funcZ’ ,[tmin,tmax])

MATLABدر رسم نمودار

Page 106: Matlab present

t = 0:pi/50:10*pi ;plot3(sin(3*t),cos(2*t),t)grid on

ezplot3('sin(t)','cos(t)','t',[0,6*pi] ) grid on

مثال

MATLABدر رسم نمودار

Page 107: Matlab present

كار در كلس

Page 108: Matlab present

xminorgrid=onyminorgrid=on[Color=[0.5 0.5 0Fontsize=14[position=[0 0 1 1

3 مودار متغير رشته اي را دريافت نموده ون3 تابعي تعريف نماييد كه .بعدي تابع مربوطه رسم نماييد

MATLABدر رسم نمودار

Page 109: Matlab present

z='tan(t)';

x='cot(t)';

y='t';

ezplot3(x,y,z)

set(gca,'color',[.5 .5 1],'xminorgrid','on','yminorgrid','on','position',[0 0 1 1])

MATLABدر رسم نمودار

Page 110: Matlab present

ezsurfc('y/(1 + x^2 + y^2)',[-5,5,-2*pi,2*pi]) ezmesh(x*exp(-x^2-y^2),[-2.5,2.5],40)

colormap([0 0 1])

MATLABدر رسم نمودار

Page 111: Matlab present

MATLABويراست دستي نمودار د ر MATLABدر رسم نمودار

Edit Plot Pan Data cursor

Insert Legend

Show Plot Tools

Page 112: Matlab present

MATLABويراست دستي نمودار د ر MATLABدر رسم نمودار

Page 113: Matlab present

MATLABويراست دستي نمودار د ر MATLABدر رسم نمودار

Page 114: Matlab present

MATLABويراست دستي نمودار د ر MATLABدر رسم نمودار

Page 115: Matlab present

MATLABويراست دستي نمودار د ر MATLABدر رسم نمودار

Page 116: Matlab present

MATLABويراست دستي نمودار د ر MATLABدر رسم نمودار

Page 117: Matlab present

MATLABويراست دستي نمودار د ر MATLABدر رسم نمودار

Page 118: Matlab present

MATLABويراست دستي نمودار د ر MATLABدر رسم نمودار

Page 119: Matlab present

MATLABويراست دستي نمودار د ر MATLABدر رسم نمودار

Page 120: Matlab present

Curve

F itting

Page 121: Matlab present

Curve F itting

ها در نموداره ا يك ي از مس ائل مهم در علوم درونياب ي دادهمهندس ي و رياضيات محس وب ميگردد ك ه در اي ن جا در مورد

بر منحنيnي اي درجه فيت نمودن يك منحني چندجمله نحوه.مورد نظر، بحث ميشود

Polynomial curve fitting

polyfitتابعرا دريافت كرده yو تاب ع ماتريس ي xاي ن تاب ع متغيرماتريس ي

را كه بر منحني متناظر باnاي از درجه وضرايب يك نمودار چندجمله

x,y فيت هستند به ما تحويل ميدهد.p=polyfit(x,y,n)

Page 122: Matlab present

Curve F itting

polyvalتابعPolynomial curve fitting

وnاي از درجه اين تابع و تابع ماتريسي ضرايب يك نمودار چندجمله

را xمتناظر با ماتريس yرا دريافت كرده و مقادير x متغيرماتريسي

.بر ميگرداند

به كار ميرود و براي polyfit اين تابع معمول بلفاصله بعد از تابع

p=polyval(p,x).شود رسم داده هاي درونيابي شده استفاده مي

Page 123: Matlab present

Curve F itting

x=0:.1:1;

y=[1 2 4 5 6 7 9 13 14 15 18];

p=polyfit(x,y,3);

a=polyval(p,x);

plot(x,y,'r',x,a,'--b','linewidth',3);

مثال

[p=[-2.7195 10.6061 8.8267 1.1678

Fitted curve=-2.7x^3 + 10.6x^2 + 8.8x + 1.2

Polynomial curve fitting

.است فيت شده Y كه بر منحني 3 اي درجه عبارتست از ضرايب چند جمله pدر اين جا ماتريس

Page 124: Matlab present

كار در كلس

Page 125: Matlab present

Curve F itting

Polynomial curve fitting

Cp = [444 521 610 699 755 769 ];

T = [298 450 600 800 1000 1033];

و دما بر حسب درجه كلوين براي آهن فريتي j/kg.kبر حسب مقادير ظرفيت حرارتي

در محدوده دمايي فوق از رابطه Cp اگرفرض گردد . خالص به صورت زير داده شده است

.معادله آن را بدست آوريد ونمودار آن را رسم نماييد. خطي پيروي ميكند

Page 126: Matlab present

Curve F itting

Exponential curve fitting

p=polyfit(T,Cp,1);p =

0.4377 327.9665

a=polyval(p,T);

plot(T,Cp,‘g',T,a,'b','linewidth',2);

xlabel('T (k)','fontsize',14)

ylabel('Cp (j/kg.k)','FONTSIZE',14)

Page 127: Matlab present

)(10 xfy =

Exponential curve fittingبراي درونيابي دادها به صورت اكسپونانسيلي از همان

استفاده ميشود با اين تفاوت كه دراينجا بهpolyfit تابع

.، لگاريتم آن وارد ميشود y جاي ماتريس

قرار اس ت طب ق تابعي yفرض كني د ك ه مقادي ر تاب ع

لگاريتمي به شكل

:آنگاه ميتوان نوشت.درونيابي شود

)log()( yxf =

logy در نتيج ه اگ ر عم ل في ت كردن بر روي ماتريس بدست مي آيد و ميتوان به كمك f(x) صورت گيرد تابع

.را هم بدست آورد yان تابع

Curve F itting

Page 128: Matlab present

كار در كلس

Page 129: Matlab present

Curve F itting

Polynomial curve fitting

.مي باشد y=exp(a*x^2 + b*x +c) اعداد زير مربوط به تابع

.را بدست آوريدa,b,cضرايب

Page 130: Matlab present

Curve F itting

Exponential curve fitting

Plot(data(:,1),data(:,3))

Page 131: Matlab present

Curve F itting

Exponential curve fitting

x=0:.1:1;

y=[1 2 4 5 6 7 9 13 14 15 18];

p=polyfit(x,log10(y),3);

a=10.^polyval(p,x);

semilogy(x,y,'+r',x,a,'-b','linewidth',3);

Page 132: Matlab present

D ata I mport

and E xport

D ata I mport

and E xport

Page 133: Matlab present

Data Import and ExportD ata

I mport and

E xport جدول زير فرمت فايل هايي كه matlab ميتواند بخواند يا بنويسدرا به هراه توابعي كه هر فرمت را پشتيباني ميكنند نشان ميدهد

Page 134: Matlab present

Data Import and ExportD ata

I mport and

E xport خواندن يك فايل) ويزارد(ما براي سادگي كار روند مرحله به مرحله

.است را بررسي ميكنيم DSCمتني كه حاوي داده هاي دستگاه

Import text data file

Page 135: Matlab present

Data Import and ExportD ata

I mport and

E xport

Import text data file

Page 136: Matlab present

Data Import and ExportD ata

I mport and

E xport

Import text data file

Page 137: Matlab present

Data Import and ExportD ata

I mport and

E xport

Import text data file

Page 138: Matlab present

Data Import and ExportD ata

I mport and

E xport

پس از طي مراحل ويزارد داده هاي انتخاب شده

از فايل مورد نظر درون فضاي كاري نرم افزار

.توليد شده و قابل دسترسي خواهند بود

روال گام به گام ذكر شده براي كليه فرمت هايي•

از آن ها پشتيباني ميكند قابل انجام matlab كه

.است

Import text data file

Page 139: Matlab present

Data Import and ExportD ata

I mport and

E xport

Export matlab file

MATLAB قادر است تا نتايج فعاليت هاي خود را به صورت هاي

مختلف ذخيره سازي نمايد كه اين كار را به كمك توابع خاصي كه

.براي هر نوع فرمت خاص تعيين شده اند انجام ميدهد

عمليات مهمترين جملنه ه، ذخير matlabدر exportاز

ا ني، ذخيره سازي تصوير ب يل مت به طريق ايجاد فا سازي

پس وند هاي مختل ف، ذخيره س ازي داده ه ا ب ه صورت

اexcelفاي ل نرم افزار ، ذخيره س ازي تص اوير متحرك ب

.ميباشد... و .aviپسوند

Page 140: Matlab present

Data Import and ExportD ata

I mport and

E xport

Export matlab fileMATLAB قادر است كه اطلعات موجود در فضاي كاري خود را

تا) .mat(به صورت فايلي با پسوند مخصوص به خود ذخيره كند

.بتوان در آينده از آن ها استفاده نمود

save فاي ل” از تابع “م تبراي ذخيره كردن اطلعات ب ه شك ل

save ('path', 'var1', 'var2').استفاده ميشودمس ير مورد نظر براي ذخيره(path) اولي ن آرگومان در عبارت بال

كردن را تعيين كرده و آرگومان هاي ديگر متغير هايي كه نياز است تا

.نگهداري شوند را تعيين مي كنند

Page 141: Matlab present

Data Import and ExportD ata

I mport and

E xport

Export matlab file

save ('path')

به شكل مقابل به كار رود، كليه saveدر اگر تابع

متغير هاي موجود در فضاي كاري، در مسير مورد

.نظر نوشته ميشوند

save

بدون ارگومان ورودي به saveدر حالتي كه تابع

كار رود كلي ه متغي ر هاي موجود در مسير جاري

matlab) نوشته ميشوند) مسير پيش فرض.

Page 142: Matlab present

كار در كلس

Page 143: Matlab present

Curve F itting

Import matlab file

وارد كرده matlabبه محيط desctop را از روي dsc.txtفايل متني با نام

.بر حسب زمان را رسم نماييد‌dh/dtومنحني

Page 144: Matlab present

Data Import and ExportD ata

I mport and

E xport

import matlab file

A=load ('path')به شكل مقابل به كار رود، كليه loadدر اگر تابع

matبا پسوند ‌pathمتغي ر هاي موجود در فاي ل

.ميشود Aداخل متغير ساختاري

Load path به شكل مقابل به كار رود، loadدر حالتي كه تابع

به همان path كليه متغير هاي موجود در مسير

.صورت وارد محيط كاري ميشوند

Page 145: Matlab present

Data Import and ExportD ata

I mport and

E xport

import fileA=load (‘filename‘,’format’)

Read data from text file;

fid=textread(‘d:\Mohsen\necessary informations.txt','%s')

'[email protected]' '--------------------------------' 'ETS' 'ID:3516769' 'user:moosavinezhad' 'pass:Smoosavi2919'

Page 146: Matlab present

پنجره هايكنترلي

Page 147: Matlab present

answer = inputdlg(prompt)

پنجره هاي كنترليInputdlg

a=inputdlg('Please input your name')a = 'Mohsen'>> whos a Name Size Bytes Class a 1x1 72 cell arrayGrand total is 7 elements using 72 bytes

Page 148: Matlab present

answer = inputdlg(prompt,dlg_title,num_lines,defAns)

پنجره هاي كنترليInputdlg

>> answer = inputdlg('Please input your function',…'Input Box',2,{'exp(x)'})answer = 'exp(x)'>> func=char(answer);>> whos answer func Name Size Bytes Class answer 1x1 72 cell array func 1x6 12 char arrayGrand total is 13 elements using 84 bytes

Page 149: Matlab present

كار در كلس

Page 150: Matlab present

ي د ك ه تابعي را از كاربر ترل ي ايحاد كن پنجره اي كن

.بگيردPrompt=‘Plaese input function name:’Title=‘Input Function’num_line=1Def_ans=‘x^2’

ابع log10(x^3)تابع ت رسم نمايد [0 100]در بازه ezplot را به كمك اين پنجره و سپس به كمك

پنجره هاي كنترلي

Page 151: Matlab present

answer = inputdlg('Plaese input function name:','Input Function',1,{'x^2'})answer = 'log10(x^3)'

>> ezplot(char(answer),[0 100])

پنجره هاي كنترلي

Page 152: Matlab present

.از كاربر دريافت نمود حال مي خواهيم بدانيم چگونه مي توان بيش از يك متغير ورودي

پنجره هاي كنترليInputdlg

>> answer = inputdlg({'Input x','input y'},'Input Function',...1,{'[1 2 3 4 5]','[2 4 6 8 10]'})answer = '[1 2 3 4 5]' '[2 4 6 8 10]'>> x=str2num(char(answer(1)));>> y=str2num(char(answer(2)));

Page 153: Matlab present

.از اين پنجره براي نمايش جواب ها و پيغام ها به كاربر استفاده مي شود

پنجره هاي كنترليmsgbox

msgbox(message,title)

>> x=1:10;>> y=2:2:20;>> p=polyfit(x,y,1);>> message=strcat('Coefficient for linear fitting curve is:[',num2str(p),']');>> msgbox(message,'Linear fitting data');

Page 154: Matlab present

كار در كلس

Page 155: Matlab present

ير كه مقاد يد لي ايحاد كن را به x,y پنجره اي كنتر

.صورت عددي از كاربر دريافت كندبه كمك پنجره كنترلي يب معادله msgboxسپس ضرا

جه يت ميشود به كاربر نشان2 در آن ف كه بر را

.دهيدPrompt= {'Input x','input y'}

Title=‘Input Function’num_line=1Def_ans ={'[1 2 3 4 5]','[2 4 6 8 10]'})

پنجره هاي كنترلي

Page 156: Matlab present

پنجره هاي كنترلي

>> answer = inputdlg({'Input x','input y'},'Input Function',...1,{'[1 2 3 4 5]','[2 4 6 8 10]'});>> x=str2num(char(answer(1)));>> y=str2num(char(answer(2)));>> p=polyfit(x,y,2);>> message=strcat('Coefficient for linear fitting curve is:[', num2str(p), ']');>> msgbox(message,’Coefficient of fitted curve’)

Page 157: Matlab present

Symbolic

M ath

Page 158: Matlab present

اس ت ك ه در آ ن ب ه محاس بات ب ه صورت matlabرياص يات س مبليك بخش ي از

جعب ه ابزار رياضيات سمبليك. سمبوليك وب ه كم ك نماده ا ص ورت م ي گيرد

.محسوب ميشود matlabمكمل بخش محاسبات عددي

Symbolic mathIntroduction

Page 159: Matlab present

جعبه ابزار رياصيات سمبليك نوع جديدي از داده ها به نام اشيا سمبليك را تعريف

يك شيي سمبوليك يك داده محسوب ميشود كه يك رشته را به عنوان. مي كند

اين جعبه ابزار به كمك اشيا سمبليك متغيرها،. يك نماد در خود ذخيره ميكند

. عبارات و ماتريس هاي سمبليك را نمايش ميدهد

Symbolic mathIntroduction

چگونه ميتوان يك متغير سمبليك تعريف كرد؟

.ميتوان متغيرهاي سمبليك تعريف نمودsym , syms به كمك توابع

Page 160: Matlab present

يك رشته را به عنوان ورودي دريافت كرده و يك خروجي symتابع symsبراي سادگي ميتوان از عبارت . از نوع سمبليك به ما ميدهد

.استفاده نمود symبه جاي

Symbolic mathIntroduction

a=sym('alpha')

a =alpha

b=a^2

b=

alpha^2

a = sqrt(sym(2^2));b=2;whos a b Name Size Bytes Class a 1x1 126 sym object b 1x1 8 double arrayGrand total is 2 elements using 126 bytes

Page 161: Matlab present

subsتابع

اين امكان را به ما ميدهد تا در يك عبارت به جاي مقدار سمبليك يك subsتابع

.عدد را جايگزين كنيم

Introduction

syms x yz=y*sin(x);z1=subs(z,x,pi/2)z1=y

syms a bf=subs(cos(a)+sin(b),{a,b},{sym('alpha'),pi})f=cos(alpha)

Symbolic math

Page 162: Matlab present

كار در كلس

Page 163: Matlab present

اب ع را ب ه ازاي مقادير z=atan(x*y)-exp(y)ت

x=1,y=2بدست آوريد.

ازاي تابع را به .رسم كنيد x=1:10;y=1:10منحني اين

Symbolic math

Page 164: Matlab present

syms x yz=atan(x*y) - exp(y);f=subs(z,{x,y},{1,2})f=-6.2819

Symbolic math

x=1:10;y=10:10:100;z=subs(z,{x,y},

{1:10,10:10:100});X1=1:10;Y1=10:10:100;Plot3(x1,y1,z)

Page 165: Matlab present

Symbolic Math functionsSymbolic Math

The Symbolic Math Toolbox provides functions to do the basic operations of calculus. The following sections describe these functions

Calculus

Differentiation Limits Integration Symbolic Summation Taylor SeriesJacobian

Page 166: Matlab present

Symbolic Math functionsSymbolic Math

Differentiation

Calculus

ExamplesAssume syms x tThen diff(sin(x^2))returns 2*cos(x^2)*xand diff(t^6,6)returns 720

Page 167: Matlab present

Symbolic Math functionsSymbolic Math

Calculus

limit ExamplesAssume syms x a t h;Then limit(sin(x)/x) => 1limit(1/x,x,0,'right') => inflimit(1/x,x,0,'left') => -inflimit((sin(x+h)-sin(x))/h,h,0) => cos(x)v = [(1 + a/x)^x, exp(-x)];limit(v,x,inf,'left') => [exp(a), 0]

Page 168: Matlab present

Symbolic Math functionsSymbolic Math

Calculusintegration

Examplesint(-2*x/(1+x^2)^2) returns 1/(1+x^2)int(x/(1+z^2),z) returns x*atan(z)int(x*log(1+x),0,1) returns 1/4int(2*x, sin(t), 1) returns 1-sin(t)^2int([exp(t),exp(alpha*t)]) returns [exp(t), 1/alpha*exp(alpha*t)]

Page 169: Matlab present

Symbolic Math functionsSymbolic Math

f = x^3-6*x^2+11*x-6g = (x-1)*(x-2)*(x-3)

h = -6+(11+(-6+x)*x)*xpretty(f), pretty(g), pretty(h)

x3 - 6 x2 + 11 x - 6(x - 1) (x - 2) (x - 3)

-6 + (11 + (-6 + x) x) x

Simplifications Substitution

s

Page 170: Matlab present

Symbolic Math functionsSymbolic Math

collect Collect coefficients R = collect(S,v)

For each polynomial in the array S of polynomials, collect(S) collects terms containing the variable v (or x, if v is not specified). The result is an array containing the collected polynomials.

Simplifications Substitution

s

Page 171: Matlab present

Symbolic Math functionsSymbolic Math

factor Factorization factor(X)

factor can take a positive integer, an array of symbolic expressions, or an array of symbolic integers as an argument. If N is a positive integer, factor(N) returns the prime factorization of N

Simplifications Substitution

s

Page 172: Matlab present

Symbolic Math functionsSymbolic Math

Simplifications Substitution

s

Page 173: Matlab present

Symbolic Math functionsSymbolic Math

simplify Symbolic simplification R = simplify(S)

simplify(S) simplifies each element of the symbolic matrix S using Maple simplification rules

Simplifications Substitution

s

Page 174: Matlab present

Symbolic Math functionsSymbolic Math

Integral Transforms

fourier

Page 175: Matlab present

Symbolic Math functionsSymbolic Math

Integral Transforms

ifourier

Page 176: Matlab present

Symbolic Math functionsSymbolic Math

Solution of Equations

dsolve Symbolic solution of ordinary differential equations

r = dsolve('eq1,eq2,...', 'cond1,cond2,...', 'v')

Description

symbolically solves the ordinary differential equation(s) specifiedby eq1, eq2,... using v as the independent variable and the boundary and/or initial condition(s) specified by cond1,cond2,....

Page 177: Matlab present

Symbolic Math functionsSymbolic Math

Examples

dsolve('Df = f + sin(t)') returns -1/2*cos(t)-1/2*sin(t)+exp(t)*C1

y = dsolve('(Dy)^2 + y^2 = 1','y(0) = 0') returns y = sin(t) -sin(t)

Solution of Equations

Page 178: Matlab present

Symbolic Math functionsSymbolic Math

Solution of Equations

solve Symbolic solution of algebraic equations

g = solve(eq)g = solve(eq,var)g = solve(eq1,eq2,...,eqn)g = solve(eq1,eq2,...,eqn,var1,var2,...,varn

Page 179: Matlab present

Symbolic Math functionsSymbolic Math

Solution of Equations

Examples

solve('a*x^2 + b*x + c') returns [ 1/2/a*(-b+(b^2-4*a*c)^(1/2)), 1/2/a*(-b-(b^2-4*a*c)^(1/2))]

solve('a*x^2 + b*x + c','b') returns -(a*x^2+c)/x

S = solve('x + y = 1','x - 11*y = 5') returns a structure S with S.y = -1/3, S.x = 4/3

Page 180: Matlab present

Symbolic Math functionsSymbolic Math

Solution of Equations

solve Symbolic solution of algebraic equations

g = solve(eq)g = solve(eq,var)g = solve(eq1,eq2,...,eqn)g = solve(eq1,eq2,...,eqn,var1,var2,...,varn)

Page 181: Matlab present

كار در كلس

Page 182: Matlab present

ا نام هاي و بازه رسم fx,gx تابعي بنويسيد كه دوتابع ب

وf(gx), g(fx) آن ه ا را درياف ت كرده وسپس

fx/gx,fx*gx آن بازه بع را در دريك subplotبه كمك تا

figure رسم نمايد.

Symbolic math

Page 183: Matlab present

Symbolic math

fgx=subs(fx,x,gx);subplot(2,2,1);ezplot(fgx,domain)

f(g(x))

fgx=subs(gx,x,fx);subplot(2,2,2);ezplot(gfx,domain)

g(f(x))

fog=fx/gx;subplot(2,2,3);ezplot(fog,domain)

f(x)/g(x)

fmg=fx*gx;subplot(2,2,4);ezplot(fmg,domain)

g(x)*f(x)

function plotted(fx,gx,domain)syms xfx=sym(fx);gx=sym(gx);

تعاريف

Page 184: Matlab present

خداحافظ