< 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.
»
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>
» x=3
x =
3
» y=7
y =
7
» x*y
ans =
2
Try typing
» x=0:.01:2;This is more interesting: it creates a new window with the graph of y=e -x^2 from x=0 to x=2.
» y=exp(-x.*x);
» plot(x,y)
»
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:2and 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!
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;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.
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)
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
» bobYou 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."