0% found this document useful (0 votes)
8 views

Programming with MATLAB for Scientists A Beginner s Introduction 1st Edition Mikhailov Eugeniy E instant download

The document is a promotional overview of the book 'Programming with MATLAB for Scientists: A Beginner’s Introduction' by Eugeniy E. Mikhailov, published by CRC Press in 2017. It provides links to download the book and mentions its contents, which cover computing essentials, MATLAB basics, and programming concepts. Additionally, it includes references to other related educational resources and books available for download.

Uploaded by

sheungyusien
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Programming with MATLAB for Scientists A Beginner s Introduction 1st Edition Mikhailov Eugeniy E instant download

The document is a promotional overview of the book 'Programming with MATLAB for Scientists: A Beginner’s Introduction' by Eugeniy E. Mikhailov, published by CRC Press in 2017. It provides links to download the book and mentions its contents, which cover computing essentials, MATLAB basics, and programming concepts. Additionally, it includes references to other related educational resources and books available for download.

Uploaded by

sheungyusien
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Programming with MATLAB for Scientists A

Beginner s Introduction 1st Edition Mikhailov


Eugeniy E pdf download

https://ebookmeta.com/product/programming-with-matlab-for-
scientists-a-beginner-s-introduction-1st-edition-mikhailov-
eugeniy-e/

Download more ebook from https://ebookmeta.com


We believe these products will be a great fit for you. Click
the link to download now, or visit ebookmeta.com
to discover even more!

Introduction to R for Social Scientists A Tidy


Programming Approach 1st Edition Ryan Kennedy

https://ebookmeta.com/product/introduction-to-r-for-social-
scientists-a-tidy-programming-approach-1st-edition-ryan-kennedy/

Applied Numerical Methods with MATLAB for Engineers and


Scientists, 5th Edition Steven Chapra

https://ebookmeta.com/product/applied-numerical-methods-with-
matlab-for-engineers-and-scientists-5th-edition-steven-chapra/

Programming with OpenSCAD A Beginner s Guide to Coding


3D Printable Objects 1st Edition Justin Gohde

https://ebookmeta.com/product/programming-with-openscad-a-
beginner-s-guide-to-coding-3d-printable-objects-1st-edition-
justin-gohde/

Energy Law: An Introduction 2nd Edition Raphael J.


Heffron

https://ebookmeta.com/product/energy-law-an-introduction-2nd-
edition-raphael-j-heffron/
Encryption in SAS 9 4 Sixth Edition Sas Institute Inc

https://ebookmeta.com/product/encryption-in-sas-9-4-sixth-
edition-sas-institute-inc/

HCI International 2022 Late Breaking Papers


Multimodality in Advanced Interaction Environments 24th
International Conference on Human Computer Interaction
HCII 2022 Virtual Event June 26 July 1 2022 Proceedings
Masaaki Kurosu
https://ebookmeta.com/product/hci-international-2022-late-
breaking-papers-multimodality-in-advanced-interaction-
environments-24th-international-conference-on-human-computer-
interaction-hcii-2022-virtual-event-june-26-july-1-2022-p/

International Organizations and Member State


Responsibility Critical Perspectives 1st Edition Ana
Sofia Barros

https://ebookmeta.com/product/international-organizations-and-
member-state-responsibility-critical-perspectives-1st-edition-
ana-sofia-barros/

A Structuralist Theory of Economics 1st Edition Adolfo


García De La Sienra

https://ebookmeta.com/product/a-structuralist-theory-of-
economics-1st-edition-adolfo-garcia-de-la-sienra/

Dirty Love 1st Edition Ainsley Booth

https://ebookmeta.com/product/dirty-love-1st-edition-ainsley-
booth/
Bette Davis Black and White 1st Edition Julia A. Stern

https://ebookmeta.com/product/bette-davis-black-and-white-1st-
edition-julia-a-stern/
Programming with
MATLAB for Scientists
A Beginner’s Introduction
Programming with
MATLAB for Scientists
A Beginner’s Introduction

Eugeniy E. Mikhailov
MATLAB® and Simulink® are trademarks of the MathWorks, Inc. and are used with permission. The
MathWorks does not warrant the accuracy of the text or exercises in this book. This book’s use or discussion of
MATLAB® and Simulink® software or related products does not constitute endorsement or sponsorship by the
MathWorks of a particular pedagogical approach or particular use of the MATLAB® and Simulink® software.

Published in 2017 by CRC Press


Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742

© 2017 by Taylor & Francis Group, LLC

CRC Press is an imprint of Taylor & Francis Group

No claim to original U.S. Government works

Printed in the United States of America on acid-free paper

10 9 8 7 6 5 4 3 2 1

International Standard Book Number-13: 978-1-4987-3831-6 (eISBN)

This book contains information obtained from authentic and highly regarded sources. Reprinted material is
quoted with permission, and sources are indicated. A wide variety of references are listed. Reasonable efforts
have been made to publish reliable data and information, but the author and the publisher cannot assume
responsibility for the validity of all materials or for the consequences of their use.

No part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic,
mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and
recording, or in any information storage or retrieval system, without written permission from the publishers.

For permission to photocopy or use material electronically from this work, please access www.copyright.com
(http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive,
Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration
for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate
system of payment has been arranged.

Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only
for identification and explanation without intent to infringe.

Library of Congress Cataloging-in-Publication Data

Names: Mikhailov, Eugeniy E., 1975- author.


Title: Programming with MATLAB for scientists : a beginner’s introduction / Eugeniy E. Mikhailov.
Description: Boca Raton, FL : CRC Press, Taylor & Francis Group, [2017]
Identifiers: LCCN 2017031570 | ISBN 9781498738286 | ISBN 1498738281
Subjects: LCSH: MATLAB. | Science–Data processing. | Engineering mathematics–Data processing.
| Numerical analysis–Data processing.
Classification: LCC Q183.9.M55 2017 | DDC 510.285/53–dc23
LC record available at https://lccn.loc.gov/2017031570

Visit the Taylor & Francis Web site at


http://www.taylorandfrancis.com

and the CRC Press Web site at


http://www.crcpress.com
Contents

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi

I Computing Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1 Computers and Programming Languages: An Introduction . . . . . . . . . . . . 3


1.1 Early History of Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Modern Computers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.1 Common features of a modern computer . . . . . . . . . . . . . . . . . . 4
1.3 What Is Programming? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 Programming Languages Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5 Numbers Representation in Computers and Its
Potential Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.5.1 Discretization—the main weakness of computers . . . . . . . . . . 7
1.5.2 Binary representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5.3 Floating-point number representation . . . . . . . . . . . . . . . . . . . . . 8
1.5.4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.6 Self-Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

2 MATLAB Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1 MATLAB’s Graphical User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2 MATLAB as a Powerful Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.2.1 MATLAB’s variable types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.2.2 Some built-in functions and operators . . . . . . . . . . . . . . . . . . . . . 15
2.2.3 Operator precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.2.4 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.3 Efficient Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.4 Using Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.5 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.5.1 Creating and accessing matrix elements . . . . . . . . . . . . . . . . . . . 19
2.5.2 Native matrix operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.5.3 Strings as matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.6 Colon (:) Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.6.1 Slicing matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.7 Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.7.1 Saving plots to files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.8 Self-Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

v
vi Contents

3 Boolean Algebra, Conditional Statements, Loops . . . . . . . . . . . . . . . . . . . . . . 31


3.1 Boolean Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.1.1 Boolean operators precedence in MATLAB . . . . . . . . . . . . . . . . 32
3.1.2 MATLAB Boolean logic examples . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.2 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.2.1 Comparison with vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.2.2 Comparison with matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.3 Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.3.1 The if-else-end statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.3.2 Short form of the “if” statement . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.4 Common Mistake with the Equality Statement . . . . . . . . . . . . . . . . . . . 36
3.5 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.5.1 The “while” loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.5.2 Special commands “break” and “continue” . . . . . . . . . . . . . . . . 38
3.5.3 The “for” loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
3.6 Self-Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

4 Functions, Scripts, and Good Programming Practice . . . . . . . . . . . . . . . . . . . 41


4.1 Motivational Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.1.1 Bank interest rate problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.1.2 Time of flight problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.2 Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.2.1 Quadratic equation solver script . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.3 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.3.1 Quadratic equation solver function . . . . . . . . . . . . . . . . . . . . . . . 46
4.4 Good Programming Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
4.4.1 Simplify the code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
4.4.2 Try to foresee unexpected behavior . . . . . . . . . . . . . . . . . . . . . . . 48
4.4.3 Run test cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
4.4.4 Check and sanitize input arguments . . . . . . . . . . . . . . . . . . . . . . 49
4.4.5 Is the solution realistic? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
4.4.6 Summary of good programming practice . . . . . . . . . . . . . . . . . . 51
4.5 Recursive and Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.5.1 Recursive functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.5.2 Anonymous functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.6 Self-Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

II Solving Everyday Problems with MATLAB . . . . . . . . . . . . . . . . . . . . . . 57

5 Solving Systems of Linear Algebraic Equations . . . . . . . . . . . . . . . . . . . . . . . . 59


5.1 The Mobile Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
5.2 Built-In MATLAB Solvers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
5.2.1 The inverse matrix method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Contents vii

5.2.2 Solution without inverse matrix calculation . . . . . . . . . . . . . . . 62


5.2.3 Which method to use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
5.3 Solution of the Mobile Problem with MATLAB . . . . . . . . . . . . . . . . . . . 63
5.3.1 Solution check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
5.4 Example: Wheatstone Bridge Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
5.5 Self-Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

6 Fitting and Data Reduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69


6.1 Necessity for Data Reduction and Fitting . . . . . . . . . . . . . . . . . . . . . . . . 69
6.2 Formal Definition for Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
6.2.1 Goodness of the fit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
6.3 Fitting Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
6.4 Parameter Uncertainty Estimations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
6.5 Evaluation of the Resulting Fit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
6.6 How to Find the Optimal Fit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
6.6.1 Example: Light diffraction on a single slit . . . . . . . . . . . . . . . . . 77
6.6.2 Plotting the data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
6.6.3 Choosing the fit model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
6.6.4 Making an initial guess for the fit parameters . . . . . . . . . . . . . . 79
6.6.5 Plotting data and the model based on the
initial guess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
6.6.6 Fitting the data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
6.6.7 Evaluating uncertainties for the fit parameters . . . . . . . . . . . . . 82
6.7 Self-Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

7 Numerical Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
7.1 Estimate of the Derivative via the Forward Difference . . . . . . . . . . . . 87
7.2 Algorithmic Error Estimate for Numerical Derivative . . . . . . . . . . . . . 88
7.3 Estimate of the Derivative via the Central Difference . . . . . . . . . . . . . 89
7.4 Self-Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

8 Root Finding Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93


8.1 Root Finding Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
8.2 Trial and Error Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
8.3 Bisection Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
8.3.1 Bisection use example and test case . . . . . . . . . . . . . . . . . . . . . . . 97
8.3.2 Possible improvement of the bisection code . . . . . . . . . . . . . . . 100
8.4 Algorithm Convergence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
8.5 False Position (Regula Falsi) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
8.6 Secant Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
8.7 Newton–Raphson Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
8.7.1 Using Newton–Raphson algorithm with the analytical
derivative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
8.7.2 Using Newton–Raphson algorithm with the numerical
derivative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
viii Contents

8.8 Ridders’ Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106


8.9 Root Finding Algorithms Gotchas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
8.10 Root Finding Algorithms Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
8.11 MATLAB’s Root Finding Built-in Command . . . . . . . . . . . . . . . . . . . . . 110
8.12 Self-Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

9 Numerical Integration Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113


9.1 Integration Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
9.2 The Rectangle Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
9.2.1 Rectangle method algorithmic error . . . . . . . . . . . . . . . . . . . . . . . 116
9.3 Trapezoidal Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
9.3.1 Trapezoidal method algorithmic error . . . . . . . . . . . . . . . . . . . . . 117
9.4 Simpson’s Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
9.4.1 Simpson’s method algorithmic error . . . . . . . . . . . . . . . . . . . . . . 118
9.5 Generalized Formula for Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
9.6 Monte Carlo Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
9.6.1 Toy example: finding the area of a pond . . . . . . . . . . . . . . . . . . . 119
9.6.2 Naive Monte Carlo integration . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
9.6.3 Monte Carlo integration derived . . . . . . . . . . . . . . . . . . . . . . . . . . 120
9.6.4 The Monte Carlo method algorithmic error . . . . . . . . . . . . . . . . 121
9.7 Multidimensional Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
9.7.1 Minimal example for integration in
two dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
9.8 Multidimensional Integration with Monte Carlo . . . . . . . . . . . . . . . . . 123
9.8.1 Monte Carlo method demonstration . . . . . . . . . . . . . . . . . . . . . . 124
9.9 Numerical Integration Gotchas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
9.9.1 Using a very large number of points . . . . . . . . . . . . . . . . . . . . . . 124
9.9.2 Using too few points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
9.10 MATLAB Functions for Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
9.11 Self-Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

10 Data Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129


10.1 The Nearest Neighbor Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
10.2 Linear Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
10.3 Polynomial Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
10.4 Criteria for a Good Interpolation Routine . . . . . . . . . . . . . . . . . . . . . . . . 134
10.5 Cubic Spline Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
10.6 MATLAB Built-In Interpolation Methods . . . . . . . . . . . . . . . . . . . . . . . . 135
10.7 Extrapolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
10.8 Unconventional Use of Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
10.8.1 Finding the location of the data crossing y = 0 . . . . . . . . . . . . . 136
10.9 Self-Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Contents ix

III Going Deeper and Expanding the Scientist’s Toolbox . . . . . . . . . . . . 139

11 Random Number Generators and Random Processes . . . . . . . . . . . . . . . . . . 141


11.1 Statistics and Probability Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
11.1.1 Discrete event probability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
11.1.2 Probability density function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
11.2 Uniform Random Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
11.3 Random Number Generators and Computers . . . . . . . . . . . . . . . . . . . . 143
11.3.1 Linear congruential generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
11.3.2 Random number generator period . . . . . . . . . . . . . . . . . . . . . . . . 145
11.4 How to Check a Random Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
11.4.1 Simple RNG test with Monte Carlo integration . . . . . . . . . . . . 146
11.5 MATLAB’s Built-In RNGs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
11.6 Self-Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

12 Monte Carlo Simulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149


12.1 Peg Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
12.2 Coin Flipping Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
12.3 One-Dimensional Infection Spread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
12.4 Self-Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160

13 The Optimization Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161


13.1 Introduction to Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
13.2 One-Dimensional Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
13.2.1 The golden section optimum search algorithm . . . . . . . . . . . . . 163
13.2.2 MATLAB’s built-in function for the one-dimension
optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
13.2.3 One-dimensional optimization examples . . . . . . . . . . . . . . . . . . 165
13.3 Multidimensional Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
13.3.1 Examples of multidimensional optimization . . . . . . . . . . . . . . . 168
13.4 Combinatorial Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
13.4.1 Backpack problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
13.4.2 Traveling salesman problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
13.5 Simulated Annealing Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
13.5.1 The backpack problem solution with the
annealing algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
13.6 Genetic Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
13.7 Self-Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

14 Ordinary Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195


14.1 Introduction to Ordinary Differential Equation . . . . . . . . . . . . . . . . . . . 195
14.2 Boundary Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
14.3 Numerical Method to Solve ODEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
14.3.1 Euler’s method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
14.3.2 The second-order Runge–Kutta method (RK2) . . . . . . . . . . . . . 199
x Contents

14.3.3 The fourth-order Runge–Kutta method (RK4) . . . . . . . . . . . . . . 200


14.3.4 Other numerical solvers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
14.4 Stiff ODEs and Stability Issues of the Numerical Solution . . . . . . . . . 201
14.5 MATLAB’s Built-In ODE Solvers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
14.6 ODE Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
14.6.1 Free fall example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
14.6.2 Motion with the air drag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
14.7 Self-Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212

15 Discrete Fourier-Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213


15.1 Fourier Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
15.1.1 Example: Fourier series for |t| . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
15.1.2 Example: Fourier series for the step function . . . . . . . . . . . . . . . 217
15.1.3 Complex Fourier series representation . . . . . . . . . . . . . . . . . . . . 218
15.1.4 Non-periodic functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
15.2 Discrete Fourier Transform (DFT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
15.3 MATLAB’s DFT Implementation and Fast Fourier
Transform (FFT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
15.4 Compact Mathematical Notation for Fourier Transforms . . . . . . . . . . 222
15.5 DFT Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
15.6 Self-Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

16 Digital Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229


16.1 Nyquist Frequency and the Minimal Sampling Rate . . . . . . . . . . . . . . 229
16.1.1 Under-sampling and aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
16.2 DFT Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
16.2.1 Low-pass filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
16.2.2 High-pass filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
16.2.3 Band-pass and band-stop filters . . . . . . . . . . . . . . . . . . . . . . . . . . 235
16.3 Filter’s Artifacts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
16.4 Windowing Artifacts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
16.5 Self-Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Preface

Intended Audience
This book is intended for anyone who wants to learn how to program with
MATLAB and seeks a concise and accessible introduction to programming,
MATLAB, and numerical methods. The author hopes that readers will find here
all necessary materials for handling their everyday computational and program-
ming problems. Even more seasoned readers may find useful insights on familiar
methods or explanations for puzzling issues they might encounter.
We will start with simple concepts and build up a skill set suitable to model,
simulate, and analyze real-life systems. Additionally, this book provides a broad
overview of the numerical methods necessary for successful scientific or engineer-
ing work. We will get familiar with a “lore” of computing, so you will know what
to look for when you decide to move to more advanced techniques.
The book is based on material of the one semester “Practical Computing for
Scientists” class taught at the College of William & Mary for students who have
not yet declared a major or students majoring in physics, neuroscience, biology,
computer science, applied math and statistics, or chemistry. The students who
successfully took this class were at all levels of their academic careers; some were
freshmen, some where seniors, and some were somewhere in between.

Why MATLAB?
A couple words about MATLAB, as it is our programming language of choice.
MATLAB has a good balance of already implemented features, which are impor-
tant for scientists and for ease of learning. MATLAB hides a lot of low-level details
from users: you do not need to think about variable types, compilation processes,
and so on. It just works. You can also do a calculation on a whole array of data
without tracking every element of the array. This part is deep inside of MATLAB.
From an instructor’s point of view, you do not need to worry about the instal-
lation of MATLAB for your class. It is easy and students are capable of doing it
alone. More importantly, it looks and works the same on variety of operational
systems, such as Windows, Mac, and Linux. MATLAB produces exactly the same
result on all computers.
From a student’s point of view, MATLAB is probably the most frequently
required programming language for an engineering or scientific position. There-
fore, if you learn MATLAB now, you likely will not need to retrain yourself to
another industry standard programming language.

xi
xii Preface

MATLAB has a downside: it is expensive to purchase if your school or work-


place does not provide it. This is not a big worry; you can do exercises from all the
chapters, except the data fitting, with a free alternative: GNU Octave. The fitting in
Octave uses a different set of commands, but everything else will work the same
(you might need minor tweaking for more advanced options).

What Is not Covered in This Book?


This book does not extensively cover MATLAB’s commands. There is no reason to
write another manual for MATLAB (which has an excellent one already) or redo
tutorials available on the web.
This book is also not a substitute for a book explaining the ins and outs of
numerical methods. Whenever possible, we discuss what interesting things can
be done with a numerical method and do not bother with the most efficient imple-
mentation. However, the beginning of the book is an exception. There, the basics
of programming are explained via implementations of some numerical method
algorithms (which often have MATLAB’s built-in equivalents already).

How to Read This Book


If you have programmed before, you can skip the majority of Part I, but make sure
that you are fluent with the element operations described there, the differences
between array and element-wise operations, and array slicing.
If you are scientist, then the plotting and fitting materials are a must. Make
sure that you have read the fitting chapter. If you need to learn anything important
about data analysis, then learn this.
The material in Part III is somewhat optional, although the author strongly
recommends the optimization problem chapter (see Chapter 13). It is amazing to
see how many other problems are essentially optimization problems and can be
solved with methods presented there. The time to use the material in this section
will probably come in upper undergraduate classes.
As you get more and more fluent with programming, reread the good pro-
gramming practice materials in Section 4.4 and try to implement more and more
techniques from there.
Preface xiii

Data files and MATLAB listings locations


All MATLAB’s listing and required data files used in the book are available on
the web.* The PDF version of this book contains direct links to such files.

MATLAB® is a registered trademark of The MathWorks, Inc.


For product information, please contact:
The MathWorks, Inc.
3 Apple Hill Drive Natick,
MA 01760-2098 USA
Tel: 508 647 7000 Fax: 508-647-7001
E-mail: info@mathworks.com
Web: www.mathworks.com

* Please visit http://physics.wm.edu/programming_with_MATLAB_book.


Other documents randomly have
different content
After the battle of Port Republic, which closed Jackson’s great
Valley campaign, the army moved up the mountain, where, upon its
summit, it remained two or three days, when it returned and went
into camp near Weir’s Cave, about five miles from Port Republic.
Here the Baltimore Light Artillery was supplied with new harness
and fresh horses, and was in a splendid condition for the dreadful
fighting about to commence around Richmond, but of which we had
not then the slightest conception.
On the 19th of June, 1862, Jackson put his troops in motion for
Richmond, and on the afternoon of the 26th the First Maryland
Infantry, which had the advance, encountered the enemy in force
about ten miles from Gaines’ Mills, and a sharp fight ensued, which
lasted some time, when Brockenborough was ordered to open fire,
which he did with effect, and the enemy retired.
At daylight on the morning of the 28th, Jackson resumed his
march, but owing to the incompetency of his guides it was late in the
afternoon before he neared the point of attack assigned him. But at
length everything indicated a rise of the curtain in the fearful drama
about to commence. Columns were marching and countermarching,
staff officers dashed hither and thither, while the crash of small
arms, and the sullen boom of artillery on the right told full well that
the work of death had begun.
About 4 o’clock Jackson threw out his skirmishers and moved
forward in line of battle, and in a few minutes the enemy were
developed in heavy force, and strongly posted, when the fighting
became terrific. The artillery was directed to take position in an open
field on the left, and were soon heavily engaged. The battle here was
very unequal, for the enemy had greatly the advantage in artillery
and position, and soon succeeded in disabling a number of Jackson’s
pieces. In a short time the Jeff Davis Mississippi battery was torn to
pieces and the Baltimore Light Artillery ordered to take its place,
immediately under the eye of Jackson himself. Gallantly the
Marylanders responded to the order, and dashing at a full run across
the field, unlimbered and opened fire.
The author was standing close beside General Jackson when the
battery went forward, and he shall long remember the look of anxiety
with which he watched it, and well he might, for upon the success of
that battery much depended. For a while the air was filled with
exploding, crashing shells, and the horses and men fell rapidly before
that withering fire, which was directed with almost the precision of a
rifle shot. Away went a limber chest high in the air, scattering death
and destruction around. “We are not close enough,” said the brave
Brockenborough. “Limber to the front, forward, gallop!” rung out his
sharp command, and in an instant the battery was in position at
point blank range. Fiercely those guns were then worked despite the
iron hail that ploughed up the ground around them, and in a few
minutes Brockenborough had the satisfaction of seeing the enemy
retire precipitately, leaving the ground covered with dead and dying
men and horses, and shattered carriages and dismounted guns. It
was French’s famous battery they had encountered, but French’s no
longer, save in name.
On the morning of the 29th, the battery accompanied Ewell’s
division to Dispatch Station, on the York River railroad, where a few
shots were exchanged with the enemy, when Ewell retraced his steps,
and moved towards Malvern Hill.
In the afternoon of the 1st of July the battle of Malvern Hill began,
and soon raged fiercely. Through the tardiness of General Huger the
enemy had been enabled to reach the heights of Malvern, where he
posted sixty guns, which swept every foot of ground around. In vain
did the heavy masses of infantry rush with desperate valor upon
these guns, but it was only to be driven back, leaving the ground
covered with heaps of dead and mangled men. In this unequal
contest artillery was not available, for not a position was to be had.
Two or three times the Baltimore Light Artillery tried it, but was as
often compelled to hastily withdraw, and when night ended the
conflict, Malvern Hill was not yet won.
Before morning McClellan withdrew to Harrison’s landing on the
James, where he was safe, and General Lee had the mortification of
knowing that the foe so completely within his toils had escaped him
through the incapacity of a General of division.
CHAPTER II.

T he long spring campaign in the Valley and the operations around


Richmond had made sad havoc in the ranks of the Maryland
Line, and it became necessary for them to recruit and reorganize
before again ready for the field. For this purpose the First Maryland
Infantry and Baltimore Light Artillery were ordered to
Charlottesville, where they remained a month, when they were once
more ordered to join Jackson, who was about to make his great
movement to the rear of Pope’s army at Manassas. Alas, the two
commands did not journey together far, for at Gordonsville an order
overtook Colonel Johnson requiring him to at once disband the First
Maryland, and the order was reluctantly obeyed.
The separation was affecting to the greatest degree, and the little
battery pursued its way with sad and lonely hearts. It was like
severing the ties that bind brother to brother, for in the series of
battles in which they had participated side by side, the conduct of
each had inspired the other with confidence and respect. “With the
First Maryland in support,” I heard Captain Brockenborough say, “I
know I am always safe.” And so it was, for one would never desert the
other while life lasted.
On the morning of the 19th of August the battery reached Orange
Court House, where, much to the joy of all, it was attached to Starke’s
Louisiana brigade. An affinity had long existed between the
Maryland and Louisiana troops, and they commanded each others’
fullest confidence. In fact they seemed nearer akin, for in both there
was that sprightliness, dash and vim not so noticeable in troops from
other States.
With three days’ rations in haversacks, Brockenborough, on the
21st, moved towards the Rappahannock, where he found the enemy
occupying the north bank in force. A severe artillery fight
immediately began, and was maintained for some hours. The battery
pitted against Brockenborough was Company M, United States
regulars, which, towards nightfall, he succeeded in silencing and
driving back with the loss of many men and an exploded caisson.
On the morning of the 22d, the artillery was thrown across the
river, but soon after encountered the enemy in heavy force, and were
compelled to recross after a desperate struggle. In this affair the
Baltimore Light Artillery suffered a loss of four men killed—Irvin,
Cox, Bradley and Reynolds—and several severely wounded.
Brockenborough finding it impossible to cross at that point, moved
up to Hanson’s Ford, where a crossing was effected, and he then
pursued his way through Orleans, Salem and Thoroughfare Gap, and
reached Manassas on the 26th, having marched fifty miles in two
days, with nothing for his men or horses to eat save the green corn
gathered along the road. Here at Manassas, though, was found in the
captured trains and sutlers’ stores all they could have desired, and
for hours they revelled in the good things their new commissary had
so bountifully supplied, and over Rhine wine and lobsters forgot for
the time the privations of the past few days.
From Manassas, Jackson moved on Centreville, but finding the
enemy there in force, he retraced his steps to Manassas, closely
pursued, and formed his line of battle, about sunset on the 28th,
upon the ground occupied by the enemy in the battle of July, 1861.
The engagement immediately commenced, and raged with great
fury for some time, but the enemy was repulsed in every assault, and
driven back with heavy loss. General Stephen D. Lee, who
commanded the whole of Jackson’s artillery, then put the several
batteries in position along the crest of a commanding hill, and there
awaited the attack sure to be renewed next day.
About two o’clock on the 29th heavy columns emerged from the
woods in Jackson’s front, and advanced boldly to the attack, but the
storm of grape and canister which tore through their ranks was more
than flesh and blood could withstand, and they were driven back
with dreadful slaughter. But again and again did those devoted
columns reform and return to the attack with undiminished ardor,
but the same terrible fire greeted them, and strewed the ground with
dead and dying.
But nevertheless Jackson’s situation was a most critical one. With
but a handful of worn and wearied troops he was battling with ten
times his numbers, which must necessarily soon wear him out and
exhaust his ammunition; but as the hearts of his men were sinking
within them, they were cheered by the clouds of dust that arose in
the distance and heralded the approach of their great chieftain, Lee,
with the veterans of Longstreet’s corps. At night the battle ceased,
and the wearied troops threw themselves upon the ground to seek a
little repose before the work of death and destruction should be
resumed on the morrow.
At the break of day on the morning of the 30th of August, the
troops were aroused from their slumbers and ordered to prepare for
the great and decisive battle at hand. But hour after hour passed by,
and except an occasional picket shot, all else was still. It was, though,
but the calm which precedes the storm, for suddenly dense masses of
the enemy emerged from the woods, and moved at the double-quick
upon Jackson’s lines. It was a grand sight to see those three lines
rush forward in the most beautiful order. For a minute a deathlike
silence prevailed, when the very earth was made to tremble by the
roar of Stephen D. Lee’s thirty-six pieces of artillery, fired at point
blank range. The slaughter was appalling, and whole ranks melted
away in an instant, but the brave survivors closed up their decimated
columns, and despite that awful fire pressed on until they
encountered the infantry posted in the railroad cut in front, where
for a time the fight was waged hand to hand. At length they began to
break and to retreat, and the batteries, which had been silent for
some time owing to the proximity of the struggling columns of
infantry, again belched forth into the fleeing mass their deadly
discharges of grape, which was continued until the fugitives reached
the shelter of the woods from which they had emerged.
Of the several batteries under General Lee that day, not one was
worked more fiercely than the Baltimore Light Artillery, and none
contributed more to the defeat and destruction of the enemy.
Long before nightfall the victory was won, and the braggart Pope,
with the remnant of his army, was seeking safety in the defences
around Washington.
In the invasion of Maryland, which followed this signal victory, the
battery was placed in the advance, and crossed the river at White’s
Ford.
On the 6th of September the battery passed through Frederick city,
and encamped on the suburbs. Many were the congratulations the
brave fellows received from the citizens, and during the three days
they remained, their wants were abundantly supplied.
Leaving Frederick city, the battery passed through Boonsboro,
Middletown, and Williamsport, where they recrossed the Potomac,
and on the 12th entered Martinsburg. From thence it moved towards
Harper’s Ferry, when upon arriving at Loudoun Heights
Brockenborough was assigned a position from which, at early dawn
of the 15th, he opened, along with other batteries, a terrific fire upon
the enemy’s entrenched position on Bolivar Heights. The batteries
were worked furiously for an hour, when just as the Confederate
infantry were put in motion to storm the works, a white flag fluttered
in the breeze, and Harper’s Ferry surrendered with its twelve
thousand troops, and artillery and supplies in abundance.
But there was heavy work yet to be done, for General Lee with a
portion of his army was confronting the overwhelming masses of
McClellan at Sharpsburg, and no time was to be lost in reaching him.
The surrender had therefore scarcely been effected when the troops
were dispatched to his aid. By a forced night march Jackson’s
artillery reached Sharpsburg on the 16th, and was immediately
assigned a position on a range of hills rather northwest of the town.
The morning of the 17th of September found the two armies in
position, and ready to begin the work of destruction. For the
Confederates the prospects of success seemed gloomy enough, for
General Lee had barely forty thousand men with which to meet the
mighty army of McClellan, numbering over one hundred and twenty
thousand troops. But the vast odds were made almost proportionate
by the superior genius of the Confederate Generals. With Lee,
Jackson and Longstreet in command nothing seemed impossible to
their troops, and therefore it was with no feelings of fear for the
result that they surveyed the long and glittering lines before them.
Soon after sunrise slight artillery skirmishing commenced along
the lines, which increased in volume until the air seemed filled with
exploding shells. Upon the position held by the batteries of
Brockenborough, Carpenter, Poague, Moody, Rain and Caskie, was
opened a terrific fire, which was promptly returned, and the enemy’s
batteries several times compelled to change position. This continued
for two hours, when it became evident that the infantry was massing
for a charge. The position was of the most vital importance, for
should the enemy succeed in gaining possession of this point, and
turning Lee’s left flank, he would be irretrievably lost. His orders to
General Jackson were therefore to “hold the range of hills to the
last.”
McClellan’s advance upon this point was gallantly met by
Jackson’s veteran infantry, and for some time the fighting was of the
most determined character; but at length the immense superiority of
numbers prevailed, and Jackson’s troops gradually fell back across
the turnpike, past the Dunkard Church and through the woods, and
appeared upon the plain beyond. Most beautifully did the heavy
columns emerge from the woods and move forward upon the
batteries quietly awaiting their nearer approach. “Do not pull a
lanyard,” said Brockenborough, who was temporarily in command of
the whole, “until you get the command.” Nearer and nearer those
solid columns approached, and amid loud huzzas rushed forward at
the double-quick. It was a moment of dreadful suspense. On, on,
they came. “Will Brockenborough never give the command?” Yes; he
now has them at the muzzles of his guns, and the next instant the
command “Fire!” was heard above the exultant cheers of the
advancing columns, and twenty-four pieces of artillery, double
shotted with canister, belched forth their deadly contents into the
very faces of the assailants.
The scene that was presented as the smoke lifted beggars
description. The ground was literally covered, nay piled, with the
slain and maimed of the enemy, and the survivors were in full
retreat. They were soon reformed, however, and again moved boldly
to the attack, but only to be again mercilessly slaughtered and driven
back. A third time they essayed, but with the same result, when, a
disordered mass of fugitives, the survivors sought the shelter of the
woods from which they had but a few minutes before emerged,
confident of success.
How anxiously the great chieftain, Lee—who was close by—must
have watched the dreadful struggle which was to decide the fate of
his army, and perhaps of the cause for which he was battling; and
how great must have been the relief as he saw the enemy in retreat
and Jackson’s shattered columns once more reformed.
Night put an end to the dreadful conflict, and Lee still held his
ground, despite the herculean efforts of his adversary to drive him
from it, but the day’s struggle had cost him thousands of his bravest
and best.
The brave Brockenborough that day won his Major’s star, and,
with his battery, received special mention in General Lee’s official
report.
The morning of the 18th broke clear and beautiful, and General
Lee was in readiness to renew the fight, which it was not doubted
would begin at an early hour. But McClellan’s beaten and shattered
army required time and rest and reinforcements before again
prepared for aggressive operations; and finding this to be the case,
Lee proceeded to bury his dead, and that night, unmolested,
recrossed the river at a point near Shepherdstown.
CHAPTER III.

S oon after the battle of Sharpsburg, the Baltimore Light Artillery,


now under command of Captain W. H. Griffin, was ordered to
join the cavalry and infantry of the Maryland Line, then encamped
near New Market, in the Valley of Virginia. Here they passed the fall
and winter months quietly in camp, and in early spring were again
prepared, with recruited ranks and renewed equipments, to enter the
field.
On the 13th of June the infantry and artillery of the Maryland Line,
with one company of its cavalry, all under command of Lieutenant-
Colonel James R. Herbert, moved towards Winchester, near which
place they were to unite with the division of General Early, which
was moving across from Front Royal. Near Kearnstown a body of
Milroy’s cavalry was encountered, but a shot from the battery
scattered them in all directions. A short time after, Early came up
and proceeded to form line of battle. The enemy soon made his
appearance in force, and opened a severe fire upon Griffin from his
batteries, which was vigorously responded to, and in a little while the
enemy were driven beyond Kearnstown. Early, as soon as he had
formed his line of battle, moved forward, and by a spirited charge of
Gordon’s brigade, drove him into his strong works to the left of
Winchester.
The next afternoon Hays’ Louisiana brigade was moved around to
the enemy’s right with orders to charge a strong line of works whilst
the artillery opened upon him in front. Griffin was posted on a
commanding hill a little to the left of the pike, and threw his first
shell into the very centre of the Star fort. Finding he had the exact
range, he commenced a furious fire, which threw the enemy for a
moment into the greatest confusion, and greatly assisted Hays in his
movement upon their right. The fire was soon spiritedly returned by
a Federal Maryland battery, and continued until night, when Milroy
evacuated his fortifications and attempted to escape with his army,
but in this he was unsuccessful, although he himself succeeded in
reaching Harper’s Ferry with a few of his troops.
The precision and effect with which the guns of the Baltimore
Light Artillery were served upon this occasion elicited the highest
praise from General Gordon, to whose brigade it was temporarily
attached, and as a mark of the high esteem in which he held the
battery, he procured them permission from General Ewell next
morning to select from among the captured guns the best pieces, to
take the place of their own, which were greatly inferior.
The day after the battle of Winchester, the corps of General Ewell
took up its line of march towards the Potomac. The Baltimore Light
Artillery was directed by some subordinate officers to report to
General Nelson of the reserve artillery. The order occasioned the
greatest surprise and indignation throughout the command, for
always before they had led the advance and covered the retreat. Such
an indignity, as they considered it, could not be tamely submitted to,
and a protest was immediately drawn up and forwarded to General
Ewell, who at once ordered the battery to join Albert G. Jenkins’
brigade of cavalry, which was the van of the army in the invasion of
Pennsylvania.
The battery crossed the Potomac on the 18th of June, and that day
joined Jenkins, when the whole command moved rapidly forward in
the greatest good humor. Many were the jokes they practiced, and
many the quaint sayings, peculiar only to the soldier. “Take them
mice out of your mouth,” one would bawl out as an officer with well
waxed moustache rode by; “take ’em out, no use to say they ain’t
thar, for I see their tails stickin out.” And as another came along, but
a short time in the service, and wearing a “boiled shirt,” and white
collar, his ears were sure to be assailed with “say mister how long did
you have to soldier ‘afore one of them things growd’ round your
neck?” And the staff officer, with handsome cavalry boots, would be
requested by a dozen voices to “come out of them thar boots, for it’s
too soon to go into winter quarters.”
En parenthèse, soldiers are queer beings, and will have their joke,
even in the face of almost certain death. At the battle of Malvern Hill,
whilst the First Maryland regiment was awaiting its turn to “go in,”
and the men were closely hugging the earth to avoid the terrible fire
of grape and canister which swept over and around them, I heard an
officer of the regiment remark to another at his side, whose face was
pressed close to the ground, “say, Captain, you’ll get a scrape down
your back directly, and you know it’s something we don’t allow here,”
and the officer addressed coolly turned over on his back, remarking,
“well, if it will please you better, I’ll take it in front.”
The command of Jenkins pursued its march rapidly through
Maryland, and struck the Pennsylvania line near Greencastle. Thence
their way lay up the Cumberland Valley to Shippensburg, where a
halt was made for a short time to allow the tired troops to partake of
the delicious apple-butter, ham, bread, &c., furnished them in
abundance by the startled inhabitants. Whilst thus enjoying
themselves to their hearts’ content, the cry of “Yanks” was raised,
and in an instant the scene changed. Cavalrymen sprang to their
horses, and artillerymen to their guns, but the wary enemy could not
be induced to come within range of Griffin’s Parrott’s, but retired
towards Carlisle, followed leisurely by Jenkins.
Upon arriving within sight of that town, the Yankee flag was found
defiantly flying from the public buildings, when Jenkins, supposing
the enemy to be there in force, prepared to attack. Placing two pieces
of artillery in a position to rake the main street, and disposing of his
other troops in the most available manner, he demanded the
surrender of the town. It was not long before a deputation of the
“solid” citizens made their appearance, and surrendered the place,
which General Jenkins at once took possession of.
After remaining at Carlisle one day, during which time they were
bountifully supplied with provisions and forage by the citizens, the
command moved in the direction of Harrisburg. At Mechanicstown a
small body of cavalry were encountered, but a shot from Griffin
caused them to heat a hasty retreat. Upon reaching the
Susquehanna, the Confederate commander found the opposite side
of the river strongly fortified, but he at once opened fire from his
batteries, which was promptly responded to. This continued until
late in the afternoon, when the enemy’s infantry advanced in force,
and a severe skirmish ensued, which lasted until after night. General
Rhodes (I have forgotten to mention the fact that Jenkins and
Rhodes united their forces at Carlisle) having accomplished his
purpose, that night withdrew in the direction of Gettysburg.
Upon arriving at Gettysburg the battery was ordered to report to
Major Latimer, who assigned it a position a short distance to the left
of the Cashtown pike. In the terrible battle that ensued, the
Baltimore Light Artillery played its part, and when the retreat
commenced it was kept in the rear to assist in covering the passage of
the army and wagon trains through the mountain passes.
At a point near Mount Zion the enemy had so stationed his guns as
to completely command the road through the gap over which a
column of infantry must pass. There was but little time to spare, for
the enemy were pressing them hard. Captain Griffin was ordered to
place his guns in position, and if possible silence the battery. It
seemed a desperate undertaking, but there was no alternative.
Quickly the brave fellows ran their guns to within point blank range,
and opened a deadly fire. It was promptly returned, and a heavy
artillery duel continued for some time, when the enemy’s battery was
driven from its position, and the infantry and wagon train passed in
safety. I have heard it asserted by old soldiers that this was one of the
most desperate artillery fights they ever witnessed. And the
Marylanders had every reason to be proud of their victory, for it was
their old antagonist, battery M, of the regular artillery, that they had
again measured strength with.
At Hagerstown the battery participated in a severe cavalry fight, in
which the Confederates were entirely successful.
After the army of General Lee recrossed the Potomac, the battery
was ordered to Fredericksburg, where it remained for a few days, and
then rejoined the main army at Culpepper Court House, and was
assigned to the battalion commanded by Major Beckham.
For two weeks everything remained quiet, and well it was, for
human endurance had been taxed to the utmost limit in the severe
campaign of the summer, and required rest and repose.
On the 10th of September Meade became restive, and General Lee
moved forward to give him battle if he desired it. Beckham was
ordered to advance and take position with his artillery near Muddy
Creek. In a short time the enemy appeared in force, and a sharp
artillery fight ensued. The enemy then threw forward his infantry,
and compelled Beckham and his supports to fall back to the vicinity
of Culpepper Court House. The artillery was here ordered to take a
position and “hold it.” The fighting soon became fierce, and the
Baltimore Light Artillery was exposed to the severest part of it, but
they gallantly held their ground for some time, despite the fire of six
pieces that were playing upon their three. But this could not last
long, for all support had been withdrawn, and the enemy’s
dismounted men were advancing in heavy force. It seemed scarcely
possible to save the battery, but the brave fellows had been in such
scrapes before, and they determined to hold on to their pieces as long
as there was a hope. Retiring through the town, they had nearly
accomplished their purpose of escaping, when a body of the enemy
charged up a cross street and captured the rear gun, with Lieutenant
John McNulty and nine men attached to it. The remaining guns were
safely taken off the field.
The next morning the enemy made their appearance in
considerable force, and the battery was enabled to repay them for the
rough treatment received the day preceding, for as a body of cavalry
were engaged in drill, entirely ignorant of the close proximity of the
Baltimore battery, it opened upon them with deadly effect at very
close range.
After the affair at Mine Run, which soon followed that at
Culpepper Court House, the battery was detached from the main
army, and temporarily assigned to duty with General Young’s
brigade of cavalry. On the 9th of October that General crossed the
Rapidan, and advanced by way of Madison Court House. His
progress was slow, as the march was by circuitous and concealed side
roads in order to avoid the observations of the enemy. On the 10th
Young met the advance of the enemy at James City, without an
intimation of his approach, and their bands were regaling the
citizens with patriotic airs, when a shot from Sergeant Harry
Marston’s gun, of the Baltimore Light Artillery, plunged into their
midst, and abruptly terminated the musical entertainment for that
evening at least. In a short time a battery was brought up, and a
severe artillery fight ensued. Soon another made its appearance and
opened an enfilading fire upon Griffin, but notwithstanding, he
stubbornly held his position. Whilst this was going on a large force of
the enemy’s skirmishers attacked the Confederate flank, and threw
the cavalry into some confusion by their unexpected onset from this
quarter. Griffin at the instant wheeled his pieces, and opened with
grape and canister upon this new enemy. This checked them until
the supports came up, when the enemy were compelled to retreat
precipitately.
On the 12th a sharp engagement took place near Brandy Station,
where the enemy were driven across the Rappahannock, and
pursued to the vicinity of Cub Run.
After this affair the army retraced its steps to Culpepper Court
House, when the Baltimore Light Artillery was ordered to proceed to
Hanover Junction, and report to Colonel Bradley T. Johnson, who
had been directed to assemble the Maryland Line at that point, and
keep open General Lee’s communication with Richmond.
The battery spent the winter and part of the spring here most
delightfully, and by the 1st of May was ready for the field with
recruited ranks, for Lieutenant John McNulty and many of the men
captured in the battles of the preceding year had returned from
Yankee prisons, and were once more ready for the fray.
CHAPTER IV.

O n the 10th of May, 1864, whilst encamped at Wickham’s Park,


the battery was ordered by General J. E. B. Stuart, to move up
along the Rivanna, and join the forces there awaiting to intercept
Sheridan, who was advancing towards Richmond. Reluctantly
Colonel Johnson suffered it to go, for during the winter and spring
he had reorganized and equipped it with much care; but the exigency
of the occasion compelled him to acquiesce, General Stuart assuring
him he would “borrow” it for but a few days, and “return it in good
condition.”
On the 11th the battery took position at Yellow Tavern, and noon
after the enemy made his appearance in force. A heavy encounter
ensued, when the battery was retired about half a mile. For a time
there was a lull in the fighting; but upon the arrival of General Stuart
it was again ordered forward supported by the cavalry, and took
position to the left of the Brook turnpike, directly in Sheridan’s front.
The battle was then renewed with great fury. Sheridan brought three
batteries to bear on Griffin at a range of not over eight hundred
yards, and the rain of shot and shrapnel became terrific, but the
brave fellows never flinched, and served their guns with great effect.
Hour after hour this savage fight was waged, but no man faltered at
his post, though the groans of the wounded and dying, and the
shrieks of maimed and disemboweled horses, were enough to appall
the stoutest heart. But General Stuart was there, watching with an
anxious eye that little command, upon which so much depended, and
they fought on, undismayed, despite the frightful scenes around
them. At length the enemy massed a heavy body of cavalry,
determined, at any sacrifice, to capture the guns that were making
such dreadful havoc in their ranks. A charge was made upon him,
when Griffin resorted to grape and canister. At every discharge whole
companies melted away, and the enemy fell back in confusion. But
again they advanced, and the Confederate cavalry giving away at the
instant, the battery was left at the mercy of the enemy, who dashed
upon it; but there the brave men continued to stay, determined to
remain at their post to the last, for all knew the vital importance of
the position; and as the enemy pressed on they were met with that
never ceasing hail of canister, until they reached the guns and rode
over the men, and sabred and captured them at their pieces. Stuart
had witnessed it all whilst rallying his broken cavalry, when seizing
the colors of the First Virginia, he rode forward, exclaiming, “Charge,
Virginians, and save those brave Marylanders!” Alas, it was his last
command on the field of battle, for at the instant he received a pistol
shot, and was conveyed mortally wounded from the field, when his
men precipitately retreated.
Nevertheless, in the confusion and excitement of the moment,
Lieutenant McNulty, with some of the gallant fellows, actually drove
two pieces off in triumph, despite the efforts of the enemy to prevent
them.
In this desperate battle at Yellow Tavern, the battery suffered the
loss of many men and horses, and two guns, and its brave
commander was a prisoner in the enemy’s hands.
Many were the acts of individual heroism displayed whilst the
battle lasted, one of which I will narrate:
During the hottest of the fight private John Hayden was struck by
a piece of shell, and dreadfully mangled, and would have bled to
death in a few minutes had not the Surgeon of the battery, Dr.
Wortham, carried him on his back into the woods and staunched the
hemorrhage. In a short time the enemy had possession of the field,
but carefully concealing himself and his charge until they had passed
on, he that night carried Hayden to a place of safety, where he
eventually recovered.
With the two guns saved from the wreck of the battery, Lieutenant
McNulty crossed the Chickahominy, closely pursued, and took
position on the right of the road, commanding the bridge, where, by
a vigorous fire, he checked the enemy’s advance and covered the
retreat. McNulty then pushed on to Old Church and joined the main
body, which had been there reassembled.
On the 13th he was ordered to Hanover Junction, where he joined
the army of General Lee, with which he remained until after the
battle of Cold Harbor, when he was ordered to Early’s command in
the Valley of Virginia.
After a march of six days, the battery reached Waynesboro’, where
four days after it joined Early, en route for the Lower Valley. Here
the battery (now under the command of Lieutenant W. B. Bean, who
had been for some time absent) was attached to General Bradley T.
Johnson’s brigade of cavalry, which had the advance.
On the morning of the 4th of July, Johnson approached
Martinsburg, when he was charged by about six hundred of the
enemy’s cavalry, which for a moment created some confusion in his
ranks; but a few well directed discharges of spherical-case from Bean
caused them to beat a precipitate retreat.
The command then pushed on and entered Martinsburg, when
they came suddenly upon a battalion of women, dressed in their
holiday attire, drawn up on the sidewalks, as though bent on
preventing Johnson from taking possession of the town, or at least
their wagon train, ladened with ice cream, confectionery, &c.; for the
fair and unfair dames, damsels and sweethearts of the troopers were
about to celebrate their great national holiday by a picnic, when
surprised by the naughty rebel Johnson, upon whom they at once
opened such a fusillade of invectives in bad and not very choice
English as to compel him and his command to retire in disgust,
leaving them masters of the field.
From Martinsburg Johnson moved to Shepherdstown, and crossed
the Potomac into Maryland, and took position on Catoctin Mountain,
where he encountered a force of the enemy with artillery, but Bean
soon drove them off, when they retired to Frederick city, closely
pursued by Johnson’s cavalry. Here, being reinforced, they made a
stand within the confines of the city, and opened fire from their
battery, protected by the houses. The fire was not returned for some
time, as Johnson was loth to open his guns upon defenceless women
and children, but finally forbearance ceasing to be a virtue, he
opened his battery, and a sharp artillery fight continued until night,
when Johnson retired to the mountain to await Early’s arrival.
Early having at length come up, Johnson with his cavalry, and a
section of the artillery under command of Lieutenant J. McNulty,
proceeded to destroy the railroad bridges at Cockeysville, and this
accomplished, he made a rapid move around Baltimore, and struck
the Washington branch of the Baltimore and Ohio Railroad at
Beltsville, where a large body of the enemy’s cavalry was met, which,
after a few shots from the battery and a charge from the cavalry,
broke and fled towards Washington in the utmost confusion.
The battery, with Johnson’s cavalry, covered Early’s retreat from
Washington, though it was but seldom brought into requisition until
the army reached Poolsville, where the enemy made a vigorous
attack, but were kept in check by Johnson’s cavalry and artillery until
the whole army had crossed in safety.
On the 29th of July General Johnson was ordered by General Early
to accompany McCausland into Pennsylvania and exact a stipulated
sum of money from the citizens of Chambersburg, or in case of their
not complying with that demand to burn the town. The Baltimore
Light Artillery was attached to the brigade, and the whole crossed at
McCoy’s Ferry and proceeded on their way. Before day on the
morning of the 30th the advance approached Chambersburg, and
after feeling the place with a few shells, and finding no enemy, the
town was entered and burned.
Retracing his steps to Virginia by way of Cumberland, McCausland
arrived at that place late in the afternoon of the next day, and found
his situation a critical one. Kelly with a large force was in his front,
strongly posted behind breastworks, and Averill was rapidly coming
up in rear, and to avoid the former he was compelled to move to the
left and take a different road from the one he had intended; but as
there was not a man in his command who knew the country, this was
not to be easily accomplished. After some delay, Col. Harry Gilmor
settled the question by seizing a Union man who was familiar with
the different roads and fords, and with a cocked revolver at his head
compelled him to pilot the way. In the night, four miles from
Cumberland, McCausland’s advance encountered the enemy, who
were, after a brisk skirmish, driven back, when McCausland
determined to await the morning to ascertain his position.
At the dawn of day the enemy was discovered in line behind the
crest of a range of hills between the canal and river, when McNulty
was ordered to post his guns, and open the fight, whilst the cavalry
dismounted and crossed the canal on a bridge hastily constructed by
Captain Welsh of the First Maryland Cavalry, when the enemy
retired. But a more formidable obstacle then presented itself in the
shape of an iron-clad battery mounted on an engine upon the
railroad, whilst the cars to which it was attached were loop-holed for
musketry, and the banks of the railroad, which formed an excellent
breastwork, was lined with infantry. A very strong block house that
commanded every approach to the ford, was also found strongly
garrisoned.
Colonel Harry Gilmor was at once ordered to carry the ford, which
he attempted in most gallant style, but was unable to reach the
opposite bank owing to the dreadful enfilading fire opened upon
him.
Lieutenant McNulty was then directed to take position with his
pieces, and open on the iron-clad. Quickly moving his guns to an
open field, and but two hundred yards from the enemy, he
unlimbered at this much exposed point, and called upon his best
gunner, Geo. McElwee, to bring his piece to bear upon the
formidable looking mass of iron before him. The brave fellow despite
the shower of bullets to which he was exposed, coolly sighted his
piece and fired, and when the smoke cleared away McNulty had the
satisfaction of seeing the huge monster enveloped in steam, for the
shot directed by the unerring aim of McElwee had pierced the boiler,
and it lay a helpless wreck upon the track. His next shot was as
effective, and entered one of the portholes, dismounting the guns
and scattering death and destruction around, when the enemy along
the bank broke and fled.
But there was yet the block house to dispose of before the
command could resume its retreat, and minutes were becoming
precious. An hour was consumed in discussing the matter before
anything definite was determined upon, when General Johnson
suggested that an attempt be made to get a piece of the artillery
across the river. The suggestion was instantly adopted, and under
cover of the bank, though subjected to a severe fire, the piece was
started over in a full run, and unlimbered in the river, and taken to
its bank by hand, when, at the instant, a demand for the surrender of
the block house was complied with, and McCausland was safe.
At Moorefield, soon after, McCausland was surprised in camp by
Averill, when thirteen men of the Baltimore Light Artillery were
sabred and captured at their guns in endeavoring to load them, and
two pieces were lost.
After this affair the remnant of the battery was ordered to the
vicinity of New Market, in the Valley of Virginia, to be reorganized
and equipped. Here the number of the battery was considerably
increased by the transfer of Marylanders who had served three years
in Fort Sumter, and also by men from Major Brethod’s old battery.
Horses and guns were furnished from the reserve artillery, and in a
short time Lieutenant McNulty (who had succeeded to the
command) found himself in a condition to take the field.
Sheridan was about this time rapidly advancing towards Staunton,
and Early, with a small force, had fallen back to Brown’s Gap, in the
Blue Ridge. McNulty was at once ordered to join him at Port
Republic, which he was enabled to do, by making a detour, on the
27th of September. From Port Republic Early moved towards
Waynesboro’, where the head of his column met that of Sheridan,
and after a desperate fight drove him back up the Valley, Early
pursuing.
The scenes which these brave men were compelled to witness as
they pressed on after the brutal Sheridan and his band of
incendiaries were appalling, indeed. Hundreds of poor, helpless
women and children were encountered on the road, fleeing to a place
of refuge, but knew not where to find it, for other homes as well as
their own had been plied with the incendiaries’ torch. Pitiously they
plead for protection and a morsel of food from the rough soldiers,
but, alas, it was not within their power to afford them either. Night
after night the heavens were illumined by the light of burning
farmhouses, barns, mills, &c., and day after day was made dark by
the dense smoke that filled the heavens. Surely, Grant and his
minion Sheridan will be long remembered by the people of the Valley
of Virginia, and their names associated by them and their posterity
with all that is bad, brutal and vindictive.
Slight skirmishing ensued between the pursued and pursuers until
the former reached Fisher’s Hill, where they met their infantry, when
Lomax, in command of the cavalry, retired to the vicinity of
Woodstock. At daylight next morning, the 8th of October, Lomax and
Rosser moved to attack the enemy who had advanced to Maurytown.
Rosser was ordered to attack on the left, and Lomax took the right,
forming his troops on both sides of the Valley pike. The Baltimore
Light Artillery, under Lieutenant McNulty, was stationed on an
eminence north of the town. The fight soon began with great fury,
Lomax and Rosser attacking simultaneously, and the enemy were
driven back some distance. Heavy reinforcements coming to the
support of the enemy, he reformed his broken columns, and the fight
was waged with redoubled fury. Towards noon Rosser, on the left,
was overwhelmed, and soon after the heavy columns massed in front
of Lomax, attacked furiously, and drove that General back in the
utmost confusion.
During this time the gallant McNulty and his brave command were
hurling death and destruction into the ranks of the enemy, but to no
avail. With the retreat of the cavalry under Lomax, McNulty limbered
up and sullenly fell back, unlimbering at every available point, and
opening his fire upon the pursuing foe, thus enabling the cavalry to
escape. In this manner the village of Woodstock was reached. Still
through its streets he continued to pour into the faces of the
advancing enemy destructive discharges of grape and canister. But
the gallant little battery is doomed, for the enemy press upon them in
overwhelming numbers, and still they disdain to abandon their
pieces. Beyond the town they make one more effort to stay the dense
masses now almost enveloping them, but even as the gunners are
ramming home the last double charge of canister, they are captured
and cut down in the act.
Twenty-three men and the four guns fell into the hands of the
enemy. Lieutenant McNulty who had had his horse killed under him,
with the balance of his men fought their way through, and escaped.
For his gallantry upon this occasion, McNulty was placed in
command of a battalion of artillery, and the command of the
Baltimore Light Artillery devolved upon Lieutenant John W.
Goodman, Junior Second Lieutenant.
Soon after the disastrous fight at Maurytown, Goodman was
ordered to Fishersville, where the little left of Early’s artillery were
preparing their winter quarters. Whilst here every effort was made to
procure guns, horses, &c, for the battery, to replace those lost in their
last fight, but without success, for the Confederate Government had
none to spare. But the brave fellows were ready for any duty that
might be assigned them, and when Sheridan, in March, threatened
Lynchburg, they gladly obeyed the summons to repair to that place
and assist in its defence in any capacity. But their services were not
required, and they were in a few days after ordered to Petersburg, to
help man the fortifications there.
When the great crash came, and the little army under General Lee
was forced to retreat before Grant’s overwhelming masses, along
with the rest was to be found the remnant of the Baltimore Light
Artillery—one day fighting as infantry, and the next as cavalry, or
assisting some battery in trouble. Noble fellows, like their comrades
of the Maryland Line, they were true to the cause they had espoused
to the last, and, like the Infantry and Cavalry, were determined to
fight on whilst a ray of hope remained. Alas! that last ray
disappeared with Lee’s surrender at Appomattox Court House, and
when told they were disarmed, and no more to be led against the
enemy, these veterans, who had unhesitatingly faced death in all its
dreadful shapes on so many bloody fields, wept like children. Surely
Maryland should be proud of her “Young Line” in the Confederate
States Army, as she was of her “Old” in the days of the Revolution.

END OF THE “MARYLAND LINE.”


ATKINS’ BRIGADE
AT
SECOND MANASSAS.

T he campaign before Richmond had ended, and McClellan’s


shattered and beaten army was reorganizing upon the banks of
the James. Our loss had also been severe, for the enemy’s artillery
had played with fearful effect upon our dense masses at Malvern
Hill; and we, too, had much to do before again prepared for the field.
But the untiring and indomitable Jackson was soon once more at the
head of his legions, and marching rapidly in search of Pope, whose
advance, under Banks, was reported in the vicinity of Cedar
Mountain.
The First Maryland Regiment of Infantry, under the command of
Colonel Bradley T. Johnson, (and to which I belonged,) having
suffered severely in the many desperate battles of the summer, had
been ordered to Gordonsville to recruit, directly after the battles
around Richmond, and it was whilst there the thunder of Jackson’s
cannon announced that he had once more encountered the
“Commissary of the Valley,” and we did not for a moment doubt the
result.
It was on the evening of the 17th of August, if I mistake not, that a
long line of prisoners arrived from the battle-field, who were turned
over to Colonel Johnson, to be forwarded at once to Richmond. My
company was the fortunate one detailed for the service, and taking a
train about 10 o’clock that night, we reached our destination shortly
after daylight next morning.
After having marched my charge to Libby, and seen them properly
cared for, I strolled up the street in hopes of meeting an
acquaintance or two; but failing in this, and feeling an unpleasant
gnawing at my stomach, I dropped in at Zetelle’s for breakfast.
Whilst waiting for my beefsteak and coffee, and indulging in a glass
of Marco Bruner, I was awakened from a reverie by a heavy footfall
upon the stairway, and to my inexpressible delight the familiar form
of Captain Atkins appeared as the door opened.
“And how the divil are you, Captain; glad to see yez. Arrah! its a
mane thrick the’ve been afther playing us, begorrah!” he exclaimed,
as he took a seat at the table. “Did yez see the morning’s papers? If
yez havn’t, it will be worth your while to get them. Don’t you think
the hathens have disbanded your regiment and my battalion; bad
luck to them.”
I was astounded at the intelligence, and could not credit it for a
moment; but when the papers were produced, there was the order,
sure enough, over the signature of George W. Randolph, Secretary of
War.
“Well, what’s to be done, Atkins? I am just from the front, and feel
convinced the army is on its way into Maryland; and, under the
circumstances, don’t like to leave the field. Come, as my senior,
suggest something.”
“And bless your soul, I will. And now for a little private council of
war betwane ourselves,” he continued, in his usual rollicking way.
“Suppose we form a brigade of our own? I’m sure there’s Grinnell, of
my battalion; Shellman, of your regiment, and whom I have just met;
you and meself; and a better brigade I wouldn’t wish to command, be
me soul I wouldn’t; and, if it plases yourself, we will call it Atkins’
Brigade.”
“Its a bargain; and now, Mr. Brigadier, issue your first order.”
“And here it is: The officers under my command, to be known as
‘Atkins’ Brigade,’ will muster their forces, preparatory to laving for
the scane of action, one week from to-day. How will that do?”
“Very well—all but the forces.”
“Arrah! there you go again. Why in the divil can’t we be officers
and privates too? I am sure there’s many brigades in the sarvice that
can’t muster more an a liftenant.”
“Very well; here’s my hand on it;” and we emptied the bottle of
Rheinish to the success of the new brigade.
But a word of my companions: Atkins was an Irish gentleman of
fortune, but passionately fond of military adventure. He first saw
service in Garibaldi’s Italian war, where he made the acquaintance of
the lamented Major Bob Wheat, and a strong attachment was the
result. At the breaking out of the rebellion, Atkins came to this
country, and hunting up his old companion-in-arms, whom he found
encamped at Manassas, in command of a Louisiana battalion, he
entered the ranks as a private, and at the First Manassas won his
captain’s stripes. He continued to serve in the command with
distinction up to the death of Wheat, and the disbanding of the
battalion as just stated.
Grinnell (a son of Henry Grinnell, one of the merchant princes of
New York) also came from Europe at the breaking out of the war,
sacrificing a lucrative business, and joined the command of Wheat
also, as a first lieutenant. In the fight with Kenly’s forces at Front
Royal, he had the misfortune to lose a portion of his right hand by a
piece of shell, and had just been declared convalescent, and was
ready for the field, but found himself without a command.
Shellman had practiced law in Frederick, Maryland, and served
with great credit as first lieutenant of my company from its
organization.
Never having kept a diary, reader, you will excuse little
discrepancies in dates, but I think it was about the 22d of August that
Atkins marshalled his forces and marched them aboard a James
River canal boat, en route for the anticipated scene of action—I mean
to say we were, and not the canal boat. We were compelled to take
this route owing to the Central Road being taxed to its utmost
capacity in the transportation of troops to reinforce Lee’s army, and
therefore our only alternative was to go by the way of Scottsville,
Charlottesville, to Gordonsville, etc.
Arriving at the former place, we hired a rickety wagon, to which
was attached a wretched specimen of the mule kind, and after a long
and tedious ride made our entre into the ancient village of Charlotte,
not with the “pomp, pride and circumstance of glorious war,” but
amid the curses of an irate driver, and such expressions as “may the
divil take the ass and his master for hathans,” from our illustrious
brigadier.
A day of rest at the excellent hotel de Farish so refreshed us that
when we resumed our trip we felt as though our brigade had
assumed the proportions of a division, and every one of us an officer.
Rapidan Station, the terminus of our trip by rail from Charlottesville,
was reached late in the afternoon, and distributing our traps equally,
we set out on foot to overtake Longstreet’s corps, the rear of General
Lee’s army. Jackson had the advance, and was making a forced
march to get in the rear of Pope’s army, at Manassas. Our march lay
along the Orange and Alexandria Railroad, through Culpepper,
where we diverged to the left and took the road to Salem and the
Plains. Shortly after leaving Culpepper we overtook the stragglers
and broken down men of the army—to be counted by thousands. The
majority of the poor fellows—barefooted and with bleeding feet—
were struggling manfully to reach their respective commands, whilst
others were quietly building shelters and laying in a supply of green
corn, to await, as they coolly informed us, “the return of the army.”
Marching rapidly, on the 28th day of the month we came upon the
rear of Longstreet’s corps in bivouac a mile or two from
Thoroughfare Gap. Heavy cannonading was going on in front, and
upon inquiring the reason for it, we were informed the enemy in
strong force were disputing the pass, but that heavy columns of
infantry had been sent over the mountain to flank them, and it was
not doubted for a moment but the movement would succeed.
A council of war was now called by our commandant, and the
brigade unanimously resolved to go into camp for the night, as
Atkins declared, “Divil the bit could he fight on an impty stomach, at
all, at all.” Selecting a little strip of woods somewhat retired from the
main body of the army, the brigade went supperless to rest.
The sun was at least an hour high when we awoke from our
slumber, to find the corps of Longstreet gone, and already several
miles on its way to relieve Jackson, who, rumor said, had been
closely pressed for two days by overwhelming numbers, and with
difficulty held his own. We arose feeling gloomy enough. The roar of
artillery had ceased, as had the rattle of musketry of the previous
evening, and everything was still as death. Poor, hapless little
brigade, retiring supperless and arising breakfastless. Shellman was
the first to complain, though having the least to sustain. “Who ever
heard of a command without a commissary?” he muttered.
“Arrah, Mr. Shellman, you see I am economizing, for what in the
divil is the use of having a commissary when there’s nothing to
commissary, and the Confideracy is poor enough, God knows, except
in shinplasters. Bad luck to ye.”
The argument was unanswerable, and Shellman subsided, “As we
have no commissary to plase the gintleman—and I’m blasted if I am
much plased meself—I would suggest we went skrimmigen.”
The command responded aye unanimously, and, rolling up our
blankets, we prepared to move. Resuming our march, we soon came
to a neat, substantial looking farm house, where it was determined
an effort should be made to procure breakfast at any price in
Confederate currency. We crossed the fence and approached the
house. Our commander was to be the spokesman, but just as he was
about to apply his knuckles to the stout oaken door, the clatter of
horses feet was heard, and to our no little surprise General Lee and
staff galloped up.
“The brigade will retrate and rally on me, being it’s in the presence
of superior numbers, for by Saint Patrick, for all the Confideracy, I
wouldn’t have Gineral Lee see me besage a lone counthry house
without his orthers,” was the command of our brigadier.
The “retrate” was conducted in good “orther,” and the forces
assembled upon the commandant some two hundred yards distant.
Here, after a short consultation, we concluded to make another essay
upon a house close at hand. As we neared the premises the prospect
of success seemed discouraging enough, for some twenty cavalry
horses were tied to the pailings, and experience had taught us their
riders were close by. Judge then of my surprise, when upon inquiring
for the proprietress of the house I was confronted by an old and dear
acquaintance, Mrs. Hamilton, of Loudon county, and now, at this
writing, residing at Chantilly, on the Little River turnpike. ’Tis
needless to say the party were made welcome, and in a few minutes
we were seated around a bountifully supplied table, laughing and
chatting merrily, the vexation of our former disappointment
forgotten. A quiet smoke followed, when we prepared to part with
this most estimable family with unfeigned regret. The dear old lady
of the house, with tears in her eyes, bid us farewell, and her last
words were, “Should anything happen to yourself or comrades, don’t
forget my house.” I little thought but forty-eight hours would elapse
ere I was once more her guest, but under vastly different
circumstances. But of that anon.
As we passed through Thoroughfare Gap there were evidences on
all sides of a stubborn fight, and I could not help being struck with
the vital importance of possessing the place. It seemed almost a
Thermopylæ, and it astonished me that it was not defended by a
larger force of the enemy. Had General Lee been delayed here forty-
eight hours we would have lost Jackson and his command, and the
remainder of the army would have been compelled to recross the
Rappahannock. How imminent the peril! But the masterly
generalship of the peerless Lee averted it, and the flanking columns
across the mountains soon removed every obstacle. But no time was
to be lost, for already we had been delayed too long; and the
incessant peals of artillery in our front plainly told us Jackson was
sorely pressed, and needed our assistance.
How little the General whose “headquarters were in the saddle,”
knew his advantage; for he had it within his power to have
annihilated the Confederate army. But if we are to believe his own
report, and the account of his “Man Friday,” (“Personal recollection,”
in that vilest of periodicals—Harper,) the illustrious Pope had too
many generals in the field, and too many masters in Washington.
The “lamented” would telegraph “do this;” Halleck would say “do
that;” Fitz John Porter would do nothing; McClellan sent the
commissary wagons to Washington, and it was but natural the
hungry troops should seek their rations there—all of which,
combined “unsaddled” poor Pope, and placed McClellan in his stead.
More than once that night, as General Lee paced his room, he was
heard to mutter: “The Gap must be forced at any sacrifice. On the
success of this movement depends everything. General Jackson must
be hard pressed; but he will hold out, or I do not know my man.
Twenty-four hours behind my promise to him; forty-eight hours
would result in his destruction.”
Friday, the 29th day of August, 1862, was a hot, sultry day, and the
corps of Longstreet, already wearied and broken down by excessive
marching, dragged itself along with difficulty. Had it not been for the
stimulus afforded by the roar of Jackson’s artillery the ten long miles
from Thoroughfare Gap would have consumed much more time, and
there would have been many more stragglers. But steadily those
veterans pressed on, and before noon the head of the column
reached Haymarket, but a short distance from the scene of conflict.
We here received the most encouraging accounts from Jackson. He
was not only holding his ground, but had driven the enemy some
distance before him. But nevertheless an expression of relief must
have passed over that usually stern and placid face when he observed
the clouds of dust that heralded the approach of the iron Longstreet.
Rapidly the different brigades and divisions were thrown into
position, and by three o’clock the line of battle was complete.
Longstreet held the right, joining Jackson near the Groveton
turnpike, and about a mile from that village, where was stationed
Hood with his Texans, Virginians and Mississippians. Being
personally acquainted with the General, I suggested to Atkins that he
proffer the services of the “brigade” to him, which was readily
acceded to. That gallant, glorious, but subsequently unfortunate
officer, received us most cordially, but regretted that for the present
he could assign us to no other duty than that of firing a musket.
Enfields were soon procured, and we took our place in the ranks of
the Fourth Texas, a regiment that at the battle of Gaine’s Mills had
won imperishable laurels.
In the woods to the left Jackson was hotly engaged. The musketry
fire was terrific, and it seemed neither to advance nor recede. A
curtain of smoke that hung over the tree-tops told us where the
fighting was going on, but not a soul could be seen. For more than
two hours this desperate struggle continued, when a soldier or two in
blue emerged from the woods into the open plain, rushing frantically
in the direction of the Federal reserves. Another and another
followed, and presently a heavily column, torn and shattered, broke
forth from the woods in wild confusion. A moment after, in close
pursuit, a column in grey appeared, moving at the double-quick with
the most beautiful precision, though subjected to the fire of a battery
of artillery, which opened on them as soon as they appeared. But the
glorious column never wavered. On, on, they pressed. The enemy
was seeking protection behind the battery. It mattered not, they
would take battery and infantry too; all the better. Oh! how it made
my heart ache as I saw the great gaps rent by grape and canister as
they pressed on in this desperate charge. But “forward” was the
command, and “forward” they went. The battery is neared; no
hesitation there; and in an instant it is enveloped by our grey jackets.
A yell, a wild Confederate yell, announced the success of the charge,
and in a moment we saw them retire with hundreds of prisoners and
the captured battery.
“Glorious, glorious,” exclaimed the heroic Hood, at whose side I
was standing, and who was most anxiously watching the charge.
“Who can it be?”
An aid-de-camp presently appeared riding at full speed in the
direction of General Lee’s headquarters. Hood hailed him with,
“Who made that splendid charge?”
“Bradley Johnson, in command of the Third Virginia brigade,” was
the response.
How my heart thrilled with pride and pleasure as I heard the
announcement; for it was my late Colonel, fighting like the members
of Atkins’ brigade, without a commission.
But what means this demonstration in our immediate front? Cheer
after cheer, huzza upon huzza, and presently a long line of
skirmishers appear over the intervening hill. An attack beyond
question. They are met unhesitatingly by our skirmishers, and a
fierce fight ensues. But the dense, dark column that now comes in
view compels our advance to fall back. But Hood, with his Texans,
Virginians and Mississippians, are in waiting. Rapidly they approach,
and musket balls are whistling in every direction.
“Let them come a little closer,” I heard him remark to a staff
officer, “they shall have a reception.”
Just then a voice behind us was heard to command, “Charge,
General, charge!”
Judge my feelings as I turned my head and saw our beloved Lee
sitting calmly upon his iron-grey, with but a single aid, and exposed
to this severe fire.
But we had little time for reflection, for the clarion voice of Hood
was heard the next instant to “fix bayonets,” and then came the
command “forward, double-quick,” and the long line of glittering
steel rushed madly down the hill.
The collision was awful, and the enemy was unable to resist its
force, though he stood to his work manfully. Gallant men fell by
hundreds on both sides. The Federal troops gave way; and the field
was covered with thousands of fugitives. The column in grey pressed
on until it neared the village of Groveton. What is this dark mass
seen in the twilight thundering down upon us, making the very earth
tremble? The column halts. On come the gallant troopers. The sight
is grand beyond description. Nearer and nearer they approach. They
are almost upon us! “Fire,” is the command, and the heavens are lit
up by the flash that leaps along the line; and when the smoke lifts,
hundreds of riderless steeds are seen flying frantically across the
plain, whilst the ground in front of us is covered with dead and
wounded men and horses.
In the excitement of the fight I had lost sight of my companions,
and it was not until I heard a voice with the Celtic twang exclaiming,
“Catch a harse, catch a harse,” that I was aware of the presence of
Atkins. “Catch a harse, catch a harse,” I heard again, as a dozen
riderless steeds dashed up the turnpike, “for I’m blasted if my
command shall be infantry any longer.” Turning my head I saw him
making frantic efforts to secure one of the frightened horses and his
efforts were successful. I was not so fortunate. Seizing a bridle, I was
leading my capture in triumph across the road, when giving a sudden
plunge, he wrenched the rein from my hand and was off like the
wind.
Leaving my companion in ecstacies over his prize, I started after
something to eat. But where was I to get it? The wagons were far in
the rear, and being tired, I could not think of hunting them up. A
happy thought suggested itself. The Federal soldiers generally
carried a day or two’s rations. I will secure what some dead man has
left. The idea may shock the sensibilities of the reader with a well-
filled stomach, but a tired and hungry soldier is not much troubled
with such wares. Crossing the Groveton road I threaded my way
among the dead and wounded with a melancholy interest. A few
minutes before the air had resounded with the shouts of infuriated
men, the roar of artillery, and the rattle of small arms; but now all
was still, save an occasional picket shot, and the groans and cries of
those who had fallen wounded in the bloody strife.
More than one dead man’s haversack did I examine that night; but
alas, some one as hungry as myself had anticipated me. I had given
the search up in despair, and was returning to my companion, when
I stumbled over the body of a Federal soldier that I did not see,

You might also like