Matlab Tutorial 1: Hello world, plotting, mathematical functions and file types

In this first Matlab tutorial, I will try to show you the basics of Matlab user interface, data types, simple functions and mathematical operations. All with basic examples so that anyone with any level of programming knowledge can start using Matlab as a mathematical laboratory.

Matlab User Interface

Start Matlab by a double click on the Matlab icon or else by searching for it under program. Now, there should be a large window containing several smaller. These could for instance be:

  • Command Window
  • Command History
  • and Workspace.

This will of course depend on what version you are currently working in. This is the desktop of Matlab.

Command Window: Here you can write your own command lines and access your own files (m-files), but normally you can also see the output from the calculations in this window.
Command History: All command lines are saved here and can be seen in the window, but the same can be achieved by using the arrow button (up). The past command lines can the be seen in the command window.
Workspace: The variables that have been used or created during the execution will be shown in this window. Here you can see value, bytes and class. When you double click on the variables, the elements of the variables become visible.

These three windows should be the default when you start Matlab.

This can of course be altered by Desktop->Desktop Layout or you can construct your own layout and save it.

Current Directory: The directory where you save your m-files or where you store them. Matlab prefers that you store your m-files in the directory work, but you can of course put them anywhere you want. If you choose to put them in any other directory, you should give the path to the directory by File-> Set Path or select current directory.

Matlab Help Browser

During the tutorials you will need to use the Help Browser in Matlab in order to find out how a certain command works. In the Help Browser you can also find related command lines, examples, description, or help with the syntax.

Matlab Help Menu Screeshot

Matlab Help Menu Screeshot

Try to use help browser by selecting: Help-> Help Browser

This window is divided into two sub-windows. The left window is named Help Navigator. Under Search write sin and press enter. To see the result look above.

Plotting Examples with Matlab

Exercise 1: Plot of sine function

By using the help browser you can find out how the sine function works. Make a plot of a sine function. The interval should be -10 to +10 radians. Each command line that is written in the command window is followed by enter. Note how this is done in the help browser. Now x becomes a variable in Workspace. Double click on x to see which elements are within the variable. If you follow the example x will have 2001 elements. In this case x is a one-dimensional vector. The first command line gives x and the second command line plots the sine function versus the x values in a figure window. The sine function is only evaluated for those x-values that are presented.

Exercise 2: Plot of logarithmic function

Now repeat the above example for a different function using the logarithmic (base 10) function instead. Plot the function for an interval x=0.1:0.01:10. It means that x goes from 0.1 to 10 with a resolution of 0.01 (0.1, 0.11, 0.12, 0.13,…10). Use the help browser and the left subwindow and index to search for a command that gives a logarithm. Maybe you can guess the first letters in this command. The plot should look like figure below. Suitable check values are log(10) and log(1) to see if the plot is reasonable.

Plot of a logarithmic function

Plot of a logarithmic function

Please note that x has changed its elements and size.

Pages: 1 2 3 4 5

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment