Ed Bueler's Very brief intro to Matlab:


For Matlab cheechakos (first-timers).

I am assuming you are using Matlab at a command line.  For example, when you first start Matlab, you get a window with something like:

< M A T L A B (R) >
Copyright 1984-2009 The MathWorks, Inc.
Version ...

To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.

» 

Or if you start Octave you get

GNU Octave, version 3.2.3
Copyright (C) 2009 John W. Eaton and others.
...
For information about changes from previous versions, type `news'.

octave:1>

This is a supercalculator. you can do all the things you would do with a calculator plus other tasks involving fancy graphing and matrix problems and such. On the very simple end, you can type
» x=3
x =
     3

» y=7
y =
     7

» x*y
ans =
    2

Try typing

» x=0:.01:2;
» y=exp(-x.*x);
» plot(x,y)
»
This is more interesting: it creates a new window with the graph of y=e -x^2 from x=0 to x=2.

The line

» x=0:.01:2;
creates a list of the x-values of the points you want to plot. The ";" at the end stops Matlab from displaying the list. Try typing
» x=0:.01:2
and see what you get.

The line

» y=exp(-x.*x);
obviously creates the y-values for the points to plot. But there is a new issue--if x is a long list of numbers, then there is an ambiguity in how to multiply x by itself. "x.*x" produces a list of the squares of every number in the list x, but "x*x" produces a single number, which is the sum of the squares of the values in the x list. (This is an indication, if you are paying attention, that Matlab is useful for matrix and vector computations, since you frequently want to do both.)

The line

» plot(x,y)
plots the list of points with coordinates in the list x and the list y. Play with this!

The supercalculator aspect becomes more impressive when you manipulate vectors and matrices.  See Matrices in Matlab: Basic for continuing the tutorial in that direction.




Now, it is a different thing to want to write a program in Matlab . You need a new window, which will allow you to write and save a program, to be run at a later time.

You get that new window by either double-clicking on an existing ".m" file, or by choosing "New -> M-file" in the "File" menu of Matlab .

The new window has the title "MATLAB Editor/Debugger - [Untitled1]", or something like that. Your job in this new window is to create a program and save it with a sexy name in a place where Matlab can find it. Then in the original ("main") Matlab window you can run it just by typing its name

If you are using Octave you will want to open an editor.  Notepad or gedit will do.

Then, for this example, type in

n=100;
h=(2-0)/n;
x=0:h:2;
y=exp(-x.*x);
z=h*(y(1)/2+sum(y(2:n))+y(n+1)/2)
noting that there is a semicolon at the end of every line but the last.  As mentioned above, semicolons tell Matlab not to report its result at every stage.

Then save it in the right place, with a name of your choice.  The name should have a ".m" extension added on, so that the system knows it is a Matlab source code file.  I save it as "bob.m".

Then run it. You run it (once it is saved) by going to the main window and typing

» bob
You will get a solution to the question: "Use the trapezoid rule with 100 steps to calculate the integral of y=e-x^2 from x=0 to x=2."


doc info