Lecture 4. Programming

Similar documents
OKLAHOMA STATE UNIVERSITY

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

Chapter 9b: Numerical Methods for Calculus and Differential Equations. Initial-Value Problems Euler Method Time-Step Independence MATLAB ODE Solvers

Lab 13: Ordinary Differential Equations

AMS 27L LAB #8 Winter 2009

Matlab Section. November 8, 2005

1 Overview of Simulink. 2 State-space equations

Lecture 8: Calculus and Differential Equations

Lecture 8: Calculus and Differential Equations

Introduction to MatLab

Laboratory 1. Solving differential equations with nonzero initial conditions

Lesson 11: Mass-Spring, Resonance and ode45

Solving ODEs and PDEs in MATLAB. Sören Boettcher

MATLAB BASICS. Instructor: Prof. Shahrouk Ahmadi. TA: Kartik Bulusu

Ordinary Differential Equations (ode)

L3: Review of linear algebra and MATLAB

EEE161 Applied Electromagnetics Laboratory 1

January 18, 2008 Steve Gu. Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,

Ordinary Differential Equations (ODE)

Vector Fields and Solutions to Ordinary Differential Equations using MATLAB/Octave

13 Numerical Solution of ODE s

MAT 275 Laboratory 7 Laplace Transform and the Symbolic Math Toolbox

We begin exploring Euler s method by looking at direction fields. Consider the direction field below.

Lecture 5b: Starting Matlab

Lecture 2. Introduction to Differential Equations. Roman Kitsela. October 1, Roman Kitsela Lecture 2 October 1, / 25

Solving systems of ODEs with Matlab

Mon Jan Improved acceleration models: linear and quadratic drag forces. Announcements: Warm-up Exercise:

(Linear equations) Applied Linear Algebra in Geoscience Using MATLAB

Read through A Graphing Checklist for Physics Graduate Students before submitting any plots in this module. See the course website for the link.

An Introduction to MatLab

Numerical Methods with Matlab: Implementations and Applications. Gerald W. Recktenwald

A First Course on Kinetics and Reaction Engineering Example S5.1

Review for Exam 2 Ben Wang and Mark Styczynski

Integration by Parts Logarithms and More Riemann Sums!

A Glimpse at Scipy FOSSEE. June Abstract This document shows a glimpse of the features of Scipy that will be explored during this course.

The roots are found with the following two statements. We have denoted the polynomial as p1, and the roots as roots_ p1.

CONTROL SYSTEMS LABORATORY ECE311 LAB 1: The Magnetic Ball Suspension System: Modelling and Simulation Using Matlab

'XNH8QLYHUVLW\ (GPXQG73UDWW-U6FKRRORI(QJLQHHULQJ. EGR 103L Fall Test 2 Solutions. Michael R. Gustafson II

Statistical methods. Mean value and standard deviations Standard statistical distributions Linear systems Matrix algebra

AMS 27L LAB #6 Winter 2009

ENED 1091 HW#3 Due Week of February 22 nd

. var save filename var. y fprintf(fl,'%f %f\n',y) y ..TXT. . fprintf() .TXT. myf = fopen('nam.txt','w'); nam.txt myf.

Solution. Your sketch should show both x and y axes marked from 5 to 5 and circles of radius 1, 3, and 5 centered at the origin.

Physics 241 Class #10 Outline (9/25/2013) Plotting Handle graphics Numerical derivatives and functions Next time numerical integrals

Solutions of the Sample Problems for the First In-Class Exam Math 246, Fall 2017, Professor David Levermore

Introduction to MATLAB

'XNH8QLYHUVLW\ (GPXQG73UDWW-U6FKRRORI(QJLQHHULQJ. EGR 103L Fall Test 2. Michael R. Gustafson II

USE OF MATLAB TO UNDERSTAND BASIC MATHEMATICS

New Mexico Tech Hyd 510

HW5 computer problem: modelling a dye molecule Objectives By the end of this lab you should: Know how to manipulate complex numbers and matrices. Prog

Scientific Computing with Case Studies SIAM Press, Lecture Notes for Unit V Solution of

Solving systems of first order equations with ode Systems of first order differential equations.

SYMBOLIC AND NUMERICAL COMPUTING FOR CHEMICAL KINETIC REACTION SCHEMES

Solution: (a) Before opening the parachute, the differential equation is given by: dv dt. = v. v(0) = 0

First In-Class Exam Solutions Math 246, Professor David Levermore Thursday, 20 September 2018

Experiment 1: Linear Regression

Initial value problems for ordinary differential equations

Math 515 Fall, 2008 Homework 2, due Friday, September 26.

MAE143A Signals & Systems - Homework 5, Winter 2013 due by the end of class Tuesday February 12, 2013.

ENGR Spring Exam 2

Table 1 Principle Matlab operators and functions Name Description Page reference

Chapter 1: Introduction

Vector Fields and Solutions to Ordinary Differential Equations using Octave

TUTORIAL: STATE VARIABLES and MATLAB

A First Course on Kinetics and Reaction Engineering Example 30.1

Quantitative Understanding in Biology Module IV: ODEs Lecture I: Introduction to ODEs

MAT 275 Laboratory 6 Forced Equations and Resonance

Introduction to SVD and Applications

Computer Labs for. Differential Equations & Linear Algebra

Laboratory 10 Forced Equations and Resonance

Lesson 9: Predator-Prey and ode45

AMATH 301 Beginning Scientific Computing

MATH 373 Numerical Analysis Homework #1 Assigned: January 24, 2018 Due: February 2, 2018

Spis treści Contents List of Examples Preface to Third Edition 21

2D Plotting with Matlab

Project Two. James K. Peterson. March 26, Department of Biological Sciences and Department of Mathematical Sciences Clemson University

1 Introduction to MATLAB

Section 7.4 Runge-Kutta Methods

Matlab for Review. NDSU Matlab Review pg 1

LAB 1: MATLAB - Introduction to Programming. Objective:

Solving Linear Systems of ODEs with Matlab

MATLAB for Engineers

Numerical solution of ODEs

Solving Differential Equations Using MATLAB

Lab 2: Static Response, Cantilevered Beam

Is the World Going to End in 2029?

2 Solving Ordinary Differential Equations Using MATLAB

ES.1803 Topic 13 Notes Jeremy Orloff

Using Matlab to integrate Ordinary Differential Equations (ODEs)

Announcements Monday, September 18

Chart 3 Data in Array

How to Create Matlab Script and Simulink Model for Designing a Pole Placement Controller

2007 Summer College on Plasma Physics

Quantitative Understanding in Biology Module IV: ODEs Lecture I: Introduction to ODEs

Introduction to First Order Equations Sections

WILEY. Differential Equations with MATLAB (Third Edition) Brian R. Hunt Ronald L. Lipsman John E. Osborn Jonathan M. Rosenberg

Homework 1 Solutions

ECE 3793 Matlab Project 3

Transcription:

Lecture 4 Advanced Matlab Programming

Announcements Hands-on Session on Friday 1318 EB Read Chapters 3-6 in your MATLAB book HW 2 opens up Friday evening

Today Numerical analysis - I Visualization I Some more programming

Advanced Matlab Programming Functions Can have variable number of inputs and outputs (see: nargin, nargout, varargin, varargout) Can have internal functions Data types: more than just arrays and strings: Structures Cell arrays File handling Supports most C-commands for file I/O (fprintf, )

MATLAB Compiler Converts function M-files to C or C++ source code (and executables) Uses native compiler to create executables Creates MEX-file from function M-file, e.g. function.m >>mcc x function Creates stand-alone a executable ecutab e from function M-file >>mcc m function

Matlab Compiler

Matlab Compiler

Visualization Matlab is great for plotting functions x = [0:0.01:2*pi]; y = cos(x); () plot(x,y)

Visualization Add titles to graphs and axis title( this is the title ) xlabel( x ) x ylabel( y )

Visualization Multiple plots plot(x,y1, b-,x,y2, r: ) legend( y1, y2 )

Visualization Matlab can also do 3D plots, such as mesh surfaces: [X,Y] = meshgrid(-2:.2:2, -2:.2:2); Z = X.* exp(-x.^2 - Y.^2); mesh(x,y,z)

Visualization Or simple 3D plots: x = -2:.02:2; y = x; z = x.* exp(-x.^2 - y.^2); Plot3(x,y,z,. )

Visualization All calls to plot, plot3, mesh, etc. will overwrite on the current figure. Make sure that you want to erase the current figure. To create a new empty figure, use the command figure

Visualization Adding figures to reports: Print the figure directly Save it to a TIFF file and add to the report (File Export ) Copy to clipboard and paste to the report (Edit Copy Figure) The background is copied too! By default it is gray. e bac g ou d s cop ed oo y de au s g ay To change the background color use: set(gcf, color, white )

Save and Load functions save <filename> saves everything in the workspace (or the function variable space) to <filename>.mat. mat If no <filename> is given, saves to matlab.mat. load <filename> loads the file.

Advanced Read and Write For formatted read and write, learn how to use the fucntions: fprintf fscanf fopen fclose >> x = 0:.1:1; >> y = [x; exp(x)]; >> fid = fopen('exp.txt','w'); t' ' ') >> fprintf(fid,'%6.2f %12.8f\n',y); >> fclose(fid)

Numerical Methods Finding roots of equations Solving matrices (Linear Algebra) Regression / Curve-fitting Numerical Integration Solving ODE s More advanced topics ( solving PDEs) for later

Numerical Integration Use the routines trapz, quad >> help trapz >> help quad (Simpson s Rule) Other useful functions: SUM, CUMSUM, DBLQUAD, TRIPLEQUAD

Examples: Evaluate the following definite integrals

Initial Value Problems (IVPs) Very important topic a large number of practical problems in science and engineering g fall into this category The solution of IVPs (and most ODEs in general) can be challenging g if you do not have the right tools Most realistic systems involve ODEs that must be solved numerically MATLAB has a suite of tools (e.g., ode45)

Examples Imagine you parachute out of a plane v = downward velocity m = mass C = drag coefficient ce Initial Condition: v(0) = 0 dv m mg Cv dt = 2

Microbial uptake To describe microbial utilization of a substrate: dc X C = k a dt K + C S

Systems of Differential Equations dy dt dy dt dy dt dy 1 2 3 n dt y1(0), y2(0), y3(0) are known = f (, t y, y, y y ) 1 2 3 = f (, t y, y, y y ) 1 2 3 = (,,, ) f t y 1 y 2 y 3 y n = f (, t y, y, y y ) 1 2 3 n n n

The Runge-Kutta Algorithm Consider the simple first order equation: dy = f(, t y) dt y (0) = a 1 1 1 1 y = k 1 y + + k δ1 δ2 δ3 δ4 6 + 3 + 3 + 6 δ =Δtf( t, y) 1 k k 1 1 δ2 =Δ tf ( tk + Δ t, yk + δ1) 2 2 1 1 δ 3 = Δ t f ( t k + Δ t, y k + δ 2 ) 2 2 δ =Δ tf( t +Δ t, y + δ ) 4 k k 3

y k Example 1 dy 2 3 Solve: = 2y + e dt y (0) = 1 = 1 1 1 1 1 y + k δ1 2 3 4 6 + + δ δ δ 3 + 3 + 6 δ t t t 1 =Δ f y =Δ y + t 2 ( k, k ) 2 k exp( 3 k) 2 1 1 1 1 δ2 =Δ tf( tk + Δ t, yk + δ1) =Δt 2 yk + δ1 + exp( 3 tk + Δt ) 2 2 2 2 2 1 1 1 1 δ3 =Δ tf( tk + Δ t, yk + δ2) =Δt 2 yk + δ2 + exp( 3 tk + Δt ) 2 2 2 2 t 2 ( ) ( ) δ4 =Δ tf( tk +Δ t, yk + δ3) Δt 2 yk + δ3 + exp( 3 tk +Δt )

eek! too many details!

The same thing in MATLAB Suppose you want to solve the DE: dy 2 2y exp( 3 t); y(0) 1 from t =0 to 5; dt = + = Step 1 Create a function (an.m m file) called myode.m m with the 2 lines function dydt=myode(t,y); dydt=-2*y^2+exp(-3*t); Step 2 Type the following command in MATLAB: [t,y]=ode45('myode',[0,5],1); plot(t,y); 1 2 1.5 0.5 Quite Simple, my dear Watson! 0 0 5 10 15 20 Adventures of Sherlock Holmes --Sir Arthur Conan Doyle

Solving a system of two differential equation (DE) using MATLAB: Two Lakes in Series (Chapra, page 128) dc 1 dt dc2 dt = λ c 11 1 = λ c λ c 21 1 22 2 10 8 6 4 2 Step1. Create the function twolakes.m function [ypr] = twolakes(t,y); 0 0 0.2 0.4 0.6 0.8 1 ypr = zeros(2,1); % This is important! It preallocates a vector with two elements for c1 and c2. ypr(1)=-2.0*y(1); ypr(2)=1.667*y(1)-1.667*y(2); Step2: Type in MATLAB: >> tspan = [0 1]; y0 = [10,0.0];[t,y]=ode45('twolakes',tspan,y0);plot(t,y);