<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE artikkel PUBLIC "-//infoweb.ntnu.no//DTD Infoweb 1.0//NO" "http://www.infoweb.ntnu.no/infoweb.dtd">
<artikkel spraak="en">
  <tittel>Matlab</tittel>
  <forfatter>livermor</forfatter>
  <sammendrag>Introduction to Matlab</sammendrag>  
  <lenker></lenker>

  <hoveddel>


	  <avsnitt>
		  MATLAB is an interactive program used for numerical calculations and visualization. It includes several commands and functions, and you can also make your own.
	  </avsnitt>
	  <avsnitt>
		  To better understand MATLAB, you can look at its name - which is a contraction for MATrix LABoratory. All calculations are based upon matrices and linear algebra. 
	  </avsnitt> 
	  <avsnitt>
		  The program is widely used in technology-studies at NTNU.
	  </avsnitt> 
	  
	  <avsnitt>
		  It is possible to widen MATLAB's use with 'toolboxes' to enable easier solving of problems in special topics. As an example, SIMULINK is a tool for simulating dynamical non-linear systems. NTNU provides the following toolboxes for employees connected to a license server: 
	  </avsnitt>
	  <ul> 
		  <li>Signal processing</li>
		  <li>Optimization</li> 
		  <li>Statistics</li> 
		  <li>Control Systems</li>
		  <li>Image Processing</li>
		  <li>Matlab Compiler</li>
		  <li>PDE Toolbox</li>
		  <li>Neural Networks</li>
	  </ul> 

	  <avsnitt>
		  For students, these are the available toolboxes:
	  </avsnitt>

<ul> 
<li>Signal processing</li> 
<li>Optimization</li> 
<li>Control Systems</li>
<li>Image Processing</li>
<li>SimPowerSystem</li>
<li>PDE Toolbox</li>
</ul>
 
<os2>Information about NTNU's license for MATLAB</os2>
<avsnitt>
As long as you are connected to the net, you can as a student or employee install and run MATLAB on your own machine. It is available from Progdist and Orakeltjenesten: <infoweb aid="491" tittel="Software at NTNU" >Software at NTNU</infoweb>. The license is retrieved from a server and allows the usage of the products listed above.
</avsnitt>

<os2>Opening and closing MATLAB</os2>
<avsnitt>
On a Windows machine or Macintosh, you will get a 'Matlab Command Window' when starting MATLAB. All calculations are done by commands in MATLAB, and graphics are displayed in a dedicated graphics window when needed, which means that you will have to 'command' MATLAB to open it first.</avsnitt> 
<avsnitt>
When starting MATLAB in a UNIX-environment, you type <kommando>matlab</kommando> in a terminal. The calculations are run in that terminal and graphics are displayed in another windows by X-Windows.</avsnitt>  
<avsnitt>
After MATLAB has started, you'll see a some introduction-text and <tegn symbol="#62"/><tegn symbol="#62"/> where you can enter commands.
</avsnitt> 
<avsnitt>
To exit MATLAB, you can either type <kommando>exit</kommando> or close the window.</avsnitt>

<os2>Demonstration of MATLAB</os2>
<avsnitt>
MATLAB has its own introduction slideshow and by typing the command <kommando>intro</kommando> it will run through some examples and you can view the .m-file to see which commands that have been used. A more in-depth demonstration can be shown by typing <kommando>demo</kommando>.
</avsnitt> 

<os2>Getting help</os2>
<avsnitt>
To get help using MATLAB, you can both use the dropdown-help in the menubar, or different commands in the 'Matlab Command Window': </avsnitt> 
<ul>
<li><uthev>help</uthev>
<br/>
Help using this command is displayed in the 'Matlab Command Window' by first displaying a list of topics you can get help on or you can write <kommando>help command_you_want_help_on</kommando>. As an example, by writing <kommando>help acos</kommando>, you'll get this:
<konsoll>
<tegn symbol="#62"/><tegn symbol="#62"/>help acos
ACOS 	Inverse cosine
	ACOS(X) is the arccosine of the elements of X. Complex results are obtained if ABS(X) > 1.0 for some element. 
	
Overloaded methods
	help sym/acos.m
</konsoll>
</li> 
<li><uthev>helpwin</uthev>
<br/>
Almost the same as <uthev>help</uthev>, but is displayed in a new window and is easier to navigate in.<br/></li> 
<li><uthev>helpdesk</uthev>
<br/>
Comprehensive hypertext documentation using a web browser. Takes some time to start and requires that you installed the documentation when installing MATLAB.
<br/></li> 
<li><uthev>lookfor</uthev>
<br/>
By typing <kommando>lookfor</kommando> with a topic, MATLAB will search both original and home-made functions. To learn more about <uthev>lookfor</uthev> use the command <kommando>help lookfor</kommando>.</li> 
<li><uthev>ver</uthev>
<br/>Gives information on what version you're using of MATLAB and toolboxes.<br/></li> 
<li><uthev>whatsnew</uthev>
<br/>By typing <kommando>whatsnew</kommando> you'll get information about the program from Readme.txt.</li>
</ul> 

<os2>Manuals</os2>
<ol>
<li>MATLAB - for all systems:</li> 
<ul>
<li>Getting Started With Matlab</li> 
<li>Using Matlab</li> 
<li>Using Matlab Graphics</li> 
<li>Application Program Interface Guide</li> 
<li>Building GUIs with Matlab</li> 
<li>Matlab Notebook User's Guide.</li> 
</ul>
<li>Using SIMULINK</li> 
</ol>
<avsnitt> 
By choosing "Full Documentation Set" on the bottom of the page we get when typing <kommando>helpdesk</kommando>, we'll get access to all manuals. They are shown in PDF by Acrobat Reader (UNIX: acroread).</avsnitt> 
<avsnitt>
In addition to this, you can get the manuals for MATLAB on MathWorks' homepage: <ekstern tittel="Documentation for MathWorks Product Family" uri="http://www.mathworks.com/access/helpdesk/help/helpdesk.shtml" /></avsnitt>


<os2>How to edit in a commandline</os2> 

<avsnitt>
<br/>
Get previous line:             		arrow-up, Ctrl-P (Unix, Windows, Mac)<br/>
Get next line:               		arrow-down, Ctrl-N (Unix, Windows, Mac)<br/>
Move one character to the left:        	arrow-left, Ctrl-B (Unix, Windows, Mac)<br/>
Move one character to the right:       	arrow-right, Ctrl-F (Unix, Windows, Mac)<br/>
Move left and delete a character:  	Delete (Unix), Backspace (Windows, Mac)<br/>
Move a word to the left:          	Ctrl-L (Unix), Ctrl-arrow-left (windows)<br/>
Move a word to the right:            	Ctrl-R (UNIX), Ctrl-arrow-right (Windows)<br/>
Move to the beginning of a line: 	Ctrl-A (Unix, Windows, Mac)<br/>
Move to the end of a line:          	Ctrl-E (Unix, Windows, Mac)<br/>
Abort existing line:            	Ctrl-U (Unix, Mac), Esc (Windows, Mac)<br/> 
Delete a character where your marker is:Ctrl-D (Unix, Windows, Mac), Del (Windows, Mac)<br/>
</avsnitt>

<os2>Examples</os2>
<avsnitt> 
We will try to give some examples of how to use MATLAB here, but for further use, search the web for your problem or ask a teacher. </avsnitt>
<avsnitt>
Adding a constant to all elements in a matrix: <br/>

<tegn symbol="#62"/><tegn symbol="#62"/> a = [1 2 3 4 5]<br/>
<tegn symbol="#62"/><tegn symbol="#62"/> b = a + 10<br/> 
b = [11 12 13 14 15]<br/>

To display a vector graphically: <br/>

<tegn symbol="#62"/><tegn symbol="#62"/> plot(b)<br/>
<tegn symbol="#62"/><tegn symbol="#62"/> bar (b)<br/>

The first command will give a line plots the vector as a line and the second as a bar chart.</avsnitt>

<avsnitt>
Calculations involving matrices: <br/>

<tegn symbol="#62"/><tegn symbol="#62"/>   A = [1 2 3; 4 5 6; 7 8 9]<br/>
<kode>
A=

    1   2   3
    4   5   6
    7   8   9
</kode> 

</avsnitt>
<avsnitt>
Transpose the matrix A and name it B: <tegn symbol="#62"/><tegn symbol="#62"/> B=A'<br/>
<kode>
B=

    1   4   7
    2   5   8
    3   6   9
</kode>
</avsnitt>
<avsnitt>
Multiplying two matrices: <br/>

<tegn symbol="#62"/><tegn symbol="#62"/> A * B
</avsnitt>
<avsnitt>
Multiplying each matrices elementwise:<br/>

<tegn symbol="#62"/><tegn symbol="#62"/> A .* B
</avsnitt>
<avsnitt>
Inverse matrix: <br/>

<tegn symbol="#62"/><tegn symbol="#62"/> inv(A)
</avsnitt>

<os2>Functions</os2>
<avsnitt>
Instead of interactive use of MATLAB, it is possible to make functions; a sequence of operations in an .m-file. Say there is something you want to do to several objects which uses several commands, it is then easier to write a function for it and use that instead of repeating the commands.
</avsnitt> 
<avsnitt>
By using a normal text editor you can make your own .m-files. Comments to explain the commands or what you do start with <tegn symbol="#37"/>. The first commentline in the function-file is what is shown when you use <kommando>lookfor</kommando>. The following comments in a function before an open line is shown by typing <kommando>help name_of_function</kommando>.
</avsnitt>
<avsnitt> 
To use a function, just type the name of it:<br/>
<tegn symbol="#62"/><tegn symbol="#62"/> name_of_file
</avsnitt>
<avsnitt>
We can choose where we want to save our functions, but if we want to use them we have to tell MATLAB where it can find it. As a standard on Windows, MATLAB looks for functions in the /work-part of the MATLAB-directory. You can change this if you want to keep your functions in another workspace.
</avsnitt> 

<os2>Toolbox</os2>
<avsnitt>
As mentioned earlier, you can add different toolboxes to MATALB to extend its functionality (a toolbox is a collection of functions). 
</avsnitt> 
<avsnitt>
To get information about a toolbox and its functions, type <kommando>help name_of_toolbox</kommando>:<br/>
<kode>
simulink/simulink    -  SIMULINK 
toolbox/control      -  Control System Toolbox.
images/images        -  Image Processing Toolbox.
nnet/nnet            -  Neural Network Toolbox.
nnet/nndemos         -  Neural Network Demonstrations and Applications.
toolbox/optim        -  Optimization Toolbox. 
toolbox/pde          -  Partial Differential Equation Toolbox.
toolbox/signal       -  Signal Processing Toolbox.
toolbox/stats        -  Statistics Toolbox.
</kode>
<br/>
</avsnitt>

<os2>Demonstration of toolboxes</os2>
<avsnitt>
To see demonstrations of how you can use MATLAB and its toolboxes, see <kommando>help</kommando> and the topics matlab/demos, simulink/simdemos, images/imdemos and nnet/nndemos in addition to <kommando>help name_of_toolbox</kommando>. 
</avsnitt>

<os2>Distributing your functions</os2>
<avsnitt>
MathWorks encourages distribution of useful home-made functions. You can find these on an anonymous catalog on their ftp server <ekstern tittel="ftp.mathworks.com" uri="ftp://ftp.mathworks.com/"/>(144.212.100.11).<br/>
 
You can access these catalogs on a normal web page as well: <ekstern tittel="The MATLAB Central file exchange." uri="http://www.mathworks.com/matlabcentral/fileexchange/index.jsp" brutt="nei"/> <br/> 

MathWorks also distributes a newsletter here: <ekstern tittel="The MathWorks MATLAB Digest" uri="http://www.mathworks.com/company/newsletters/" brutt="nei"/> (http://www.mathworks.com/company/newsletters/).</avsnitt>

<os2>Print</os2>
<avsnitt>
For printing graphical plots you can use the command <kommando>print</kommando>.
</avsnitt>
<avsnitt> 
You can also save a graphical plot in different formats, by using the command <kommando>print -djpeg name_of_file</kommando>. Or you can use <uthev>File</uthev>-<uthev>Export</uthev> where you will be given a choice of different file formats such as JPEG, TIFF, PNG and EPS.
</avsnitt> 
<avsnitt>
It is possible to print the calculation part as well, by <kommando>diary filename.txt</kommando>. Everything you then type in the "Matlab Command Window" will then be written to that file. You can turn this on and off by <kommando>diary on</kommando> and <kommando>diary off</kommando>.
</avsnitt> 
<avsnitt>
If you haven't done that, and still want copy of the calculations, you can in Windows/Macintosh use <uthev>File</uthev>-<uthev>Print/Print Selection</uthev>. For the UNIX version you have to copy the text, paste it into a text editor and then print.
</avsnitt>

<os2>C and Fortran in MATLAB</os2>
<avsnitt> 
It is possible to use both C and Fortran in MATLAB. These functions are called MEX-files. For more information you can see the  MATLAB manual: External Interface Guide. 
</avsnitt>

<os2>Export and import</os2>
<avsnitt>
There are several ways to import data to MATLAB: </avsnitt>
<ul>
<li>Define a variable containing the data</li>
<li>Use the editor and define the data in the same way in an .m-file as a script.</li>
<li>Import data from a 'flat ASCII file'. A 'flat' file stores all data in ASCII format with a certain length for the rows, spaces between numbers and line feed after each line. You import these with the command <kommando>load filename</kommando>.</li>
<li>Using MATLAB's In/Out-functions like <kommando>fopen</kommando> and <kommando>fread</kommando>. Often used when retrieving data from other programs.</li>
<li>Create a MEX-file for reading data - used if there already exists C or Fortran functions for reading data from other programs. </li>
</ul>
<br/>
<avsnitt>
MATLAB's I/O functions are based on C. 
</avsnitt>
<avsnitt>
<uthev>Export</uthev>: There are several ways to export data from MATLAB: 
</avsnitt>
<ul>
<li>As mentioned above, you can use the command <kommando>diary</kommando> and later edit the file the command calls. </li>
<li>It is possible to save data in ASCII formatData kan lagres i ASCII format by using <kommando>save filename.txt name_of_variable -ascii</kommando>. </li>
<li>Cut and paste from MATLAB to a text editor. </li>
<li>Use In/Out functions such as <kommando>fwrite</kommando>. </li>
<li>Create a MEX-file. This and the former method is often used when exporting data to another program.  </li>
<li>Print data as a MAT-file with the command <kommando>load</kommando> and use a C or Fortran program to convert this data to whichever format you want. </li>
</ul>

<os2>Miscellaneous:</os2>

<ul>
<li>MATLAB supports four commands for files and directories on your machine: <kommando>dir</kommando>(show list of files), <kommando>type</kommando>(display the contents of a file), <kommando>delete</kommando>(delete file), <kommando>cd</kommando> (change directory). </li>
<li>You can make a command continue onto the next line when ending the current one with <uthev>...</uthev>. If we end a command with semicolon, the result will not be shown. It is possible to type severeal MATLAB commands on the same line with commas between. </li>
<li>A variable or function name has to start with a letter and can contain up to 19 characters. </li>
</ul>

<os2>For more information, see these pages (also mentioned in the article):</os2>
<ul>
<li>MathWorks:<ekstern tittel="Learning MATLAB" uri="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab.shtml" brutt="nei"/></li>
<li><ekstern tittel="MATLAB newsletters" uri="http://www.mathworks.com/company/newsletters/" brutt="nei"/></li>
<li><ekstern tittel="Matlab in academia" uri="http://www.mathworks.com/academia/" brutt="nei"/> </li>
</ul>



  </hoveddel>
</artikkel>


