Name:_
Student #:_
York University
Department of Electrical Engineering & Computer Science
Lassonde School of Engineering
EECS 1541: Introduction to Computing for the Physical Sciences
Test 2 (Solutions)
April 2nd, 2015
Instructor: John Lam
Instructions:

Name:_
Student #:_
York University
Department of Electrical Engineering & Computer Science
Lassonde School of Engineering
EECS 1541: Introduction to Computing for the Physical Sciences
Test 1 (Solutions)
February 10th, 2015
Instructor: John Lam
Instructio

Ordinary differential equations
1
"Simple" pendulum
the simple pendulum is a
standard topic in most
introductory physics courses
the tangential component
of the net force acting on the
pendulum is
= = sin
= sin
2
http:/commons.wikimedia.org/wiki/Fil

Numerical integration
1
Numerical integration
numerical integration attempts to estimate the value
of a definite integral without solving for the indefinite
integral; i.e.,
=
estimate the value of
=
without solving for
recall the first fundamental the

2/5/15 7:13 PM
C:\Us.\1541_test1_assignment1_solutions.m
% CH. 3 quesitons from the textbook,
% solutions for #5,6,7,10,12,24,29,30,31,37
% #5.
x = input('Enter a real number: ');
fprintf('The number is: %.2f\n',x)
% #6.
x = 12345.6789;
fprintf('The real

Ordinary differential equations
1
Example problem
consider the no-drag projectile motion problem
0 initial velocity
0 , 0
2
initial position
Example problem
the solution is known to all physics students
= 0 cos
= 0 sin 1 2
2
0 initial velocity
0 , 0 =

% Ch. 5 exercises (5, 7, 13, 18, 23, 28, 29, 30, 36, 40)
% Question 5
x = input('Enter an interger n: ');
for index = 1:x
disp('I love this stuff!');
end
% Question 7
function [y] = sumsteps2(x)
total = 0;
for index = 1:2:x
total = total + index;
end
y =

% Ch. 4 exercises (6, 7, 8, 10, 12, 13, 18, 22) %
% Question 6
a = input('Enter the value of "a":');
b = input('Enter the value of "b":');
if a =0
disp('Error! "a" cannot be equal to zero');
else
y = eccentricity(a,b);
fprintf('The result is 0.2\n',y);
en

Course Description
This course introduces students to computer-based problem solving techniques that can be used to approach
problems in the physical sciences, such as answering questions that require numerical computation, as well as
basic analysis of ex

% Lab 1
% Question 1
% dimensions in feet
ht = 4;
len = 8;
wid = 4;
% meters per foot
mPerFt = 0.0254 * 12;
% volume in cubic meters
volume = ht * len * wid * mPerFt ^ 3
%
Question 2a
% from the diagram we have
%
tan(theta) = 1 AU / x
% where x is the num

Representing numbers
and Basic MATLAB
1
Representing numbers
numbers used by computers do not behave the same as
numbers used in mathematics
e.g., try the following in MATLAB:
help intmax
x = intmax;
x + 1
2
Representing numbers
numbers used by computers

You have 2 hours to complete this test.
There are 5 multi-part questions in this test. Create a script named test1.m to answer the questions.
For each part of each question, create a new cell (section) in your script. You do not need to publish a report
f

Logicals
1
Logicals
a logical expression is an expression that evaluates to
either true or false
a logical variable is a variable whose value is either true
or false
logical variables are usually called Boolean variables in
computer science
2
Logicals in

% Lab 2 Sinusoidal waves
% This lab uses MATLAB to explore sinusoidal waves.
% Amplitude
% Plot three sinusoidal waves each having a different amplitude
% for time t from -3 seconds to 3 seconds in steps of 0.01 seconds
t = -3:0.01:3;
A1 = 0.5;
A2 = 1;
A3

% Lab 3
V = [-1 -1;
1 -1;
1 1;
-1 1];
F = [1 2 3;
1 3 4];
C = [1 0 0;
1 0 0];
% Question 1 Translations in 2D commute
% Translating an object by a translation d1 followed by a translation
% d2 yields the same results as translating by d2 followed by d1.
%

Logical indexing
you can use a logical array to perform indexing on
another array
MATLAB extracts the array elements corresponding to the
nonzero values in the logical array
1
the output is always in the form of a column vector unless the
array is a vecto

Vectors and Matrices II
1
Matrices
a maxtrix is a 2-dimensional array where the size of the
dimensions is usually larger than 1
2x3
2
5x6
Creating matrices
a matrix of size m x n can be created by entering m row
vectors of length n separated by semi-colon

Numerical differentiation
1
Announcement
there is a script and some functions available for this
lecture on the course web site
2
Numerical differentiation
numerical differentiation attempts to estimate the
value of the derivative of a function without
co

Matrix operations
Scripts
1
Matrix transpose
if A is an m x n matrix then the transpose of A is an
n x m matrix where the row vectors of A are written as
column vectors
2
> u = [1 2 3];
> v = u'
v =
1
2
3
> A = [1 2 3;
4 5 6];
> B = A'
B =
1
4
2
5
3
6
3
m

Scripts
1
MATLAB Scripts
a script is text file containing a sequence of MATLAB
commands
each command usually occurs on a separate line of the file
MATLAB can run the commands in a script by reading
the file and interpreting the text as MATLAB
commands
2
c

Computing for the Physical
Sciences
CSE1541M
1
Who Am I?
Dr. Burton Ma
office
Lassonde 2046
hours : see syllabus on course web page
email
2
burton@cse.yorku.ca
Course Format
everything you need to know is on the course website
http:/www.eecs.yorku.ca/cour

Random variables
1
Note
there is no chapter in the textbook that corresponds to
this topic
2
Computing random numbers
the ability to quickly generate random numbers has
many very useful applications
Monte Carlo methods
Monte Carlo simulations
statistical

Basic statistics (continued)
1
Interquartile range
like the mean, the standard deviation and variance are
sensitive to erroneous measurements
the interquartile range is a more robust measure of
dispersion or spread
quartiles
interquartile range = 3 1
2
th

User-defined functions
1
Recall the script example from Day 5
an undamped spring-mass system is an example of a
simple harmonic oscillator
the position of the mass is given by
= sin
2
2
3
User-defined functions
scripts are useful but:
the variables used

You have 2 hours to complete this test.
There are 5 multi-part questions in this test. Create a script named test1.m to answer the questions.
For each part of each question, create a new cell (section) in your script. You do not need to publish a report
f

1
Textbook exercises
The textbook has many programming exercises suitable for reviewing for Test 2:
Chapter 5 17, 18, 19, 22, 24, 26, 28, 29, 30, 32, 33, 36, 40, 45
Chapter 12 1, 3, 7, 8 (try to generalize your function so that it can compute a moving ave

Basic MATLAB continued;
Using functions
1
Variable names
valid variable
names
x
$
does not begin with a letter
$ is not allowed in variable names
x6
6x
does not begin with a letter
lastValue
if
if is a keyword
pi_over_2
2
invalid variable
names
reason