How to write functions to MATLAB

How to write functions to MATLAB

MATLAB is the popular software package for the solution of technical, mathematical, statistical tasks, calculations and modeling. Also the programming language of the same name which is used in this package is called. Let's consider an order of writing of functions for the MATLAB environment.

It is required to you

  • The computer with the installed MATLAB package of any version.

Instruction

1. In the environment of MATLAB there are several operating modes. The simplest is an input of teams directly in a window of teams (Command Window). If it is not visible in the interface of the program, means it is necessary to open it. It is possible to find a window of teams through the Desktop menu-> Command Window. Give for an example we will enter into this window consistently one after another teams ""x = [1:100]; y = sqrt(x); plot(y);"", we will also press the Input key (Enter). The program will instantly create variables X, will create variable Y and will count its values on the set function, and then will construct its schedule. We can be switched by shooters of the Up and Down keyboard in a window of teams between all entered teams, immediately change them if necessary, and on pressing of Enter to send to the MATLAB environment for execution again. Conveniently? Certainly. And the main thing - very quickly. All these actions take several seconds. But what if more complex organization of teams is necessary? If cyclic execution of any teams is necessary? To enter teams manually on one, and then long to look for them in the history can be quite tiresome business.

2. To simplify life to the scientist, the engineer or the student, the window of the editor (Editor) serves. Let's open the editor's window through the Desktop menu-> Editor. Here it is possible to create new variables, to build schedules, to write programs (scripts), to create components for exchange with other environments, to create applications with the user interface (GUI) and also to edit already available. But we are interested in writing of the program containing functions for reuse in the future at present. Therefore we go to the File menu and we choose New-> M-File.

3. In the field of the editor we will write the simple program, but slightly we will complicate it: function draw_plot (x) y = log(x); % we Set the first funktsiyusubplot (1, 2, 1), plot (x, y); % we Build the first to the schedule = sqrt(x); % we Set the second funktsiyusubplot (1, 2, 2), plot (x, y); % we Build the second grafikma added the second function and we will output two schedules next to each other at once. The sign of percent designates in the environment of MATLAB comments. Let's not forget to keep the program. Standard extension of the file with the Matlab program – *.m. Now close the editor and a window with the schedule which we constructed earlier.

4. We pass back into a window of teams. It is possible to clean the history of commands that excess information did not distract us. For this purpose click with the right mouse button in the field of input of teams and in the opened context menu select the Clear Command Window item. Variable X remained with us after the previous experiment, we did not change it and did not delete. Therefore it is possible to enter into a window of teams at once: draw_plot(x); You will see that MATLAB will read our function from the file and will execute it, having drawn the schedule.

Author: «MirrorInfo» Dream Team


Print