Physics 234: Exercise 2
Imagine a hypothetical non-binary computer whose oating-point numbers are encoded using
one sign bit and seven decimal digits. Each number is stored internally in the format
i
f4
f3
f2
f1
f0
e
and interpreted as the value (, i, f,
Solving non-linear system of equations
It is easy to find the numerical solution to a linear system:
Ax=b
numerical solution: > x = A \ b
But how do we solve problems for which the elements of matrix A depend on the
values of vector x?
Example: what are t
PHYS234, thursday Midterm 1.
Name:
ID:
Question 1:
The conversion between a length xm in meters and a length xf in feet is
xm = 0.3048 xf
Write two separate function m-files, one to convert a measurement in feet to meters (feet2m) and one to do
the revers
Solving linear system of equations
The goal in this section is to find the value of a column vector x (with n elements)
such that it is the solution to the linear system of equations represented in the form:
Ax=b
formal solution: x = (A)
1
b
where
A is a
LOOPS
Loops are at the heart of computing. They are used to repeat
operations many times, and to set conditional executions of
statements. Computer algoriths all involve loops.
There are 3 kinds of loops:
1) IF - loops:
Used for comparison between variabl
Numerical Integration of Ordinary Differential
Equations (ODE)
The goal in this section is to integrate an ordinary differential equation in time.
dy
= f (t, y)
dt
t independent variable
(1)
y dependent variable
f right-hand side (equivalent to the slope
m-files and Programing
Script Files
Not really programs
No input/output parameters
Script variables are part of workspace
Useful for tasks that never change
Advice: Scripts oer no advantage over functions.
Functions have many advantages over scripts.
Solutions to an overdetermined problem
The goal in this section is to find a solution when we have more equations than
unknowns. A classic example of this occurs when we try to fit a simple function to
a number of data points. Suppose we have m data point
Finding the roots of f (x)
The goal in this section is to find the value of x for which f (x) = 0
Example 1
f (x) = 2x + 1 = 0
solution: x =
1
2
Example 2
2
f (x) = x + 1 = 0
solutions: x1 = 1, x2 = 1
Example 3
f (x) =
x cos(x) 1 = 0
solutions: ?
In t
Unavoidable errors in computing
Digital representation of numbers
limit in size
limit in resolution
Floating point arithmetic
roundoff error
machine precision
Truncation errors
Digital representation of numbers
Numbers are stored in the computer as
Numerical Integration
Question: what is:
Z
b
f (x)dx =?
a
Answer: Equivalent to area under the curve.
Numerical Integration
In calculus, we approximate the area as a sum of rectangles:
Define n number of points: x1, x2, x3, . . . , xn1, xn
Set x1 = a , xn
function ps1q4
%
ID: 1364035, Craig Yanitski
%
The solution to question 4 of the first problem set
x = linspace (0, 5, 100);
%Initialize a vector with the x values
a = 0.1:0.2:1.1;
%Initialize a vector with the constant
values
Table = [x',(erf(x*a(1)', (e
Physics 234: Solutions to Practice Exam Questions
1.
2. A polynomial with roots at 1, 2, and 3 can be factored as p(x) (x + 1)(x 2)(x 3). The
requirement that p(0) = 1 xes the prefactor:
1
1
p(x) = (x + 1)(x 2)(x 3) = x3 4x2 + x + 6 .
6
6
3. The absolute
Physics 234: Computational Physics
Final Exam
Friday, April 17, 2009 / 14300717300 / V103
Students Name: Kev ll BQAQlA
Instructions
There are ten questions. You should attempt all of them. Mark your response on the test paper
in the space provided. F
Physics 234: Solutions to Practice Exam Questions
1.
2. A polynomial with roots at 1, 2, and 3 can be factored as p(x) (x + 1)(x 2)(x 3). The
requirement that p(0) = 1 xes the prefactor:
1
1
p(x) = (x + 1)(x 2)(x 3) = x3 4x2 + x + 6 .
6
6
3.
4. Suppose f
Solutions to practice test 1
1(b) The number 255 = 111111112 is the largest that can be represented in an 8-bit unsigned
binary system. Hence, 255 + 1 overows to give 000000002 = 0.
2(a) Since its second argument is passed by value, foo(4,z) does nothing
Physics 234: Computational Physics
In-class Midterm Exam
Wednesday, February 11, 2009
Students Name:
Fill-in-the-blank and multiple choice questions (20 points)
Mark your answers on the exam sheet in blue or black ink. Please be clear
about your selection
Physics 234: Exercise 1 Solutions
1. (a) Take the complement of each bit (i.e., exchange 0 1) and add 1. In this way 3 goes to 3
as follows: 00000011 11111100 + 1 = 11111101.
(b) The most negative representable number 2N 1 is its own twos complement. When
Physics 234: Computational Physics
In-class Midterm Exam
Friday, February 12, 2010
Students Name:
Fill-in-the-blank and multiple choice questions (20 points)
Mark your answers on the exam sheet in blue or black ink. Please be clear
about your selections.
1(b) The number 255 = 111111112 is the largest that can be represented in an 8-bit unsigned
binary system. Hence, 255 + 1 overows to give 000000002 = 0.
2(a) Since its second argument is passed by value, foo(4,z) does nothing to the value of z.
bar(2.3,z)
Physics 234: Lab Test
Tuesday, April 6, 2010
Students Name:
UserID: p234u
1. Consider the recursive sequence dened by x0 = 2 and xn+1 := 2 + 2/xn . Its asymptotic value
X = limn xn is equal to the innite continued fraction
2
X =2+
2
2+
2
2+
2+
2
2 +
Repo
Physics 234: Lab Test
Thursday, April 8, 2010
Students Name:
UserID: p234u
1. Consider the recursive sequence dened by x0 = 3 and xn+1 := 3 + 3/xn . Its asymptotic value
X = limn xn is equal to the innite continued fraction
3
X =3+
3
3+
3
3+
3+
3
3 +
Rep
Physics 234: Practice Lab Test
1. How many of the numbers 1, 2, . . . , 1000 are perfect squares?
31
How many are perfect cubes?
10
(Hint: you can solve this question without using either sqrt or pow.)
2. Consider an unbounded square grid of points spaced
Physics 234: Lab Test
Tuesday, March 31, 2009 / Thursday, April 2, 2009
Students Name:
UserID: p234u
1. Consider the nite sequence of numbers
S = (n2 + 3n5 )65 = (4, 100, 738, . . . , 3480876100).
n=1
How many of the numbers in S are divisible by 12? (Hin
Interpolation
Suppose we have a set of m data or function points (xi, yi), or (xi, F (xi).
Goal: Find F (
x) when x
is not one of data points
Condition: if x
= xi, we recover F (
x) = F (xi) = yi.
Options:
1. Polynomial fit for the whole domain.
2. Piec