1 - Stanford University



CME100 V. Khayms

Vector Calculus for Engineers Fall 2004

MATLAB Tutorial, Part 1

MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation. Typical uses include:

• Math and computation

• Algorithm development

• Modeling, simulation, and prototyping

• Data analysis, exploration, and visualization

• Scientific and engineering graphics

• Application development, including Graphical User Interface building

MATLAB is an interactive system whose basic data element is an array that does not require dimensioning. This allows you to solve many technical computing problems, especially those with matrix and vector formulations, in a fraction of the time it would take to write a program in a scalar noninteractive language such as C or Fortran.

MATLAB has evolved over a period of years with input from many users. In university environments, it is the standard instructional tool for introductory and advanced courses in mathematics, engineering, and science. In industry, MATLAB is the tool of choice for high-productivity research, development, and analysis.

MATLAB features a family of application-specific solutions called toolboxes. Very important to most users of MATLAB, toolboxes allow you to learn and apply specialized technology. Toolboxes are comprehensive collections of MATLAB functions (M-files) that extend the MATLAB environment to solve particular classes of problems. Areas in which toolboxes are available include signal processing, control systems, neural networks, statistics, fuzzy logic, wavelets, partial differential equations, simulation, and many others.

MATLAB is available on many workstations throughout campus. To start MATLAB on a Windows system, log onto any of the workstations and double-click on the MATLAB icon. On a UNIX system, type matlab at the operating system prompt. A command window will appear with the MATLAB prompt >> (or EDU>> in the student edition) indicating that the program is ready to receive instructions.

1. Basic operations

a) Using MATLAB as a calculator

>> (2+3)*4/5

ans =

4

b) Defining scalar variables

>> a=2

a =

2

>> b=3

b =

3

>> c=a+b

c =

5

>> c=a+b;

>> % This is a comment

c) Defining arrays and using array elements in computation

>> a=[1 2 3]

a =

1 2 3

>> a(1)

ans =

1

>> a(2)

ans =

2

>> a(3)

ans =

3

>> x=a(1)*a(2)

x =

2

d) Appending arrays

>> a

a =

1 2 3

>> b=[3 4]

b =

3 4

>> c=[a,b]

c =

1 2 3 3 4

2. Plotting

a) Basic plotting

>> x=0:0.1:15; % creates an array % of x values

>> % can also use x=linspace(0,15,100);

>> y=sin(x).*x; % creates an array

% of y values

>> plot(x,y)

Adding grid and labels

>> grid on; % adds grid

>> xlabel('Time'); % adds x-axis

% label

>> ylabel('Position'); % adds y-axis

% label

>> title('Pretty plot'); % adds title

[pic]

Adding data sets on a single plot

>> z=cos(x).*x.^(0.5);

>> hold on

>> plot(x,z,'--');

>> hold off

>> close % closes window

[pic]

Multiple plots in one figure

>> subplot(2,2,1);

>> plot(x,y);

>> grid on

>> subplot(2,2,2);

>> plot(x,z,'--');

>> grid on

>> subplot(2,2,3);

>> plot(y,z);

>> grid on

[pic]

b) Getting help !

>> help plot

>> lookfor plot

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download