This tutorial explains using examples how to perform with Matlab many types of calculations including solving ordinary differential equations. A detailed description of Matlab (functions, commands, libraries, ...) can be found in the help menu of Matlab. We can also get help from the Matlab command window by typing the command help subject after the Matlab prompt and pressing the Enter key. The word subject can be any function, command or library name. The help command displays the names of functions in capital letters (e.g. QUADL, ODE113, ...). The name of a function must be typed in small letter in the Matlab command window (e.g. quadl, ode113, ...).
Almost all the information provided in this document is also valid for Octave. The presentation of the results of a computation with Octave may sometime be different than the presentation of the same results with Matlab. A very small number of Matlab functions are missing in Octave. We mention one in the section on solving ordinary differential equations below. In such situation, there is usually a function in Octave that can perform the same task.
In each of the examples of this tutorial on Matlab, we give the code to be executed and display the results of the code after it has been run in Matlab.
In this tutorial, the text in blue is the code as it should be typed in after the Matlab prompt.
The following topics are covered in the documents of the present section of the website about Matlab:
To launch Matlab from a computer running Linux/Unix, it suffices to type matlab at the prompt in a terminal window and press the Enter key. From a computer running Windows, it suffices to click on the Matlab icon or to select the item Matlab in the menu. In all cases, we get a new window with the Matlab prompt.
>>
The cursor is sitting just after the Matlab prompt waiting for the user to type in instructions.
By default, Matlab save all the commands executed in the command history window.
However, it may be useful to save part of or all the commands and outputs of a session inside a file for the purpose of using them later to generate a Matlab session.
To save a Matlab session (or part of it), it suffices to execute the command diary. After that, Matlab echos all commands and outputs into a default file usually called diary. To save the commands and outputs in the file named assignment1 for instance, it suffices to execute the command diary assignment1. After that, all the commands and outputs will be saved to the file assignment1 in the working directory. To stop saving the commands and outputs, it suffices to execute the command diary off. To start again saving the commands and outputs into the file assignment1, it suffices to execute diary on. The new commands and results are appended to the existing content of the file assignment1.
Matlab provides several files management commands that allow users to list, view and delete m-files, to view the content of a directory/folder, to change the current directory/folder, etc. A summary of some commands is given in the following table.
| Command | Description |
|---|---|
| what | Display a list of all m-files in the current directory. |
| dir | List all files in the current directory. |
| type file_name | Display the content of the m-file file_name.m. |
| delete file_name | Delete the m-file file_name.m. |
| cd path | Change the current directory to the directory/folder given by path. |
| cd | Show present working directory/folder. |
| which funct_name | Display the directory path to the m-file funct_name.m. |
It is also very simple to execute a Matlab script in batch mode on a computer running Linux/Unix. For instance, to execute the script my_code.m in batch mode, it suffices to enter the following command at the prompt of a terminal.
% nice +19 matlab -nosplash < my_code.m > my_code.log
The nice command is a Linux/Unix command to change the priority of execution of the program to be executed. The argument +19 lowers the priority of execution of the program so that it does not monopolize the CPU of the computer. The output of the program is sent to the file my_code.log. The option -nosplash cancels the display of the welcoming message when Matlab is launched.