Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to MATLAB Session 4 Simopekka Vänskä, THL 2010.

Similar presentations


Presentation on theme: "Introduction to MATLAB Session 4 Simopekka Vänskä, THL 2010."— Presentation transcript:

1 Introduction to MATLAB Session 4 Simopekka Vänskä, THL 2010

2 Introduction to MATLAB - Session 4 Contents of this course Session 5 Graphics 2 More linear algebra Starting homework 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

3 Function functions – function handles

4 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

5 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.

6 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);

7 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)

8 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.

9 Introduction to MATLAB - Session 4 Example: ODE’s 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…

10 Problems Session 4

11 Introduction to MATLAB - Session 4 Problems 1. Implement the minimum search of previous examples (Way 1-4). 2. Write the previous ode –example as an anonymous function handle call. 3. Compute the integral ∫ [0,1] F(x) dx, F(x) = sqrt(x)/exp(x^2), with quad.

12 Introduction to MATLAB - Session 4 Problems – my function function 4. Write a function differf.m that computes the differential of the argument function f:R n  R m 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+he j )-f(x))/h. Test your differf with a)f(x) = x^2+3x,b) f(x) = Ax, A = rand(4,5).

13 Introduction to MATLAB - Session 4 Problems 5. 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  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/s 2  Air resistance (drag) directed opposite to the velocity v: D(v) = c(v)mv 2 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 s x ’(t) = v x (t), s y ’(t) = v y (t) v x ’(t) = F x (v,t), v y ’(t) = F y (v,t).  What is optimal  for getting the longest fly? Use fminsearch.

14 >> quit …to exit MATLAB.


Download ppt "Introduction to MATLAB Session 4 Simopekka Vänskä, THL 2010."

Similar presentations


Ads by Google