matlab programmering

14
MATLAB Programmering Anders P. Ravn Institut for Datalogi Aalborg Universitet Forår 2005 d = size(s); for k = 1:d(1), for n = 1:d(2), if (s(k,n) < 0) r(k,n) = -1; else r(k,n) = 1; end; end; end

Upload: charlene-durham

Post on 31-Dec-2015

23 views

Category:

Documents


0 download

DESCRIPTION

d = size(s); for k = 1:d(1), for n = 1:d(2), if (s(k,n) < 0) r(k,n) = -1; else r(k,n) = 1; end; end; end. MATLAB Programmering. Anders P. Ravn Institut for Datalogi Aalborg Universitet Forår 2005. Funktionaler (Function-functions). - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: MATLAB  Programmering

MATLAB Programmering

Anders P. Ravn

Institut for Datalogi

Aalborg Universitet

Forår 2005

d = size(s); for k = 1:d(1), for n = 1:d(2), if (s(k,n) < 0) r(k,n) = -1; else r(k,n) = 1; end; end; end

Page 2: MATLAB  Programmering

Funktionaler (Function-functions)

FPLOT(FUN,LIMS) plots the function specified by the string FUN between the x-axis limits specified by LIMS = [XMIN XMAX]. FUN must be the name of an M-file function with variable x such as 'sin(x)', 'diric(x,10)' .

plot('sinus(x,17)', [0, 2*pi] )

0 1 2 3 4 5 6-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Sammenlign:

x = 0: 0.2 : 2*pi;plot(x, sinus(x,17))

Page 3: MATLAB  Programmering

Integral

» quad('sin', 0, 2*pi)

ans = 0

» quad('sinus(x,2)', 0, 2*pi)??? Can not find function 'sinus(x,2)'.

function r = sin2(x)r = sinus(x,2);

» quad('sin2', 0, 2*pi)

ans = 3.1416

Page 4: MATLAB  Programmering

Polynomier[an an-1 an-2 … a1 a0] * [xn xn-1 xn-2 … x 1 ]’

» roots([1 -2 1])ans = 1 1

» roots([1 -3 3 -1])ans = 1.0000 1.0000 + 0.0000i 1.0000 - 0.0000i

» roots([1 -4 6 -4 1])ans = 1.0001 1.0000 + 0.0001i 1.0000 - 0.0001i 0.9999

Page 5: MATLAB  Programmering

» p2 = [1 -2 1];» p3 = [1 -3 3 -1];» p4 = [1 -4 6 -4 1];

» x = 0.6:0.05:1.4;

» plot(x,polyval(p2,x), x, polyval(p3,x), x,polyval(p4,x), x,polyval([0],x) )

Undersøgelse

0.6 0.7 0.8 0.9 1 1.1 1.2 1.3 1.4-0.1

-0.05

0

0.05

0.1

0.15

0.2

0.25

Page 6: MATLAB  Programmering

Løsning af Differentialligningerfunction xdiff = difflign(t,x)% Differentialligningerne% x(1)'(t) = x(2)% x(2)'(t) = -x(1)%xdiff = [ - x(2) ; x(1) ];

» [t,x] = ode23('difflign', [0,2*pi], [0,1]');» plot(t,x)

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

Page 7: MATLAB  Programmering

Hvor er Matricen ?

Styremoment

Drivmoment

Resulterende moment

Resulterende kraft

Page 8: MATLAB  Programmering

Minimum

» fmin('sin', 0, 2*pi)

ans = 4.7124

» ans/pi

ans = 1.5000

Page 9: MATLAB  Programmering

Positionsbestemmelse

p = [x , y]

w1 = [a,b] -- lokalt

A = [ cos(theta) sin(theta) -sin(theta) cos(theta) ]

wl GLOBAL = ( p’ + A*w1’)’

p

M1

?

M1‘ = p ‘+ X*w1’

Page 10: MATLAB  Programmering

Løsningsforsøg

p

X = (M1‘ - p ‘)/w1’

» p = [1 1];

» w1 = [1 0.5];

» M = [1.5 0];

» (M' - p')/w1'ans = 0.5000 0 -1.0000 0

M1‘ = p ‘+ X*w1’

Page 11: MATLAB  Programmering

Bedre forsøg

M1‘ = p ‘+ X*w1’

X= [ cos(theta) sin(theta) -sin(theta) cos(theta) ]

f(theta) = M1‘ - p‘ - X *w1’

Dvs . f(theta) = [ 0 0]’

Husk så at for vektor a gælder: a*a’ = |a|2

Dvs. Vi kan finde minimum for

z(theta) = f(theta) * f(theta)’

function r = fz(theta)global M p w1;

X = [cos(theta), sin(theta); -sin(theta), cos(theta)];

f = M' - p' - X*w1';

r = f'*f;

Page 12: MATLAB  Programmering

Løsning

p

» global p M w1» p = [1 1];» w1 = [1 0.5];» M = [1.5 0];

» fmin('fz', 0, 2*pi)ans = 1.5708» ans/pians = 0.5000

» M = [0 0];» fmin('fz', 0, 2*pi)ans = 2.8198» ans/pians = 0.8976

Page 13: MATLAB  Programmering

Prøvefunction r = fz(x)global M p w1; X = [cos(x), sin(x); -sin(x), cos(x)]; r1 = M' - p' - X*w1'; r = r1'*r1;

» global M w1 p» M = [1.5 0]» w1 = [1 0.5]» p = [1 1]

» fmin('fz',0,pi)/pians = 0.5000

fz(ans*pi)ans = 1.2326e-032

Page 14: MATLAB  Programmering

Næste gang …

FunktionerUdførelse af program

function …, global, …, I/O