Matlab; Computational Methods In Physics
Queens College, CUNY Physics Department
Physics 661 – Computational Methods in Physics Spring 2019 – Dr. David Goldberg
Page 1/5
HOMEWORK ASSIGNMENT 1
Vectors and Matrices – Back to Basics MATLAB has many ways to deal with vectors and matrices very efficiently. However, in order to practice foundational skills, you will write your own script to accomplish some of these basic tasks.
Vector Algebra Due Monday, 2/4/18, before 4PM
Magnitude of an n-Dimensional Vector Given an n-dimensional vector:
�⃗�𝑣 = [𝑣𝑣1, 𝑣𝑣2, 𝑣𝑣3, … , 𝑣𝑣𝑛𝑛−1, 𝑣𝑣𝑛𝑛] The magnitude is defined as:
|�⃗�𝑣| = �𝑣𝑣1 2 + 𝑣𝑣2
2 + 𝑣𝑣3 3 + ⋯ + 𝑣𝑣𝑛𝑛−1
2 + 𝑣𝑣𝑛𝑛2
Write a script that defines a 1 × 𝑛𝑛 vector, and computes it’s magnitude. The framework of the script can be as follows:
1 clear 2 3 % define vector 4 v = [1, 2, 3, 4, 5]; 5 6 fill in code here that computes the magnitude 7 8 % display result 9 disp(['Magnitude of v, |v| = ', num2str(magV)])
Note that the code you write in after line 4 should automatically adjust for vectors of different dimension. If you were to add an additional element to the vector in line 4, the rest of the script should not need any modification in order to run properly. Test your script for different vectors and with other dimensions.
Scalar Product of n-Dimensional Vectors Given two n-dimensional vectors, �⃗�𝑎, and 𝑏𝑏�⃗ , where:
�⃗�𝑎 = [𝑎𝑎1, 𝑎𝑎2, … , 𝑎𝑎𝑛𝑛] 𝑏𝑏 = [𝑏𝑏1, 𝑏𝑏2, … , 𝑏𝑏𝑛𝑛]
The scalar product, �⃗�𝑎 ⋅ 𝑏𝑏�⃗ is defined as:
�⃗�𝑎 ⋅ 𝑏𝑏�⃗ = 𝑎𝑎1𝑏𝑏1 + 𝑎𝑎2𝑏𝑏2 + ⋯ + 𝑎𝑎𝑛𝑛𝑏𝑏𝑛𝑛 Write a script that computes the scalar product of two vectors. The outline can be as follows:
Physics 661 Spring 2019
Dr. David Goldberg
Page 2/5
1 clear 2 3 % define vectors 4 a = [1, 2, 3, 4, 5]; 5 b = [6, 7, 8, 9, 10]; 6 7 fill in code here that computes the scalar product 8 9 % display result 10 disp(['Scalar product a.b = ', num2str(sProd)])
Again, note that the code you write in after line 5 should automatically adjust for vectors of different dimension. If you were to add an additional element to the vector in line 4 & 5, the rest of the script should not need any modification in order to run properly. Test your script for different vectors and of other dimensions.
Matrix Algebra Due Wednesday, 2/6/18, before 4PM
Part I: y = Ax Suppose vector 𝐱𝐱 is of dimension 𝑛𝑛 × 1 and matrix 𝐀𝐀 is of dimension 𝑚𝑚 × 𝑛𝑛:
𝐱𝐱 = �
𝑥𝑥1 𝑥𝑥2 ⋮ 𝑥𝑥𝑛𝑛
� ; 𝐀𝐀 = � 𝑎𝑎11 ⋯ 𝑎𝑎1𝑛𝑛 ⋮ ⋱ ⋮
𝑎𝑎𝑚𝑚1 ⋯ 𝑎𝑎𝑚𝑚𝑛𝑛 �
The product of 𝐱𝐱 and 𝐀𝐀 is defined as:
𝐲𝐲 = 𝐀𝐀𝐱𝐱 = � 𝑎𝑎11 ⋯ 𝑎𝑎1𝑛𝑛 ⋮ ⋱ ⋮
𝑎𝑎𝑚𝑚1 ⋯ 𝑎𝑎𝑚𝑚𝑛𝑛 �� 𝑥𝑥1 ⋮ 𝑥𝑥𝑛𝑛 � = �
𝑎𝑎11𝑥𝑥1 + 𝑎𝑎12𝑥𝑥2 + ⋯ + 𝑎𝑎1𝑛𝑛𝑥𝑥𝑛𝑛 ⋮
𝑎𝑎𝑚𝑚1𝑥𝑥1 + 𝑎𝑎𝑚𝑚2𝑥𝑥2 + ⋯ + 𝑎𝑎𝑚𝑚𝑛𝑛𝑥𝑥𝑛𝑛 � = �
𝑦𝑦1 ⋮ 𝑦𝑦𝑚𝑚 �
Where 𝐲𝐲 is of dimension 𝑚𝑚 × 1. A generic element in 𝐲𝐲 is defined by:
𝑦𝑦𝑖𝑖 = �𝑎𝑎𝑖𝑖𝑖𝑖𝑥𝑥𝑖𝑖
𝑛𝑛
𝑖𝑖=1
for 𝑖𝑖 = 1, 2, . . . , 𝑚𝑚
Write a script where given 𝐱𝐱 and 𝐀𝐀, computes 𝐲𝐲.
Part II: 𝐶𝐶 = 𝐴𝐴 × 𝐵𝐵 Suppose matrix 𝐀𝐀 is of dimension 𝑚𝑚 × 𝑝𝑝 and matrix 𝐁𝐁 is of dimension 𝑝𝑝 × 𝑛𝑛. The product of A and B, 𝐀𝐀 × 𝐁𝐁 is defined as:
𝐂𝐂 = 𝐀𝐀 × 𝐁𝐁 = � 𝑎𝑎11 ⋯ 𝑎𝑎1𝑝𝑝 ⋮ ⋱ ⋮
𝑎𝑎𝑚𝑚1 ⋯ 𝑎𝑎𝑚𝑚𝑝𝑝 �� 𝑏𝑏11 ⋯ 𝑎𝑎1𝑛𝑛 ⋮ ⋱ ⋮ 𝑎𝑎𝑝𝑝1 ⋯ 𝑏𝑏𝑝𝑝𝑛𝑛
� =
⎣ ⎢ ⎢ ⎢ ⎢ ⎢ ⎡�𝑎𝑎1𝑖𝑖𝑏𝑏𝑖𝑖1
𝑝𝑝
𝑖𝑖=1
⋯ �𝑎𝑎1𝑖𝑖𝑏𝑏𝑖𝑖𝑛𝑛
𝑝𝑝
𝑖𝑖=1 ⋮ ⋱ ⋮
�𝑎𝑎𝑚𝑚𝑖𝑖𝑏𝑏𝑖𝑖1
𝑝𝑝
𝑖𝑖=1
⋯ �𝑎𝑎𝑚𝑚𝑖𝑖𝑏𝑏𝑖𝑖𝑛𝑛
𝑝𝑝
𝑖𝑖=1 ⎦ ⎥ ⎥ ⎥ ⎥ ⎥ ⎤
= � 𝑐𝑐11 ⋯ 𝑐𝑐1𝑚𝑚 ⋮ ⋱ ⋮
𝑐𝑐𝑚𝑚1 ⋯ 𝑐𝑐𝑚𝑚𝑛𝑛 �
Physics 661 Spring 2019
Dr. David Goldberg
Page 3/5
Where 𝐲𝐲 is of dimension 𝑚𝑚 × 1. A generic element in 𝑦𝑦 is defined by:
𝑐𝑐𝑖𝑖𝑖𝑖 = � 𝑎𝑎𝑖𝑖𝑖𝑖𝑏𝑏𝑖𝑖𝑖𝑖
𝑝𝑝
𝑖𝑖=1
for 𝑖𝑖 = 1, 2, . . . , 𝑚𝑚 𝑗𝑗 = 1, 2, . . . , 𝑚𝑚
Write a script where given 𝐀𝐀 and 𝐁𝐁, computes 𝐂𝐂.
Rolling Dice – Winner Winner, Chicken Dinner Rolling a Single Dice
Due: Monday, 2/4/18, before 4PM In a game of standard six-sided dice, the sides are labeled 1, 2…,6. Modified the script we wrote in class for the fair two-sided coin so that it simulates a fair six-sided dice. Below is an example of what the output should look like:
How many rolls are needed until the distributions begins to consistently look uniform? 10, 102, 103, or more?
Rolling Two Dice Due: Wednesday, 2/6/18, before 4PM
Suppose you roll two fair dice at the same time. The list of possible outcomes on a single roll of two dice is shown below where, (𝑖𝑖, 𝑗𝑗) denotes 𝑖𝑖, on dice 1, and 𝑗𝑗, on dice 2:
(1,1) (1,2) (1,3) (1,4) (1,5) (1,6)
(2,1) (2,2) (2,3) (2,4) (2,5) (2,6)
(3,1) (3,2) (3,3) (3,4) (3,5) (3,6)
(4,1) (4,2) (4,3) (4,4) (4,5) (4,6)
(5,1) (5,2) (5,3) (5,4) (5,5) (5,6)
(6,1) (6,2) (6,3) (6,4) (6,5) (6,6) Table 1: All possible outcomes of a two-dice roll. The highlighted diagonals show pairs of the same sum.
Physics 661 Spring 2019
Dr. David Goldberg
Page 4/5
Part 1 • You are to write a MATLAB script that rolls two dice, and computes the sum of the numbers that
appear on the top-face of the dice. • Repeat this for 𝑁𝑁 rolls and plot in bar plot the frequency of each sum, for:
𝑁𝑁 = 10, 50, 100, 1000, 104, 105, 106. If you are confident to try 𝑁𝑁 = 107 and beyond, you may but it will take a time to run depending of the efficiency of the code and the speed of your computer.
• Make sure your graphs are properly annotated with meaningful titles and labels.
Your results should be similar to those shown in the graphs below. The solid circles represent the “expected outcome” based on probability. The circles were added by using the “hold on” command after plotting the bar graph. Don’t forget to use the “hold off” command after you’re finished adding the plot.
Part 2 Explore how the statistics change if:
• One of the dice is unfair. • Both dice are unfair, but not necessarily weighted to the same side.
What changes do you observe for these scenarios?
- Vectors and Matrices – Back to Basics
- Vector Algebra Due Monday, 2/4/18, before 4PM
- Magnitude of an n-Dimensional Vector
- Scalar Product of n-Dimensional Vectors
- Matrix Algebra Due Wednesday, 2/6/18, before 4PM
- Part I: y=Ax
- Part II: 𝐶=𝐴×𝐵
- Rolling Dice – Winner Winner, Chicken Dinner
- Rolling a Single Dice Due: Monday, 2/4/18, before 4PM
- Rolling Two Dice Due: Wednesday, 2/6/18, before 4PM
- Part 1
- Part 2