Universitet for miljø- og biovitenskap: Fag TEL230 Reguleringsteknikk

Matlab-kurs

Praktiske opplysninger


Om Matlab

Matlab brukes til numeriske beregninger og visualisering av data.

Anvendelsesområder:

  • Plotting av data (Matlab)
  • Matematiske funksjoner (Matlab)
  • Likningsløsning (Matlab)
  • Lineær algebra (matriseregning) (Matlab)
  • Reguleringsteknikk (Control System Toolbox)
  • Simulering (Matlab, Control System Toolbox, Simulink)
  • Signalbehandling (Signal Processing Toolbox)
  • Statistikk (Statistical Toolbox)
  • Optimalisering (Optimization Toolbox)
  • + + (20-30 toolboxer)

Arbeidsomgivelsene i Matlab

[Kap. 3]

Vi gjør oss kjent med arbeidsomgivelsene:
  • Kommandovinduet med kommandolinja
  • Arbeidsrommet
  • Arbeidskatalogvinduet
  • Kommandohistorievinduet
  • Oppstartingsvinduet
  • Hjelp!

We try the following simple commands from the command line (the commands are executed by pressing Enter):

a=1+1 The response is shown in the command window.

a=2+2; The response is not shown in the command window.

a Shows the value of the variable a.

1+2 The result is assigned to the inbuilt variable ans.

ans+4 The result is 7.

d=1+2+...[Enter]
3+4
Three dots means that the command line continues.

e=1+1,f=2+2 One or more expressions - separated by semicolon or comma - can be written on one line.

f, F Octave separates between ordinary and capital letters. f exists, while F does not exist.

0.1, 1e-1, 2^3, exp(1), pi Various ways to enter numbers. Obs: Desimalpunktum, ikke -komma!

Matlab functions are vectorized, i.e. functions can be called with vectorial arguments.

t=[0:0.1:100]; Vector

f=2; %Hz Note the percent sign. It means comments.

y=sin(2*pi*f*t) y becomes a vector, too, of same length as t.


Hjelp!

Tre alternativer:

  1. Kommandolinje-hjelp: Eksempel: help sin (på kommandlinja)
  2. Hjelp-menyen
  3. Hjelp på Web: http://mathworks.com

Scripts

A Matlab script is a text file of name *.m containing Matlab expressions. You can edit the script using any text editor, e.g. Notepad, or using the inbuilt Matlab editor which can be launched via File / New.

You run the script in two ways (causing all the expressions to be execued automatically as if they were written on the command line individually):

  1. By typing the script name (without the file extension m) on the command line
  2. By pressing the Run-button in the toolbar in the editor

You should use scripts even for small tasks.

Example:

Enter the following expressions in an empty document in the Matlab editor:

clear;
a=1;
b=2;
c=a+b

Save the script as (e.g.) c:\temp\myscript1.m.

Run the script via the Run button


Matrix (or array) operations

Creating matrices. Getting matrix information:

A=[1,2;3,4] Cretaes matrix (or array) A.

r=[0:0.2:10] Row vector (or array)

c=[0:0.2:10]' Column vector (or array)

r(1) Returns the first element of r. Note that 1 (not 0) is the first index.

r(end) Returns the last element of r.

x=[1,2,3]'; y=[4,5,6]'; B=[x,y] Creates matrix B from given column vectors.

size(x) Size of x.

B(:,2) Returns column no. 2 in B.

A few special matrices:

C=eye(3) Identity matrix

D=ones(3,2) Matrix of ones

E=diag([1,2,3]) Diagonal matrix

Matrix calculations:

Matrices can be used in matrix calculations. A few examples:

F=C+E

A*inv(A)

Elementwise calculations:

x=[1,2,3]'; y=[4,5,6]'; z=x.*y Note the operator .*


Plotting

Vi prøver plott1.m, som demonstrerer plotting.

Vi prøver figureditering via menyvalg og funksjonsknapper i figurvinduet.

Vi prøver plott_edit.m, som illustrerer figureditering via kommandoer.


[Fagets hjemmeside] [Fagets framdriftsplan]


Oppdatert 24.11.05 av Finn Haugen, faglærer (e-postadresse: finn@techteach.no).