• Ei tuloksia

Matlab 1: Get Started

N/A
N/A
Info
Lataa
Protected

Academic year: 2022

Jaa "Matlab 1: Get Started"

Copied!
28
0
0

Kokoteksti

(1)

Matlab 1: Get Started

(2)

Starting/Existing Matlab 2/2

Run matlab in command line:

In Terminal/console

i.Change to you work directory ii.Type: matlab -nodesktop

iii.To close it, type exit

(3)

Keep track of your work

Comment your code: %

Save your command history: diary

Display current variables: who

Clear variables: clear

>> diary MatlabHistory.txt; %Start to record you command history

>> a=1;

>> who;

Your variables are:

a

>> clear;

>> who;

>> diary MatlabHistory.txt; %Start to record you command history

>> a=1;

>> who;

Your variables are:

a

>> clear;

>> who;

(4)

Get Help

Type Help, your will get a bounch of help topics. Select the topic your are looking for.

Or you can type helpdesk to start the help window.

Use help toolbar.

If you know your function. Type help -Function_Name

(5)

More learning material

Get a book from library by searching keyword:

matlab

Some toturials from internet:

http://www.math.ufl.edu/help/matlab-tutorial/

http://www.cyclismo.org/tutorial/matlab/

Quick Reference:

http://www.math.umaine.edu/~hiebeler/comp/matlabR.html

http://www.cs.cmu.edu/~tom/10601_fall2012/recitations/matlab_quickref.pdf

Examples:

http://www.mathworks.se/help/matlab/examples/

(6)

Mathematical Functions

>> a=1; b=10; c=-2;

>> d = a*b + c d =8

>> d = b/c d = -5

More math funtions and predefined constant in

http://www.mathworks.se/help/matlab/functionlist.html

- Search the page by:

Mathematics to find the math functions

Constants and Test Matrices for predefined constant

(7)

Matlab 2: Plotting

http://www.mathworks.se/help/matlab/ref/plot.html http://www.mathworks.com/discovery/gallery.html

(8)

9

Simple Plot

Sample Code:

>> x = 0:pi/100:2*pi;

>> % x is a sequence from 0 to 2*pi, step by pi/100

>> y = sin(x);

>> sineplot = figure; %open new plot window

>> plot(x,y); % plot sin(x)

>> xlabel('x:0 : 2\pi'); ylabel('sin(x)');

>> title('Sine function'); %add labels and title

>> saveas(sineplot,'sineplot','jpg');

>> % save plot to sineplot.jpg

(9)

0 1 2 3 4 5 6 7 -1

-0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1

x:0 : 2

sin(x)

Sine function

(10)

Multiple data sets in one plot

>> x = 0:pi/100:2*pi; y1 = sin(x); y2=2*sin(x);

>> plot(x,y1,'r-'); % plot sin(x)

>> hold

>> plot(x,y2,'g+'); % plot 2*sin(x) to the same figure

>> hold off

>> % or we can use

>> %plot(x,y1,'r-',x,y2,'g+');

>> xlabel('x:0 : 2\pi'); ylabel('sin functions');

>> title('Sin functions');

>> legend('sin(x)','2*sin(x)');% add legend

(11)
(12)

Subplots

>> x = 0:pi/100:2*pi; y1 = sin(x); y2=2*sin(x);

>> subplot(1,2,1);

>> % plot with one row, two columns

>> plot(x,y1,'r-');

>> title('sin(x)')% plot sin(x)

>> subplot(1,2,2);

>> plot(x,y2,'g+');

>> title('2*sin(x)');% plot 2*sin(x)

(13)
(14)

Matlab 3: Matrices

http://www.mathworks.se/help/matlab/learn_matlab/matrices-and-arrays.html http://www.math.utah.edu/~eyre/computing/matlab-intro/matrices.html

(15)

Construct Vectors and Matrices 1/3

Format

:

Vectors and matrices are enbraced by squares braces ([]). For example: vector1 = [1 2 3 4];

Row elment separated by spaces or commas (,)

Column vector separated by semicolon (;)

Special Matrix:

Check the functions to generate special matrices, for example, eye(n) to generate identity matrix.

(16)

Construct Vectors and Matrices 2/3

Preallocate your matrix make programs run faster.

For example, you can allocate a matrix x before you use it by :

x = zeros(1, 10^6);

And then you can fill the matrix with values later.

Check for more technics to improve your program:

http://www.mathworks.se/help/matlab/matlab_prog/techniques-for-improving-performance.html

(17)

Construct Vectors and Matrices 3/3

Sample Codes:

>> vector1 = [1 2 3 4]

vector1 =

1 2 3 4

>> >> matrix1 = [1 2 3; 4 5 6; 7 8 0]

matrix1 =

1 2 3 4 5 6 7 8 0

(18)

Matrix Operation 1/3

Transpose: by adding singl quote (')

>> matrix1 matrix1 =

1 2 3 4 5 6 7 8 0

>> matrix1' ans =

1 4 7 2 5 8 3 6 0

(19)

21

Matrix Operation 2/3

>> a = [1 2; 3 4];

>> b = [2 2; 2 2];

>>c= a*b c =

6 6 14 14

>> a.*b ans =

2 4

The dimensiones of two matrices must be agree to do matrix

operation

Matrix operation is defined by * / ^

If you want to do

element-by-element operation, add . in front of the matrix operation.

(20)

Matrix Operation 3/3

Inverse of matrix: inv(matrix)

>> inv(a)*c ans =

2 2 2 2

>> a\c ans =

2.0000 2.0000 2.0000 2.0000

(21)

Matlab 4: Control flow

http://www.mathworks.se/help/matlab/matlab_prog/loop-control-statements.html http://www.mathworks.se/help/matlab/control-flow.html

(22)

If structure 1/2

if expression statements elseif

expression statements else

statements end

Note:

No semicolon(;) is

needed at the end of line

Indentation is not

needed but good for reading

Non't forget end at last

(23)

If structure 2/2

a = rand(1);

%generate random number from uniform(0,1) if a>0.5

fprintf('a = %f is larger than 0.5',a);

elseif a==0.5

fprintf('a = %f equales 0.5',a);

else

fprintf('a = %f is less than 0.5', a);

end

(24)

for/while Loop

for index = values statements

end

while expression is true statements

end

Note:

Try to avoid loop if possible.

Matrix operation is much faster.

(25)

>> t = cputime;%start to record cpu time a = ones(10^3); b = ones(10^3,10^3);

for i = [1:1:10^3]%for loop to do a*b abi = 0;

for j = [1:1:10^3]

abi = abi + a(j)*b(i,j);

end end

Cputime-t %print cpu time to do a*b by loop ans =

3.3540

>> t = cputime; a*b; cputime-t

%print cpu time to do a*b by matrix operation ans =

(26)

Matlab 4: Functions

http://www.mathworks.se/products/

http://www.tech.plym.ac.uk/spmc/links/matlab/matlab_toolbox.html http://stommel.tamu.edu/~baum/toolboxes.html

http://www.mathworks.com/matlabcentral/newsreader/view_thread/106127

(27)

29

Define Function

Format:

function return_value = function_name( parameters ) Statement

End

Example:

Create m file as average.m (The same name as your function), put in your matlab path. Type the following content in average.m

function y = average(x) y = sum(x)/length(x);

end

Then, call the fucntion by:

>> z = 1:49;

>> average(z) ans =

(28)

Matlab Built-in Functions

Check the built-in functions. They are always more efficient.

>> help elfun % display the elementary functions

>> help specfun % display special functions

There are many free toolboxs for specific fields.

Check the following links (Please don't use them in this course if you are required to write functions by yourself.)

http://www.mathworks.se/products/

http://www.tech.plym.ac.uk/spmc/links/matlab/matlab_toolbox.html http://stommel.tamu.edu/~baum/toolboxes.html

http://www.mathworks.com/matlabcentral/newsreader/view_thread/106127

Viittaukset

LIITTYVÄT TIEDOSTOT

Graphs ->Legacy Dialogs -> Boxplot -> Simple -> Variable : Lapsen paino grammoina, Catecory axis : Lapsen sukupuoli.. Graphs ->Legacy Dialogs -> Histogram

Graphs ->Legacy Dialogs -> Boxplot -> Simple -> Variable : Lapsen paino grammoina, Catecory axis : Lapsen sukupuoli.. Graphs ->Legacy Dialogs -> Histogram

Jos tehdään näin, niin suoritetaan testaus 5 %:n merkitsevyys- eli riskitasolla, ja hyväksytään H 0... Tätä

LIEB<>HLHFB@>>GB>G FNM::MBHBM: C: DKHFHLHFB FHGHLHFB:G E@HKBMFB>GM:KDDNNLDHKK>EHBL>DWFNM::MBHG:EE>>EBDNHKF:G >MMW

:EN>BEM:&:GLBM>DLMB>GD:NMM:L::F>GDB>EBC:IHACHBLF:BL>GDB>E>M>G@E:GMBC:FNNMDB>E>M

[r]

– <Ctrl+H> – Activate/Deactivate Context Help Window – <Ctrl+B> – Remove Broken Wires From Block Diagram – <Ctrl+E> – Toggle Between Front Panel and

>> ei tarvita suojausta synnytyksen jälkeen - Anti-D T 1/2 3 viikkoa. >> näkyy veressä