matlab programmering
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 PresentationTRANSCRIPT
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
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))
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
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
» 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
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
Hvor er Matricen ?
Styremoment
Drivmoment
Resulterende moment
Resulterende kraft
Minimum
» fmin('sin', 0, 2*pi)
ans = 4.7124
» ans/pi
ans = 1.5000
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’
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’
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;
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
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
Næste gang …
FunktionerUdførelse af program
function …, global, …, I/O