Matlab Instruction Primer; Chem 691, Spring 2016

Similar documents
Lecture 7 Symbolic Computations

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

LAB 1: MATLAB - Introduction to Programming. Objective:

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

Physics with Matlab and Mathematica Exercise #1 28 Aug 2012

EEE161 Applied Electromagnetics Laboratory 1

Homework 1 Solutions

Created: 2/3/96 Modified: September 29, Author: Theresa Julia Zielinski Page 1

USE OF MATLAB TO UNDERSTAND BASIC MATHEMATICS

Note: The command name is upper case in the description given by help, but must be lower case in actual use. And the backslash Anb is dierent when A i

Laboratory handout 1 Mathematical preliminaries

Linear Algebra Using MATLAB

Lab 6: Linear Algebra

(Mathematical Operations with Arrays) Applied Linear Algebra in Geoscience Using MATLAB

Math Assignment 3 - Linear Algebra

I. Units and Conversions 1 A. Atomic Units 1 B. Energy Conversions 2 C. Example: vibrational spacings 2

(Linear equations) Applied Linear Algebra in Geoscience Using MATLAB

Introduction to SVD and Applications

Write a simple 1D DFT code in Python

Companion. Jeffrey E. Jones

5.1 Classical Harmonic Oscillator

Applied Linear Algebra in Geoscience Using MATLAB

Linear Algebra and TI 89

Eigenvalues and Eigenvectors

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

Introduction to Computational Neuroscience

SECTION 2: VECTORS AND MATRICES. ENGR 112 Introduction to Engineering Computing

Algebraic. techniques1

CHEM 301: Homework assignment #5

Math 263 Assignment #4 Solutions. 0 = f 1 (x,y,z) = 2x 1 0 = f 2 (x,y,z) = z 2 0 = f 3 (x,y,z) = y 1

UNIVERSITY OF MISSOURI-COLUMBIA PHYSICS DEPARTMENT. PART I Qualifying Examination. January 20, 2015, 5:00 p.m. to 8:00 p.m.

Lab 2: Static Response, Cantilevered Beam

x x2 2 + x3 3 x4 3. Use the divided-difference method to find a polynomial of least degree that fits the values shown: (b)

ECE 204 Numerical Methods for Computer Engineers MIDTERM EXAMINATION /8:00-9:30

MATLAB crash course 1 / 27. MATLAB crash course. Cesar E. Tamayo Economics - Rutgers. September 27th, /27

2D Plotting with Matlab

Archive of past papers, solutions and homeworks for. MATH 224, Linear Algebra 2, Spring 2013, Laurence Barker

NOTES ON LINEAR ALGEBRA CLASS HANDOUT

Programs for Natural Cubic Spline Interpolation

Lab 2 Worksheet. Problems. Problem 1: Geometry and Linear Equations

Computational Foundations of Cognitive Science

TI89 Titanium Exercises - Part 10.

Basic Math Review for CS4830

Ph2b Quiz - 2. Instructions

Math 98 - Introduction to MATLAB Programming. Spring Lecture 3

4 Gaussian Mixture Models

Project 2: Using linear systems for numerical solution of boundary value problems

Numerical Methods Lecture 2 Simultaneous Equations

REVIEW: The Matching Method Algorithm

tutorial ii: One-parameter bifurcation analysis of equilibria with matcont

Numerical Methods Lecture 2 Simultaneous Equations

Basic Linear Algebra in MATLAB

L3: Review of linear algebra and MATLAB

2 Getting Started with Numerical Computations in Python

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

Google Page Rank Project Linear Algebra Summer 2012

Who am I? Math 1080: Numerical Linear Algebra. Books. Format

New Mexico Tech Hyd 510

Octave. Tutorial. Daniel Lamprecht. March 26, Graz University of Technology. Slides based on previous work by Ingo Holzmann

MOL410/510 Problem Set 1 - Linear Algebra - Due Friday Sept. 30

Physics 202 Laboratory 5. Linear Algebra 1. Laboratory 5. Physics 202 Laboratory

Using Matlab for Laboratory Data Reduction

7. Numerical Solutions of the TISE

Mathematics for Graphics and Vision

Physics 137A Quantum Mechanics Fall 2012 Midterm II - Solutions

Example 7.3.1: For what values of k does the following system of linear equations have no solution, a unique solution, or infinitely many solutions?

26 Group Theory Basics

Basic Math Review for CS1340

Linear Algebra review Powers of a diagonalizable matrix Spectral decomposition

Eigenvalue and Eigenvector Homework

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

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

Applied Linear Algebra in Geoscience Using MATLAB

For a system with more than one electron, we can t solve the Schrödinger Eq. exactly. We must develop methods of approximation, such as

Spring 2019 Exam 2 3/27/19 Time Limit: / Problem Points Score. Total: 280

MITOCW watch?v=ztnnigvy5iq

Laboratory 11 Control Systems Laboratory ECE3557. State Feedback Controller for Position Control of a Flexible Joint

x 3y 2z = 6 1.2) 2x 4y 3z = 8 3x + 6y + 8z = 5 x + 3y 2z + 5t = 4 1.5) 2x + 8y z + 9t = 9 3x + 5y 12z + 17t = 7

Bayesian Analysis - A First Example

a(b + c) = ab + ac a, b, c k

Students should read Sections of Rogawski's Calculus [1] for a detailed discussion of the material presented in this section.

Vibrations and Rotations of Diatomic Molecules

S(c)/ c = 2 J T r = 2v, (2)

R BC. reaction coordinate or reaction progress R. 5) 8pts) (a) Which of the following molecules would give an infrared spectrum? HCl O 2 H 2 O CO 2

(t) ), ( ) where t is time, T is the reaction time, u n is the position of the nth car, and the "sensitivity coefficient" λ may depend on un 1(

. = V c = V [x]v (5.1) c 1. c k

Harmonic Oscillator with raising and lowering operators. We write the Schrödinger equation for the harmonic oscillator in one dimension as follows:

Learning MATLAB by doing MATLAB

Massachusetts Institute of Technology Physics Department

MECH : a Primer for Matlab s ode suite of functions

88 CHAPTER 3. SYMMETRIES

Lecture 3: Special Matrices

ONE AND MANY ELECTRON ATOMS Chapter 15

Chemistry 532 Problem Set 7 Spring 2012 Solutions

We say that a polynomial is in the standard form if it is written in the order of decreasing exponents of x. Operations on polynomials:

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

5.4 Given the basis e 1, e 2 write the matrices that represent the unitary transformations corresponding to the following changes of basis:

SIGNALS AND LINEAR SYSTEMS LABORATORY EELE Experiment (2) Introduction to MATLAB - Part (2) Prepared by:

Chemical Reaction Engineering - Part 7 Richard K. Herz,

These videos and handouts are supplemental documents of paper X. Li, Z. Huang. An Inverted Classroom Approach to Educate MATLAB in Chemical Process

Transcription:

1 Matlab Instruction Primer; Chem 691, Spring 2016 This version dated February 10, 2017 CONTENTS I. Help: To obtain information about any instruction in Matlab 1 II. Scripting 1 III. Loops, determine an array of values, plot the result 2 IV. Work with functions symbolically 3 V. Functions 4 A. Anonymous functions 4 VI. minimum of discrete data 5 VII. Linear Algebra 6 I. HELP: TO OBTAIN INFORMATION ABOUT ANY INSTRUCTION IN MATLAB help instruction % instruction can be any command or hit? on the matlab menu bar. Then go to MATLAB/Getting Started or go to the MATLAB Academy. II. SCRIPTING Example: f = Acos(ωt)

2 A=30; % specify parameters, semicolon silences the return %percentage sign allows comment to follow ome=2.5; % you specify the variable name t=0.1; f=a*cos(ome*t) % built-in function cos(x). no semicolon so the result is printed out A=30;ome=2.5;t=0.1;f=A*cos(ome*t) % one line equivalent Problem 1 The wavefunction for the ground state of the harmonic oscillator with reduced mass µ and force constant k is ( α ψ 0(x) = π ) 1/4e αx 2 /2 where α = kµ. For the H 2 molecule µ = 918 (atomic units) and ω = 0.02 (atomic units). Calculate k and α given that ω = k/µ. The value of r e for H 2 is 1.4 bohr (atomic units). Defining x = r r e write (and save) a Matlab script to determine ψ 0(r = 1.5). III. LOOPS, DETERMINE AN ARRAY OF VALUES, PLOT THE RESULT for it = 1:100 % it is counter, following fortran convention names of integers start % with i,j,k,l,m,n but this is not necessary t = 0.05*it; % determine successive time intervals; note looped commands are indented f(it)=a*cos(ome*t); % store result as a vector f end % terminate loop size(f) % check size of the vector (it should be 100 x 1 (100 rows 1 column) % plot result plot(f) % just plot result, with integer abscissa plot(0.05*[1:100],f, linewidth,1) % plot result f vs t %change width of line from 0.5 px (default) to 1 xlabel( time / s ) % label x axis ylabel( function ) % label y axis

3 set(gca, fontsize,14) % increase size of lables help plot % find out about the many plotting options Problem 2 Write a loop to determine the H 2 v=0 vibrational wavefunction at r=[0.8:0.02:1.9]. Call this array psi values. Then plot these points. IV. WORK WITH FUNCTIONS SYMBOLICALLY clear A t ome clear all % clear values of variables % clear all variables from workspace syms A t ome f=a*cos(ome*t) diff(f,t) diff(f,a) int(f,t) int(f,t,pi/4,2*pi) % declare A, T, omega as symbolic variables % redefine f in terms of symbolic variables % derivative of f with respect to t % derivative of f with respect to A % indefinite integral % definite integral subs(int(f,t,pi/4,2*pi),[a ome],[30 1.2]) % give a definite value to omega % in the result of the integration single(ans) % numerical value for answer g=exp(a*t)/(1+t^2) pretty(g) % more complicated expression % display the expression for f more clearly ff=subs(f,[a ome],[30 2.5]) % give definite values to two of the parameters in f(a,ome,t) ezplot(ff,[-2 4]) % plot the result for -2 <= t <= 4

4 Problem 3 Declare the symbolic variables α and x, then define ψ 0(x,α) as a symbolic function psi0. NB The symbolic variable α should be defined as positive, namely syms alpha x assume(alpha, positive ) % note the apostrophes Show that ψ 0(x) is normalized int(psi0*psi0,-inf,inf) The wavefunction for v=2 is ( α ) 1/4 1 ψ 2 = 2 (2αx 2 1)e αx2 /2 π Define a symbolic function psi2 for ψ 2(x) and show that 2 2 = 1 and 2 0 = 0 The harmonic oscillator Hamiltonian is Ĥ(x) = ˆT + ˆV = 1 ψ 0(x) to show that 0 ˆV 0 = ω/4 2µ d 2 dx 2 + 1 2 kx2. Use your symbolic expression for and 0 ˆT 0 = ω/4 Hint Remember that α = kµ = ωµ. V. FUNCTIONS A. Anonymous functions g=@(t)30*cos(2.5*t) ezplot(g,-2,4) %the @(t) denotes a function of the variable t %use ezplot to plot the function fminbnd(g,1,2) % find the minimum of g in the range 1 <= t <= 2 gg=matlabfunction(ff) % convert any symbolic expression ff to a function % note upper case F int(ff,0,pi) % integrate ff from 0 to pi integral(gg,0,pi) % numerically integrate the anonymous function from 0 to pi

5 VI. MINIMUM OF DISCRETE DATA tt=[1 1.2 1.4 1.6 1.8] % five values of t gx=g(tt) % 30 cos(2.5 t) at these five values plot(tt,gx, o- ) % plot the four values, note the o- commands a line-point plot cc=polyfit(tt,gx,3) % fit these points with a cubic polynomial rr=roots(polyder(cc)) polyval(cc,rr(2)) % find the roots of the derivative of cc, this defines the minima % note that only one value [rr(2)] lies in the range 1 < t < 1.8 % determine the value of the function at this value Problem 4 As a further example you can define an anonymous function of two variables for the wavefunction of the harmonic oscillator ppsi0=@(alpha,xx)(alpha/pi)^(1/4)*exp(-alpha*xx.*xx/2) % note that i m using a different name for the independent % variable and for the function, so as not to redefine the symbolic variables psi0 and x % also note that i m making the definition so that the variable can be a vector in which case % use.* invoking element-by-element multiplication (see the Linear Algebra section below). Then you can define a vector of independent variables, and obtain at once the values of ψ 0(x), t=[0:0.01:1]; plot(t,ppsi0(20,t)) % no FOR loop needed. % Also, the name of the independent variable doesn t have to be xx Define, similarly, an anonymous function of two variables (α and x) for the v=2 wavefunction. Call this function psi2(alpha,xx). Obtain a vector of values of psi2, and plot. Using the Matlab function ginput(1), obtain an estimate of the root of this function. Then, fit 4 points (call these xxx) around the root. xxx=[0.1.2.3] % guess that these straddle the root cc=polyfit(xxx,psi2(alpha,xxx),3); rr=roots(cc) % there will be several roots, pick the one that s physically reasonable % this should be close to the value you got by graphical interpolation

6 VII. LINEAR ALGEBRA vec=[.1.2.4 ] vec % define a row vector of 3 elements % the prime is the transpose, converting this to a column vector vec*vec % the dot product vec *vec % the outer product mat=[.1.2.3;.4.2 -.3;.5 1 0] % a 3x3 matrix, the semicolon indicates the end of a line mat(:,1) % outputs the first column of the matrix as a column vector mat(2,:) % outputs the 2nd row of the matrix as a row vector mat*vec % a matrix-vector product (the result is a column vector) mati=inv(mat) % the inverse of a matrix mati*mat % the product of mat times its inverse; this is equal to the unit matrix identity(3) % the 3x3 unit matrix mats=mat+mat % create a symmetric matrix eig(mats) % the eigenvalues of this matrix [evec eval]=eig(mat) % eigenvectors and eigenvalues of mats diag([.1.2.3.4]) % create a diagonal matrix with [.1.2.3.4] along the diagonal Matlab has an important property, that allows element-by-element multiplication or division of two vectors (or matrices) of the same size vecm=vec.*vec; (or vecd=vec./vec;) % note the. which precedes the % multiplication or division

7 Problem 5 Define the symbolic variables cs, sn, H11, H12 and H22. where cs cos(θ) and sn sin(θ) Define the matrices C=[cs sn;-sn cs] H=[H11 H12;H12 H22] E = C. *H*C E = simplify(e) % diagonalize H % simplify E Then, determine the value of θ so that the off-diagonal matrix element of the C matrix vanishes. With this value of θ, determine the values of the diagonal matrix elements E 11 and E 22. Note: Here, use the symbolic capability of Matlab to help you solve the problem. You ll have to do some simple algebra on your own at the end, to finish the problem.