د آ ام ات
مقدمة Matlab (M-File)! ا%$#"ام (Command Window) ا ا (Functions) ا (Scripts) ٢
٣
البرامج النصية Scripts ( Scripts ) آ Blank M-file < اNew7 < File ا 3 453ا : 9 ة ا 4 ;ر.Editor اآ$> % ا$ 4 ت..Save ا 7 < File 3@ A 453ا ا<!=?4ا ( D آ$ ا%?4ا : : 9 ة Cا 9!@ (B4 ا 7 وا.Command Windows ا 7 H type 9!@ D ( (B4 ض G$; ا% ا4?. (١ (٢ (٣ (٤ (٥ ٤
البرامج النصية Scripts $#ا! ب ل( ١ ): %This Program calculates the area of a rectangle %First the length & width is assigned length_rect=6 width_rect=2 % The area is calculated based on the length &width area= length_rect* width_rect ا' ا:ول $98 +(/7 و 56 ا'& +&#أ آ $98 ';% +*(م ا'& %ب,')- ٦ و,/ ٢ و-& ه 2! ا'*. ٥
البرامج النصية Scripts ل( ٢ ): #$9+ ا'& ا' 8 '% '#< وا'@ج %This Program calculates the area of a rectangle %First the length & width is assigned length_rect = input( Enter the length of rectangle: ) width_rect= input( Enter the width of rectangle: ) % The area is calculated based on the length &width area= length_rect* width_rect; ا' C ' input +$ض ا' A ا K &س. : ) is disp( the area of rectangle I H ا' G ( disp(area) M>#C ا' ا'* G H+;@+ L ا' G @#م N ا' G %(ل ا' P)G (د $ض و C, disp +*(م ا' A Q +ر ا' G واة. C (اء آن %(ل أو F. ٦
البرامج النصية Scripts ل( ٣ ): #$9+ ا'& ا' 8 '% 8U ا'@ج %This Program calculates the area of a rectangle %First the length & width is assigned length_rect = input( Enter the length of rectangle: ) width_rect= input( Enter the width of rectangle: ) % The area is calculated based on the length &width area= length_rect* width_rect; fprintf( the area of rectangle is : % f \n, area) A/) 9 #ل %f Q ا'( VC و(,. +$ض ا' A fprintf ا' ا' N%G, و/ MUG ا'* ا'$#د+..#+#P 9 #ل \n Q ا W *ل ' ٧
البرامج النصية Scripts %#دات ا' A/)G ا:آ - وا C @#ا : ا' QU$G أ "اد L;; أ "اد ا!L ا45 ;رف و<"ة :; N%O% ا'; %d %f %c %s ٨
البرامج النصية Scripts ل( ٤ ) +*(م ا'& ا'' %ب ا'* G ا'*+& ': %the approximate value of integral I f(x)=1/x^3, x belong to [1 3] %First the vectors are assigned a=1; b=3; x=[a b]; fprintf( % f \n, x) y=x.^(-3); fprintf( % f \n, y) I=(b-a)*(y(1)+y(2))/2; %the approximate value of integral I. fprintf( the approximate value of integral :% f \n, I) ٩
١٠
البرامج النصية Scripts والرسوم البيانيةPlotting (١١ ٤٨) N G\ ا' A/)G ا'' ا'& +*(م ل( ٥ ) C %This is a really simple plot of just one point! %Create coordinate variables and plot a red star x=11; y=48; '(ن plot ا' G %ف * plot(x, y, 'r*').(١١ ٤٨) %Change the axes and label them axis([9 12 35 55]) xlabel('time') xlabel ylabel('temperature') %Put a title on the plot title('time and Temp') +a ا' A ' G %ور. H+وU ylabel U (ان 'C. +C ا' A ا: G N ا' A/)G title axis +%#د ا' A ا'* Q ا' G %ور. +a ا' A ١١
البرامج النصية Scripts والرسوم البيانيةPlotting %#دات ا:'(ان: ا'(ن ا'; ا'(ن ا';!@Cا m أزرق b y أ ST اL7! g k أ< 4 ا 7 %د r c اV7 ان w ١٢
البرامج النصية Scripts والرسوم البيانيةPlotting %#دات ا'(ز: ا'$ I ا'; ا'$ I ا'; s دا 5 ة o 4 * ( d N!%X G$ W) 4 %"ا% v h ر G$ W) < %4T 4 p (4 G$ W) > + إ 0 رة 4ا A X G$ W) - إ 0 رة ا]ح ^ x Oا x [3. ١٣
البرامج النصية Scripts والرسوم البيانيةPlotting %#دات أ(اع ا'@(ط: (ع ا'@ V [3$ \3@ [3$ \3@ 4$ ا'; --.- : - ١٤
البرامج النصية Scripts والرسوم البيانيةPlotting ل( ٦ ) +*(م ا'& ا'' C ٣ U%U ت x = 0:pi/100:2*pi; y1 = 2*cos(x); y2 = cos(x); y3 = 0.5*cos(x); plot(x,y1,'--',x,y2,'-',x,y3,':') xlabel('0 \leqx\leq2\pi') ylabel('cosine functions') title('typical example of multiple plots') ١٥
البرامج النصية Scripts والرسوم البيانيةPlotting 9 (ا A 5 #ة ' C (م ا'&: و 56, Figure Window N] آN ;$ت ا@: 9 ة %ا ^@ : 9 ة %ا Window Figure :ر_ : ا@: 9 ة Figure Window "4 @;@4ا ;ا? 4ا 3ا % T ]ط BCا a 4 %ا ر[ (4 G ض ا :;4ا ا 44 رة 4) : 9 ة ا% و: 3 $> ا 4 @;@ت : ا@: 9 ة ا' A clf figure hold on hold off grid on grid off Legend ١٦
١٧
المصفوفات Matrices ;"د rot90 $ "دي إذا آن إ: و%\ Te ا"د N ا4!: "ور ١٨٠ در "د ات ا"وران :4 4 ه 9 ا >>rot90(a,2) "ور 3 رب ا N :!4ا هN B4) C' وVC ذ' f C @#ام HG+ >>rot90(a, -1) آ? B4) < 9 ف @L )!: U HG+ W و# 2 ف آ G (د أو آ 'H C 2 ف HG+ >>A(:,2)=[] >>A(3,:)=[] ١٨
المصفوفات Matrices 3 A = 1 1 $دWت > إ+\د GP ه N HO) 4 ا 4 دiت [#ا $ا @" (B$ 3x 1 + 2x 2 x 3 = 10 -x 1 + 3x 2 + 2x 3 = 5 x 1 x 2 x 3 = -1 2 1 3 2 1 1 x x = x x 1 2 3 10 b = 5 1!ض: Ax=b أن :<$B (B4 95"@ ١٩
المصفوفات Matrices Ax=b A -1 Ax=A -1 b x=a -1 b :$ا ::! NB $دWت > إ+\د GP N> 4 ا 4 دiت [#ا %$#"ام 3 ب >> A = [ 3 2-1; -1 3 2; 1-1 -1]; >> b = [ 10; 5; -1]; >> x = inv(a)*b x = -2.0000 5.0000-6.0000 MATLAB : C% C[ (B4 ٢٠
٢١
الاختيار Selection 0 ط if ا'$&رة if ا'$&رة Cرات end 0 ط if else end Cرات( 1 ) Cرات( 2 ) if-else ٢٢
الاختيار Selection ا' i وط '& U ء %ج ذا ا'$ ji * ا' G $ Iت ا' QU$G ا' G $ ا' QU$G ا' G $ أآ& =< +وي أو أآ& > أlm => +وي أو أlm < W +وي == +وي ~= ا' UG * ا' G $ Iت ا' QU$G ا' G $ ا' QU$G ا' G $ أو أو L@ :!4ا && و & و L@ :!4ا!@ا ~ ٢٣
الاختيار Selection ا'* G '$#د+ H N G '25U ل( ١ ) اآ ا:وا ا' Iز ل آن ا' G *(م, W +وي ا' 5 x=input( x= ); y=input( y= ); if y~=0 div=x/y fprintf( %5.2f / %5.2f = % 5.2f \n, x,y,div) end ٢٤
الاختيار Selection ل( ٢ ) #$9+ ا' G ل ا' 8 %n Mo9 رC ' 9(/7 #م إ ا'* G إذا آن ا' G *(م, $#وم x=input( x= ); y=input( y= ); if y ~= 0 div = x/y fprintf( %5.2f / %5.2f = % 5.2f \n, x, y, div) else fprintf( the division has not been able \n ) end ٢٥
الحلقات Loops for ا'%* ا'$&رات اآ for ل 3ا = $;ل 3;ا Cرات end إ' Q ٥ H ا' Iز '%ب \ G (ع ا: #اد s=0; for i = 5 : 20 s = s + i ; end fprintf( The sum from 5 to 20 = %d\n,s) ل( ٣ ) ٢٠ ٢٦
0 ط while Cرات end while s=0; i=5; الحلقات Loops,N while ا'%* ا'%* وا C @#م ا' 8 ا' G ل #ل ل( ٤ ) while i<=20 s=s+i; end i=i+1; fprintf( The sum from 5 to 20 = %d\n,s) ٢٧
٢٨
) التوابع Functions Function M-file < ;ي \[#.$ا Function) آ A9 اNew7 < File ا 3 453ا mn : 9 ة ا 4 ;ر.Editor وه (١ ا% ا$ و%]ء ا#ج ا$و و%]ء NT"ا function[ output_args] = Untitled( input_args) %UNTITLED Summary of this function goes here % Detailed explanation goes here end m $ا $ا p و q! ا$ ٢٩
Functions عباوتلا تoI ل) آ A'ا Function) ( (١ < نأ نB %ا $ا هو r! %ا.?4ا (٢ C$ :إ $ا أ.ير$Tا (٣ ي; $ا 4 تارCا $ا <[ ( $ا.ه9!@ (٤ < نأ ي; $ا تارC ($ ء[%و ج#ا (٥ (B4 9!@ $ا ( D $آ G4%ا : ة9: او7ا Command Windows وأ (4? 4و ء[%ا (.(% ٢٦ ( )ا ٢٠١٢ ٣٠ ج 0ر.أ.ق
التوابع Functions 3 f(x)=x -1 ا' A %ب ا'' ا' A +*(م ل( ١ ) GK function [y ] = f( x ) %This function calculates the f(x)=x 3-1 y=x^3-1; end ٣١
التوابع Functions A9 HG+ آ ء دا< ا C # 9 A ($3D " (4 "$%i ء "$ 4ا A Tر $ا (ف Tرج ا4?) داT A "$ 4ا $ا (ف (4 r! (?4ا ٣٢
التابع الداخلي Internal Function ا' A +*(م ل( ٢ ) =x 0:2pi أ P H f(x)=sin(x) ا'' %ب ا' G\G (ع '(ا A function [sf ] = sumf( ) %This function calculates sum of sin(x) sf=0; x=0:pi/100:2*pi; for i=1:length(x) sf=sf+f(x(i)); end end function [y] = f( x ) y=sin(x); end اi % r! ($ ٣٣
التابع الخارجيFunction External <ر P ل( ٣ ) #$9+ ا' G ل ا' 8 '\$ ا' A function [sf ] = sumf( f ) %This function calculates sum of sin(x) sf=0; x=0:pi/100:2*pi; for i=1:length(x) sf=sf+feval(f,x(i)); end end sumf( sin ) "i A اi %$" ء ا#ر NB0 اi %$" ء ٣٤
رسم التوابع Drawing Functions HG/ 3 f(x)=x -1 ل( ٤ ) +*(م C ا' G \ل 10] [1 ا' A ا'' ا' A fplot('f ',[0,10], ':') axis([0 10 0 1000]) xlabel('0 \leq x \leq 10') ylabel('f(x)') title(' f(x)=x^3-1 ') ٣٥
٣٦
طريقة نيوتن Newton Method H9) *+- G+ ا' 2 ي ا' A function newton(f,df,x0,tol,n) %number of iterance iter=0; %the first value of function u=feval(f,x0); %the first value of function s derivative v=feval(df,x0); %the first error value err=abs(u/v); disp(' ') disp(' iter x f(x) df(x) xn+1-xn ') disp(' ') fprintf('%2.0f %12.6f %12.6f %12.6f\n',iter,x0,u,v) while (err>tol)&(iter<=n)&(v~=0) x1=x0-u/v; err=abs(x1-x0); x0=x1; u=feval(f,x0); v=feval(df,x0); iter=iter+1; fprintf('%2.0f %12.6f %12.6f %12.6f %12.6f\n',iter,x0,u,v,err) end if (v==0) disp(' division by zero') end if (iter>n) disp(' Method failed to converge') end ٣٧
طريقة نيوتن Newton Method H9) *+- 8&9 ل Q f= 2x3 1 أو ف ا df = 6 x2 ف ا n و =10 و tol=1e-04 x0=0.5 ا د " newton را! ا >> newton('f','df',x0,tol,n) - أن! +ل ا&%$() ا' واا' &%$ة >> newton('f','df',0.5,1e-04,10) 1 ه :./ 0 12 iter x f(x) df(x) xn+1-xn 0 0.500000-0.500000 2.000000 1 0.750000 0.125000 3.000000 0.250000 2 0.708333 0.003472 2.833333 0.041667 3 0.707108 0.000003 2.828431 0.001225 4 0.707107 0.000000 2.828427 0.000001 ٣٨
طرق ا يجاد الجذورMethod Finding Root NB Dق ( 3D إد ا 9 ور <%@4ا اآ$> $ا @? ا 4 ل.(ا! u ا 7 و A ) ا ا#D^. (ا! u ا() ا@ 3 ] ا($. (ا! u ا(() ا D3. (ا! u اا) u:) ("3ا."4ا ( ٤٠