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
Matlab Primer(CSE)
BrenkMundaniMuntean Introduction to Matlab (CSE)
Markus BrenkRalf MundaniIonel Muntean
Scientific Computing in Computer ScienceTechnische Universitat Munchen
21. Oktober 2005
Brenk Mundani Muntean (TUM) Matlab Primer (CSE) 21. Oktober 2005 1 / 38
Matlab Primer(CSE)
BrenkMundaniMuntean
Schedule of the next two days
Thursday, October 20:9:30 – 12:30 interactive lecture12:30 – 13:30 lunch break13:30 – 16:30 supervised individual work
Friday, October 21:9:30 – 12:30 interactive lecture12:30 – 13:30 lunch break13:30 – 15:30 supervised individual work15:30 – 16:30 organisation of the scientific computing lab
Brenk Mundani Muntean (TUM) Matlab Primer (CSE) 21. Oktober 2005 2 / 38
Brenk Mundani Muntean (TUM) Matlab Primer (CSE) 21. Oktober 2005 28 / 38
Matlab Primer(CSE)
BrenkMundaniMuntean
Vector func.
Matrix func.
Mat. notation
Strings,messages
Command line
Efficiency
Graphics II
Text strings, error messages, input cont.
>> disp(’Hello world!’);>> disp (’The value of pi is:’), disp (pi)>> val = input(’Pleace enter a number:’)>> error(’Sorry, the value is out of range!’)>>
Exercise 8
Write a M-file that counts the number of elements betweentwo blancs resp. the begin and the end of a given string!
Brenk Mundani Muntean (TUM) Matlab Primer (CSE) 21. Oktober 2005 29 / 38
Matlab Primer(CSE)
BrenkMundaniMuntean
Vector func.
Matrix func.
Mat. notation
Strings,messages
Command line
Efficiency
Graphics II
M-files II (function files)
function a = square area(e)% SQUARE AREA. Area of a square.% SQUARE AREA(E) is the area of a square.% E is the lenght of a edge.
a = e*e;
% end of square area
>> area = square area(2.0)>> help square area
Brenk Mundani Muntean (TUM) Matlab Primer (CSE) 21. Oktober 2005 30 / 38
Matlab Primer(CSE)
BrenkMundaniMuntean
Vector func.
Matrix func.
Mat. notation
Strings,messages
Command line
Efficiency
Graphics II
M-files II (function files) cont.
function [vol,diag] = cube info(e,d)% CUBE INFO. Volume and length of the diagonal of% a cube.%% [VOLL,DIAG] = CUBE INFO(E,D) produces the volume% of a cube VOL and the length of diagonal of the% cube. Where E is the length of a edge of a% D-dimensional cube.
Brenk Mundani Muntean (TUM) Matlab Primer (CSE) 21. Oktober 2005 31 / 38
Matlab Primer(CSE)
BrenkMundaniMuntean
Vector func.
Matrix func.
Mat. notation
Strings,messages
Command line
Efficiency
Graphics II
M-files II (function files) cont.
function [vol,diag] = cube info(e,d)% CUBE INFO. Volume and length of the diagonal of% a cube.%% [VOL,DIAG] = CUBE INFO(E) produces the volume of% a cube VOL and the length of diagonal of the cube.% Where E is the length of a edge of the cube.%% [VOLL,DIAG] = CUBE INFO(E,D) produces the volume% of a cube VOL and the length of diagonal of the% cube. Where E is the length of a edge of a% D-dimensional cube.
...
Brenk Mundani Muntean (TUM) Matlab Primer (CSE) 21. Oktober 2005 32 / 38
Matlab Primer(CSE)
BrenkMundaniMuntean
Vector func.
Matrix func.
Mat. notation
Strings,messages
Command line
Efficiency
Graphics II
M-files II (function files) cont.
...if (nargin < 2)
d = 3;end
vol = eˆd;diag = e * sqrt(d);
% end of cube info
>> [vol, diag] = cube info(1.5)
>> type cube info>> type flops>> type rank
Brenk Mundani Muntean (TUM) Matlab Primer (CSE) 21. Oktober 2005 33 / 38
Matlab Primer(CSE)
BrenkMundaniMuntean
Vector func.
Matrix func.
Mat. notation
Strings,messages
Command line
Efficiency
Graphics II
Comparing efficiency of algorithms: flops and etime
>> n = 10;>> A = rand(n)>> x = rand(n,1)>> b = rand(n,1)>> flops(0); A*x; flops>> t = clock; x = A\b; time = etime(clock,t)>> clear n, x, b, A
Brenk Mundani Muntean (TUM) Matlab Primer (CSE) 21. Oktober 2005 34 / 38
Matlab Primer(CSE)
BrenkMundaniMuntean
Vector func.
Matrix func.
Mat. notation
Strings,messages
Command line
Efficiency
Graphics II
Graphics II
close all[X,Y] = meshgrid(-10:.2:10, -10:.2:10);Z = sinsinc(0.0,X,Y);surf(X,Y,Z);
function f = sinsinc (t,x,y)r = sqrt(x.ˆ2+y.ˆ2) + eps;f = cos(t)*sin(r)./r;
Exercise 9
Print the surface plot into an eps-file!
Brenk Mundani Muntean (TUM) Matlab Primer (CSE) 21. Oktober 2005 35 / 38
Brenk Mundani Muntean (TUM) Matlab Primer (CSE) 21. Oktober 2005 37 / 38
Matlab Primer(CSE)
BrenkMundaniMuntean
Vector func.
Matrix func.
Mat. notation
Strings,messages
Command line
Efficiency
Graphics II
Graphics II cont.
>> movie(M)>> movie2avi(M,’∼/matlab/sinc.avi’)
Exercise 11
Print the different figures to ’Portable Network Graphic (PNG)’files instead of creating the movie! Modify the name of the fileaccording to the loop index.
Brenk Mundani Muntean (TUM) Matlab Primer (CSE) 21. Oktober 2005 38 / 38