300 likes | 419 Views
Basis of Mathematical Modeling. LECTURE 1 Computation and visualization in MATLAB. Dr. N.K. Sakhnenko, PhD, Professor Associate. Outline. History Useful links and References Accessing MATLAB Matrices and Matrix Operations Two-Dimensional Graphics Three-Dimensional Graphics.
E N D
Basis of Mathematical Modeling LECTURE 1 Computation and visualization in MATLAB Dr. N.K. Sakhnenko, PhD, Professor Associate
Outline • History • Useful links and References • Accessing MATLAB • Matrices and Matrix Operations • Two-Dimensional Graphics • Three-Dimensional Graphics
Introduction http://www.mathworks.com/ MATLAB, developed by The MathWorks, Inc., integrates computation, visualization and programming in MATLAB and companion toolboxes provide engineers, scientists, mathematicians, and educators with an environment for technical computing applications. These products serve a broad range of tasks across a variety of industries from automotive and electronics to industrial equipment and telecommunications.
History • MATLAB was invented in the late 1970s by Cleve Moler, the chairman of the computer science department at the University of New Mexico. It soon spread to other universities and found a strong audience within the applied mathematics. • Jack Little, an engineer, recognizing its commersial potential, joined with Moler. They founded The MathWorks in 1984 to continue its development. • MATLAB was first adopted by control design engineers, but quickly spread to many other domains. It is now also used in education, in particular the teaching of linear algebra and numerical analysis, and is popular amongst scientists involved with image processing.
Useful links www.mathworks.com Free online books http://www.math.utah.edu/lab/ms/matlab/matlab.html www.math.mtu.edu/~msgocken/intro/intro.html MATLAB related links, tools, libraries, and resources for scientists and engineers. http://www.mathtools.net/MATLAB/index.html http://www.mathworks.com/products/demos/ - Matlab Demos.
References The MathWorks, Inc, MATLAB. Getting Started Guide. T. Davis and K. Sigmon, MATLAB Primer, Chapman & Hall\CRC, 2005 K. Chen, J. Giblin, A. Irving, Explorations with MATLAB, Cambridge University Press, 2006
Accessing MATLAB The MATLAB Desktop In Microsoft windows we can enter MATLAB with double-click on the MATLAB icon
Command Window The command window is where you can interact with MATLAB directly. Any output is immediately printed to the window. Expression and statements are usually of the form: variable=expression or simply: expression >> a=6-4 <Enter> a =2 >> 1+2 <Enter> ans =3 >> a*ans <Enter> ans =6 If the variable name and = sign are omitted, a variable ans (for answer) is automatically created to which the result is assigned.
Matrices Entering a Matrix: MATLAB=Matrix Laboratory!!! >> A=[1 2 3;0 1 -5;0 0 1] A = 1 2 3 0 1 -5 0 0 1 >> a=[1,2,4;5,0,-1;6,0,2] a = 1 2 4 5 0 -1 6 0 2 >> A(2,3) ans = -5 • MATLAB is case-sensitive in the names of commands, functions and variables, so A and a are two different variables. • A coma or blank separates the elements within a row. A semicolon ends a row. • Individual matrix entries can be referenced with indices inside parentheses.
Matrix Operators • The following matrix operators are available in MATLAB: • + addition • subtraction • * multiplication • ^ power • ‘ transpose • \ left division • / right division • inv inversion • det finding of the determinant
Systems of linear equations If the last character of a statement is a semicolon, display of the result is suppressed, but the assignment is carried out. This is essential in suppressing unwanted display of intermediate results. >> A=[1.2 0.3 -0.2; 0.5 2.1 1.3; -0.9 0.7 5.6]; >> b=[1.3; 3.9; 5.4]; >> x=inv(A)*b x = 1.0000 1.0000 1.0000
How to control output All computations in MATLAB are done in double precision (about 15 digits accuracy). The format of the displayed output can be controlled by the following commands: FORMAT SHORT Scaled fixed point format with 5 digits FORMAT LONG Scaled fixed point format with 15 digits FORMAT SHORT E Floating point format with 5 digits FORMAT LONG E Floating point format with 15 digits FORMAT SHORT G Best of fixed or floating point format with 5 digits FORMAT LONG G Best of fixed or floating point format with 15 digits FORMAT BANK dollars and cents FORMAT SHORT is the default. Our can change output format with >File >Preferences
MATLAB functions MATLAB provides a large number of standard elementary mathematical functions. MATLAB also provides many more advanced mathematical functions. For a list of the elementary mathematical functions, type help elfun For a list of more advanced functions, type help specfun help elmat >> x=exp(-25)*log(7.4)/7 ; >> y=(sin(4.45*pi)+cos(3.78*pi))/tan(7) ; >> z=x+y^2 z = 4.0706
Entry-by-entry operations Note that “.*” (dot-star) is for entry-by-entry multiplication; ”.^” (dot-power) is for entry-by-entry power. For example, either: [1 2 3 4].*[1 2 3 4] [1 2 3 4].^2 will yield [1 4 9 16] . Try it. Entry-by-entry operations are particularly useful using MATLAB graphics! The colon operator The colon “:” is used to generate matrixes. The expression [1:0.2:2] is a row vector 1.0000 1.2000 1.4000 1.6000 1.8000 2.0000
Two-Dimensional Graphics Enter the commanddemoand select some of the visualization and graphics demos! >>x=[0:0.05:2*pi]; >> y=sin(x.^2); >> plot(x,y); The plot command creates linear x-y plot, if x and y are vectors of the same length.
Two-Dimensional Graphics >> x1=[-2:0.01:-1]; >> y1=-2-x1; >> x2=[-1:0.01:1]; >> y2=x2; >> x3=[1:0.01:2]; >> y3=2-x3; >> x=[x1 x2 x3]; >> y=[y1 y2 y3]; >> plot(x,y)
Parametrically defined curves >> t=[0:0.01:4*pi]; >> x=t-sin(t); >> y=1-cos(t); >>plot(x,y);
Graphics in polar coordinates >> fi=[0:0.01:2*pi]; >> ro=3*(1-cos(fi)); >> polar(fi,ro);
Titles, labels, line types, marker types, colors The graphs can be given titles, axes labels and text placed within the graph with the following commands: title graph title xlabel x-axis label ylabel y-axis label
Two-Dimensional Graphic >>x=[0:pi/20:2*pi]; >> f1=cos(2*x); >> f2=cos(3*x); >> plot(x,f1,x,f2) >> grid on >> title('Figure 1') >> xlabel('x') >> ylabel('y') >> legend ('y=cos(2x)','y=cos(3x)')
Plotting multiple data sets in one graph >> x=[-2*pi:0.1:2*pi]; >> y1=cos(x); >> y2=cos(x).^2; >> y3=cos(x).^3; >> plot(x,y1,'r.',x,y2,'k:',x,y3,'--ok')
Subplots The command subplot(m,n,p) partitions a single figure into an m-by-n array of panes, and make pane p the current plot. The panes are numbered left to right. >> fi=[0:0.01:2*pi]; >> ro_1=(cos(3*fi)); >> subplot(2,2,1); >> polar(fi,ro_1); >> ro_2=(sin(3*fi)); >> subplot(2,2,2); >> polar(fi,ro_2); >> ro_3=(cos(5*fi)); >> subplot(2,2,3); >> polar(fi,ro_3); >> ro_4=(sin(5*fi)); >> subplot(2,2,4); >> polar(fi,ro_4);
Diagrams >> a=[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8]; >> b=[1.2 2.3 2.0 1.2 1.6 0.7 0.4 1.9]; >> bar(a,b) >>x = [-2.9:0.2:2.9]; >>bar(x,exp(-x.*x))
Circular diagram Circular diagrams display the percentage that each element in a vector or matrix contributes to the sum of all elements. >> c=[19.5 13.4 42.6 7.9]; >> pie(c)
Three-Dimensional Graphics >> [X,Y]=meshgrid(-3:0.1:3,-3:0.1:3); >> Z=sin(X)./(X.^2+Y.^2+0.3); >> mesh(X,Y,Z) >> surf(X,Y,Z) >> shading interp
Three-Dimensional Graphics >> [X,Y]=meshgrid(-1:0.05:1,0:0.05:1); >> Z=4*sin(2*pi*X).*cos(1.5*pi*Y).*(1-X.^2).*Y.*(1-Y); >> surf(X,Y,Z) >> shading interp >> contourf(X,Y,Z) >> colorbar contourf(Z) draws a contour plot of matrix Z, where Z is interpreted as heights with respect to a plane.
Three-Dimensional Graphics >> x = [-2:.2:2]; y = [-1:.2:1]; >> [xx,yy] = meshgrid(x,y); >> zz = xx.*exp(-xx.^2-yy.^2); >> mesh(xx,yy,zz) >> contour(xx,yy,zz) >> [px,py] = gradient(zz); >> quiver(x,y,px,py)