140 likes | 394 Views
Introduction to MATLAB Session 4. Simopekka Vänskä, THL 2010. Session 5 Graphics 2 More linear algebra Starting homework. Contents of this course. Session 4 Function functions. Session 1 General Matrices M-files Session 2 Some matrix commands Logical expressions Graphics 1
E N D
Introductionto MATLABSession 4 Simopekka Vänskä, THL 2010
Session 5 Graphics 2 More linear algebra Starting homework Contents of this course • Session 4 • Function functions Session 1 • General • Matrices • M-files Session 2 • Some matrix commands • Logical expressions • Graphics 1 Session 3 • My functions + strings, cells • Controlling program flow Introduction to MATLAB - Session 4
Function functions Function functions mean operations that are done to (mathematical) functions: • Root (zero) finding of a function. • Optimization: Find a minimum/maximum of a function • Compute the integral of a function • Definite integral • Integral function • Integral operators • Compute the derivative of a function • Compute the solution of a differential equation • Here, the differential equation is the function to be called Introduction to MATLAB - Session 4
Calling function functions The argument function has to be passed to the computing function. This can be done in several ways. Example. Find a minimum for f(x) = x^2+p*x with fminsearch. Here,p is a parameter, say p=5. Way 1 Calling with the function name (old Matlab versions). • Write a m-file function y = testx2A(x) p = 5; y = x.^2 + p*x; • >> x0 = fminsearch(’testx2A’,randn(1)) • Here randn(1) is the starting point for the minimum search. Introduction to MATLAB - Session 4
…calling function functions Function Handles Way 2 Calling with the function handle @ to the m-file: • Write testx2A.m as above. • >> x0 = fminsearch(@testx2A,randn(1)) • Remark: Instead of m-file, testx2A could be a subfunction. • A function handle datatype is a pointer a function. • Compare, cell is a pointer to a matrix. • Creating a function handle: >> fff = @testx2A; • This creates a variable fff which is a pointer to function testx2A. • The variable fff can be passed to functions as other variables. • A command line fx = fff(x); executes now fx = testx2A(x); Introduction to MATLAB - Session 4
…calling function functions Function Handle to anonymous function Way 3 Calling with @ to the anonymous function: p = 5; ff = @(x) x.^2 + p*x; x0 = fminsearch(ff,randn(1)) or directly, x0 = fminsearch(@(x) x.^2 + p*x, randn(1)); • No need to write any m-files. • Easy to change parameter value p. • The syntax for an anonymous function: fhandle = @(argument list) a single expression which is called fhandle(argument list) Introduction to MATLAB - Session 4
…calling function functions Changing p (in complicated cases) Way 1-2: To change p you need to edit testx2A m-file! Way 3: Parameter p can be changed, but only simple expressions possible. Way 4 The anonymous function to my function: function y = testx2B(x,p) y = x.^2 + p*x; end ff = @(x) testx2B(x,p); % now ff is a single variable function x0 = fminsearch(ff,randn(1)) % or fminsearch(@(x) testx2B(x,p), randn(1)) • Remark: x0 = fminsearch(@testx2B, randn(1)); is an error. Introduction to MATLAB - Session 4
Solve the initial value problem x’’(t) = x’(t) + x(t) x’(0) = 1, x(0) = 0, on interval [0,1]. ODE solvers ode45, ode23, or similar for the 1st order problems Write the problem as the 1st order system to m-file: function dy = dftest(t,y) % y(1) = x(t), y(2) = x’(t) % dy(1) = x’(t), dy(2) = x’’(t) dy = [y(2); y(2) + y(1)]; Call the solver >> y0 = [0; 1]; >> [t,y] = ode23(@dftest,[0,1],y0); >> x = y(:,1); Here, the sizes dy and y not very consistent… Example: ODE’s Introduction to MATLAB - Session 4
Problems • Implement the minimum search of previous examples (Way 1-4). • Write the previous ode –example as an anonymous function handle call. • Compute the integral ∫[0,1] F(x) dx, F(x) = sqrt(x)/exp(x^2), with quad. Introduction to MATLAB - Session 4
Problems – my function function • Write a function differf.m that computes the differential of the argument function f:RnRm at a given point: function df = differf(fff,x,h) Here, fff is the function argument x is the point in Rn at which the differential is computed h is the small number in the difference quotience Recall, a differential at x is a linear matrix Df(x) with f(x+h) = f(x) + Df(x)h + error(x,h), and the j’th column of Df(x) can be computed as Df(x)(:,j) = lim (f(x+hej)-f(x))/h. Test your differf with a) f(x) = x^2+3x, b) f(x) = Ax, A = rand(4,5). Introduction to MATLAB - Session 4
Problems • Compute the fly path s(t) of a batted baseball from F = ma(t), a(t) = v’(t) = s’’(t), until the ball hits the ground. Use ode23. • Let v0 be the starting velocity, e.g. v0 = 50 m/s. • Let s0=(0,0) be the initial position. Let a be the angle between the ground and the initial direction of the ball. • Forces: • Gravitational force downwards mG, m = .14 kg, G = 9.81 m/s2 • Air resistance (drag) directed opposite to the velocity v: D(v) = c(v)mv2 where[1] c(v) ≈ 0.004 + 0.006/(1+exp((v-35)/5)), [v] = m/s. [1] Computational Physics, Fitzpatrick, webpages Hint: Write the 1st order system sx’(t) = vx(t), sy’(t) = vy(t) vx’(t) = Fx(v,t), vy’(t) = Fy(v,t). • What is optimal a for getting the longest fly? Use fminsearch. Introduction to MATLAB - Session 4
>>quit …to exit MATLAB.