140 likes | 273 Views
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).
E N D
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) 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] ) Sammenlign: x = 0: 0.2 : 2*pi; plot(x, sinus(x,17))
Integral » quad('sin', 0, 2*pi) ans = 0 function r = sin2(x) r = sinus(x,2); » quad('sin2', 0, 2*pi) ans = 3.1416 » quad('sinus(x,2)', 0, 2*pi) ??? Can not find function 'sinus(x,2)'.
Polynomier [an an-1 an-2 … a1 a0] * [xn xn-1 xn-2 … x1 ]’ » roots([1 -3 3 -1]) ans = 1.0000 1.0000 + 0.0000i 1.0000 - 0.0000i » roots([1 -2 1]) ans = 1 1 » roots([1 -4 6 -4 1]) ans = 1.0001 1.0000 + 0.0001i 1.0000 - 0.0001i 0.9999
Undersøgelse » 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) )
Løsning af Differentialligninger function 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)
Minimum » fmin('sin', 0, 2*pi) ans = 4.7124 » ans/pi ans = 1.5000
p Positionsbestemmelse p = [x , y] w1 = [a,b] -- lokalt A = [ cos(theta) sin(theta) -sin(theta) cos(theta) ] wl GLOBAL = ( p’ + A*w1’)’ M1 ? M1‘ = p ‘+ X*w1’
p Løsningsforsøg M1‘ = p ‘+ X*w1’ 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
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;
p Løsning » global p M w1 » p = [1 1]; » w1 = [1 0.5]; » M = [1.5 0]; » fmin('fz', 0, 2*pi) ans = 1.5708 » ans/pi ans = 0.5000 » M = [0 0]; » fmin('fz', 0, 2*pi) ans = 2.8198 » ans/pi ans = 0.8976
Prøve function 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)/pi ans = 0.5000 fz(ans*pi) ans = 1.2326e-032
Næste gang … Funktioner Udførelse af program function …, global, …, I/O