1 / 17

עבודה ב- MATLAB

עבודה ב- MATLAB. תרגול מספר 1. Matlab היא סביבה אינטראקטיבית לחישוב מדעי והנדסי, סימולציה, ויזואליזציה ותכנון אלגוריתמים . איך למצוא מידע נוסף : פקודות help, helpwin, helpdesk, demo מתוך ה - Matlab אם לדוגמא ברצונכם לקבל עזרה על הפקודה mean >> Help mean באינטרנט:

elvin
Download Presentation

עבודה ב- MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. עבודה ב- MATLAB תרגול מספר 1 אנליזה נומרית 1

  2. Matlab היא סביבה אינטראקטיבית לחישוב מדעי והנדסי,סימולציה, ויזואליזציה ותכנון אלגוריתמים. איך למצוא מידע נוסף: פקודות help, helpwin, helpdesk, demoמתוך ה -Matlab אם לדוגמא ברצונכם לקבל עזרה על הפקודה mean >> Help mean באינטרנט: http://www.mathworks.com/support/ העבודה הבסיסית בMatlab - אנליזה נומרית 1

  3. »Help eig EIG Eigenvalues and eigenvectors. E = EIG(X) is a vector containing the eigenvalues of a square matrix X. [V,D] = EIG(X) produces a diagonal matrix D of eigenvalues and a full matrix V whose columns are the corresponding eigenvectors so that X*V = V*D. » lookfor differentiation NUDERST Selects the step size for numerical differentiation DIFF Alternative entry to the symbolic differentiation function. In unix: !ls or !mkdir כדי לקבל עזרה על פונקציה: help func_name כדי לחפש פונקציה ששמה לא ידוע lookfor keyword כדי לקבל כלי עזרה אינטראקטיביhelpwin/helpdesk איזה משתנים קיימים בשדה העבודהwhos פקודות למ"ה : !command עזרה ב- matlab אנליזה נומרית 1

  4. מטריצות ב- Matlab .מטריצות הן מבנה הנתונים העיקרי כדי לבנות מטריצה פשוט כותבים: • a=[0.2,7,11,5] - וקטור-שורה (אופרטור , שרשור לאורך שורה) • x=5:2:11 -וקטור ]5,7,9,11[= x (אופרטורa:b:c ) • m=[2.1;66;11;9]עמודהוקטור • A=[3 4 5; 4 7 9; 2 6 7] - 3X3 מטריצה • הרכבת מטריצה מתת מטריצותB=[a b;c d] • A=B(3:5, 4:7)מטריצה-תתהעתקת • הוצאת עמודות מסוימות ממטריצהA=B(:,[1 3 2 4]) • A=B’ - (transpose)שחלוףמטריצה • יצירת משתנה STRING : w=‘I am a string’ אנליזה נומרית 1

  5. >> format short (default) >> x=31.415926 x = 31.4159 >> format long >> x x = 31.41592600000000 >> format long e >> x x= 3.141592600000000e+001 >> format short e >> x x = 3.1416e+001 הפקודה formatמגדירה את הצגת המספרים אנליזה נומרית 1

  6. פעולות עם מטריצות • יצירת מטריצות ייחודיות: (zeros(3,5), rand(5,1) ones(6,2),eye (6,2 • פקודות על מטריצות:+,-,*(כפל מטריצי),/(חילוק מטריצי),^ a^2 אוa+b או A*x-y • פקודות על אברים מתאימים במטריצותelement wise : a./b אוa.^2 או a.*b • הפעלת פונקציות:(sqrt(x), sin(y), exp(a+ib), result=isempty(a • [v,d] = eig(x); • S=sum(A) % S is a row vector of the columns sums of A • S=sum(sum(A)) % sum of matrix A • [xsize ysize]=size(M) אנליזה נומרית 1

  7. The system of equations: 3*x-5*y=-44 2*x+y=27 In Matlab: >> A=[3,-5 ; 2,1]; >> b=[-44 ; 27]; >> s=inv(A)*b (or s=A\b) s = 7.0000 13.0000 >> x=s(1); >> y=s(2); דוגמא לפתרון מערכת משוואות לינאריות אנליזה נומרית 1

  8. פונקציתfind ותנאים לוגיים • תנאים לוגיים == , < , > , (not equal)~= ,(not)~ • find(a==3) מחזירה אינדקסים בa לאיברים שמקיימים את התנאי הלוגי. הפונקציה מחזירה אינדקס יחיד במטריצה (שרץ לאורך העמודות) או זוג אינדקסים (x,y) (תלוי במספר פרמטרי החזרה) • a=[1 2 3;4 5 6;7 8 9] • a = • 1 2 3 • 4 5 6 • 7 8 9 • » z=find(a>7) • z = • 6 • 9 • » [i,k]=find(a>7) • i = • 3 • 3 • k = • 2 • 3 אנליזה נומרית 1

  9. חישובים עם דיוק מסוים • Variable precision arithmetic • Syntax R = vpa(A) R = vpa(A,d) • Description vpa(A) uses variable-precision arithmetic (VPA) to compute each element of A to d decimal digits of accuracy, where d is the current setting of digits. Each element of the result is a symbolic expression. vpa(A,d) uses d digits, instead of the current setting of digits. דוגמות: digits(25) q = vpa(sin(sym('pi')/6)) vpa('sin(pi/3)^2',2) אנליזה נומרית 1

  10. plot(x,y), plot(x,sin(x)) %plot a function figure , figure(k)%open a new figure hold on, hold off subplot(3,1,2) %several plots in figure axis([xmin xmax ymin ymax]) title(‘figure title’) %add title to figure Visualization and Graphics אנליזה נומרית 1

  11. דוגמא לגרף פשוט • x=0:0.01:5; • y=sin(x).*cos(50*x); • plot(x,y) • title(‘Example plot’) • xlabel(‘x’) • ylabel(‘y=sin(x).*cos(50*x)’) אנליזה נומרית 1

  12. פקודת if: if (A > B), statement; elseif (A< B), statement; elseif ~A,statement; else, statement; end ifi==1, statement; end ifres(n,2) ~= 0, statement; else, statement; end בקרת זרימה אנליזה נומרית 1

  13. פקודת for • לולאה פשוטה: forn=1:1:4, subplot(2,2,n) plot(a(:,1),a(:,n+1)) title(num2str(n)) end אנליזה נומרית 1

  14. פקודות while a = 4; fa=sin(a); b = 2; fb = sin(b); while a - b > 5 * eps, x = (a+b)/2; fx = sin(x); if sign(fx) == sign(fa), a = x; fa=fx; break; else b = x; fb = fx; end end אנליזה נומרית 1

  15. פונקציות ב- Matlab נכתוב פונקציה pyt.m function[c,d,e]=pyt(a,b) % returns the hyotensus (yeter) in a right angle % triangle according to Pythagoras theorem % c is the hyotensus % d and e are the two sharp angles c=sqrt(a.^2+b.^2); d=atan(b/a); e=pi/2-d; אנליזה נומרית 1

  16. בד”כ לא נוח לעבוד בצורה אינטראקטיבית בלבד מאבדים את כל מה שנעשה ביןsession ל - session ניתן לעבוד בעורך (editor) המועדף או בעורך של matlabע”י edit. כדי להפעיל my_m_file.m scriptפשוט כותבים בMatlab את השם my_m_fileוזה מריץ את הפקודות סדרתית בסביבה הגלובלית. פקודות שימושיות: load Mydata.dat- טוענת את תוכן הקובץMydata.dat למשתנהMydata whatמחזירה את השמות של קבציmatlab בcurrent directory s=sprintf(‘Mydata%d’,I) - זהה לאותה פקודה ב- C diary(‘diary_file’), diary offמקליט כל העבודה לקובץ . M-files אנליזה נומרית 1

  17. שמירת workspace • הפקודהwhos מציגה את המשתנים שבזיכרון ואת גודלם. • הפקודהsave file_name שומרת משתנים אלה בקובץfile_name.mat וניתן לשחזר משתנים אלה לתוך הזיכרון ע"יload file_name • ניתן גם להגדיר את הפורמט, לדוגמא: >> save Data.asc –ascii A >> save Data.asc –ascii –double A • ניתן להיעזר בפקודות save ו- load לצורך העברת נתונים בין MATLAB לתוכנות אחרות. לדוגמא יצרתם קובץ של נתונים ב- C וברצונכם להציגו גרפית ב- MATLAB. • הפקודהclear a מוחקת את משתנהa , הפקודהclear מוחקת את כל המשתנים שבזיכרון. אנליזה נומרית 1

More Related