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

Slides:



Advertisements
Similar presentations
Ch 7.7: Fundamental Matrices
Advertisements

Dynamics of a four-bar linkage
One Dimensional Kinematics
Turn in your homework in the front. Begin: Journal 9/03 1. Write the equation for distance using time and velocity. 2. Write the equation for velocity.
1 Basic Differentiation Rules and Rates of Change Section 2.2.
Physics 215 – Fall 2014Lecture Welcome back to Physics 215 Today’s agenda: Using graphs to solve problems Constant acceleration Free fall.
Kinematics of Particles
CIS 101: Computer Programming and Problem Solving Lecture 7 Usman Roshan Department of Computer Science NJIT.
Ch 7.9: Nonhomogeneous Linear Systems
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 2 Mathematical Modeling and Engineering Problem Solving.
Copyright © Cengage Learning. All rights reserved. 13 The Integral.
Numerical Solutions of Ordinary Differential Equations
Free Fall Lecture 3.
Finding a free maximum Derivatives and extreme points Using second derivatives to identify a maximum/minimum.
Section 1.1 Differential Equations & Mathematical Models
The Mathematics of Star Trek Lecture 3: Equations of Motion and Escape Velocity.
Chapter 1 Computing Tools Analytic and Algorithmic Solutions Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.
An Introduction to Programming and Algorithms. Course Objectives A basic understanding of engineering problem solving process. A basic understanding of.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
Copyright © The McGraw-Hill Companies, Inc. Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 1 An Overview of MATLAB.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
System Control Theory Lab
Using the Derivative AP Physics C Mrs. Coyle
Introduction To this point MATLAB has been used to answer questions with a numeric value ▫Variables are assigned specific values ▫Answers are numbers MATLAB.
CSE 3802 / ECE 3431 Numerical Methods in Scientific Computation
4.1 The Indefinite Integral. Antiderivative An antiderivative of a function f is a function F such that Ex.An antiderivative of since is.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
1 Solution of Nonlinear Equation Dr. Asaf Varol
Chapter 3 Roots of Equations. Objectives Understanding what roots problems are and where they occur in engineering and science Knowing how to determine.
Introduction to MATLAB Session 1 Simopekka Vänskä, THL 2010.
Introduction to MATLAB Session 5 Simopekka Vänskä, THL 2010.
Sec. 4.1 Antiderivatives and Indefinite Integration By Dr. Julia Arnold.
Beginning Programming for Engineers
Motivation Thus far we have dealt primarily with the input/output characteristics of linear systems. State variable, or state space, representations describe.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
6.094 Introduction to programming in MATLAB Danilo Šćepanović IAP 2008 Lecture 3 : Solving Equations and Curve Fitting.
Drag Forces AP Physics C.
6.1 The Indefinite Integral
Numerical Analysis 3D Plots. A numerical method is a technique for computing a numerical approximation of the solution to a mathematical problem.
Write the derivative for each of the following.. Calculus Indefinite Integrals Tuesday, December 15, 2015 (with a hint of the definite integral)
Recap Cubic Spline Interpolation Multidimensional Interpolation Curve Fitting Linear Regression Polynomial Regression The Polyval Function The Interactive.
Introduction to MATLAB Session 2 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2010.
Arab Open University Faculty of Computer Studies M132: Linear Algebra
Applications of Differentiation Section 4.9 Antiderivatives
Lecture 3 Ordinary Differential equations Purpose of lecture: Solve 1 st order ODE by substitution and separation Solve 2 nd order homogeneous ODE Derive.
ANTIDERIVATIVES AND INDEFINITE INTEGRATION Section 4.1.
DIFFERENTIAL EQUATIONS Note: Differential equations are equations containing a derivative. They can be solved by integration to obtain a general solution.
 y’ = 3x and y’ = x are examples of differential equations  Differential Form dy = f(x) dx.
Root Finding UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Ch 1.1: Basic Mathematical Models; Direction Fields Differential equations are equations containing derivatives. The following are examples of physical.
Array Accessing and Strings ENGR 1187 MATLAB 3. Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
ACSL, POSTECH1 MATLAB 입문 CHAPTER 8 Numerical Calculus and Differential Equations.
CIVE1620- Engineering Mathematics 1.1 Lecturer: Dr Duncan Borman Integration by Partial fractions Further applications of integration - acceleration, velocity,
College Algebra Chapter 2 Functions and Graphs Section 2.4 Linear Equations in Two Variables and Linear Functions.
CHAPTER 9.10~9.17 Vector Calculus.
Boyce/DiPrima 10th ed, Ch 7.9: Nonhomogeneous Linear Systems Elementary Differential Equations and Boundary Value Problems, 10th edition, by William E.
Sec. 4.1 Antiderivatives and Indefinite Integration
State Space Representation
Warm Up Find the x-intercept of each function. 1. f(x) = –3x + 9
5-1 Solving Quadratic Equations by Graphing and Factoring SWBAT
College Algebra Chapter 2 Functions and Graphs
Autonomous Cyber-Physical Systems: Dynamical Systems
MATH My research interests lie primarily in the area of numerical analysis and scientific computing, …
Objectives Solve quadratic equations by graphing or factoring.
State Space Analysis UNIT-V.
MATH My research interests lie primarily in the area of numerical analysis and scientific computing, …
ANTIDERIVATIVES AND INDEFINITE INTEGRATION
Section 9.4 – Solving Differential Equations Symbolically
Presentation transcript:

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

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

Function functions – function handles

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 to the m-file:  Write testx2A.m as above.  >> x0 =  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  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 to the anonymous function: p = 5; ff x.^2 + p*x; x0 = fminsearch(ff,randn(1)) or directly, x0 = 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 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 testx2B(x,p); % now ff is a single variable function x0 = fminsearch(ff,randn(1)) % or testx2B(x,p), randn(1)) Remark: x0 = randn(1)); is an error.

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] = >> x = y(:,1);  Here, the sizes dy and y not very consistent…

Problems Session 4

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.

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

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) ≈ /(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.

>> quit …to exit MATLAB.