0% found this document useful (0 votes)
8 views81 pages

Introduction to Computational Physics

Computational physics integrates physics, mathematics, and computer science to solve complex problems using computational power. It finds applications in various fields such as astrophysics, materials science, and medical imaging, utilizing simulations, data analysis, and numerical methods. The document also covers the fundamentals of computation, programming languages, and data visualization techniques, particularly using Python.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views81 pages

Introduction to Computational Physics

Computational physics integrates physics, mathematics, and computer science to solve complex problems using computational power. It finds applications in various fields such as astrophysics, materials science, and medical imaging, utilizing simulations, data analysis, and numerical methods. The document also covers the fundamentals of computation, programming languages, and data visualization techniques, particularly using Python.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Computational Physics

Dr. Ashok Kumar Baral

Department of Physics
VIT Bhopal University
Computational Physics
Computational physics is a combination branch of physics, mathematics,
and computer science where complex problems of physics are solved very
subtly with the help of computational power.

The thinking ability power of


the human mind is much higher
but the power of calculation is
much less. But in the case of
computers, the ability to
perform fine calculations in a
very short period of time is
much higher than that of the
human mind.
Computational Physics
Use of computational physics: (1) Astrophysics and astronomy, (2)
Computational plasma physics, (3) High energy physics, (4) Atomic and
nuclear physics, (5) Applied mathematical physics, 6. Materials Sciences, 7.
Atmospheric Science and so on.
Some Key Areas of Applications:
Simulation of physical systems: Modeling real-world
systems like fluids, planetary motion, and atomic structures.
Data Analysis: Processing and interpreting large data sets
from experiments or simulations.
Numerical Methods: Solving differential equations, linear
algebra problems, and integral equations.
Visualization: Representing results graphically for better
understanding (e.g., graphs, simulations, and 3D models).
Real-Life Examples:
•Weather Forecasting: Computers simulate how air, wind, and heat will
move to predict tomorrow's weather.
•Medical Imaging: Computational physics helps doctors see inside the
body using MRI or CT scans.
•Movie Special Effects: Simulating explosions, water, or fire in movies
involves physics calculations done by computers.
Examples of Uses of Computation in Materials Sciences
Computation in weather forecast
Computation in Astrophysics
Fundamentals of Computations
A computer is an electronic device that accepts data, performs operations, displays results,
and stores the data or results as needed. It is a combination of hardware and
software resources that integrate together and provide various functionalities to the user.

Hardware: It is the physical components


of a computer like a processor, memory
devices, monitor, keyboard, etc.
Software: It is a set of programs or
instructions that are required by the hardware
resources to function properly.
Fundamentals of Computations
There are basically three important components of a computer:
[Link] Unit
[Link] Processing Unit(CPU)
[Link] Unit

Input
The input unit consists of input devices
that are attached to the computer.
These devices take input and convert it
into binary language that the computer
understands. Some of the common
input devices are keyboard,
mouse, and scanner.
Fundamentals of Computations
Once the information is entered into the computer by the input device, the processor
processes it. The CPU is called the brain of the computer. It first fetches instructions
from memory and then interprets them so as to know what is to be done. If required,
data is fetched from memory or input device. Thereafter CPU executes or performs the
required computation, and then either stores the output or displays it on the output
device.
CPU has three main components: Arithmetic Logic Unit (ALU), Control Unit (CU),
and Memory registers (unit)

Arithmetic Logic Unit (ALU): It performs mathematical calculation (addition,


subtraction, multiplication, and division) and takes logical decisions.

1. ALU is the main component of the CPU


2. ALU is a digital circuit that is used to perform arithmetic and logical operations.
Fundamentals of Computations
Control Unit: The control unit coordinates and controls the data flow in and out of the
CPU. It also controls all the operations of the ALU, memory unit, and also input/output
units.
1. It is also responsible for carrying out all the instructions stored in the program.
2. It decodes the fetched instruction, interprets it, and sends control signals to
input/output devices until the required operation is done properly by ALU and
memory.

Memory or Storage Unit: A register is a temporary unit of memory in the


CPU. These are used to store the data, which is directly used by the processor.
Registers can be of different sizes(16-bit, 32-bit, 64-bit, and so on) and each
register inside the CPU has a specific function, like storing data, storing an
instruction, storing the address of a location in memory, etc.
Memory attached to the CPU is used for the storage of data and instructions and is
called internal memory. This memory is also called RAM, i.e., Random Access Memory.
Fundamentals of Computations
Output Unit: The output unit consists of output devices that are attached to the
computer. It converts the binary data coming from the CPU to human
understandable form. The common output devices are monitors, printer, plotters,
etc.
1. The output unit displays or prints the processed data in a user-friendly
format.
2. The output unit is formed by attaching the output devices of a computer.
3. The output unit accepts the information from the CPU and displays it in a
user-readable form.
Fundamentals of Computations
Computers generally operate in a 2-step cycle

1. Fetch the instruction


2. Execute the instruction

Computers are basically a combination of information processing


systems (CPU, memory) and environments (input and output devices).

Instruction or the set of instructions processed by the CPU is called a


program.

The text form of these instructions is called a CODE.

CPU can only process the instructions that are in machine language.
Fundamentals of Computations
Convert into Binary Language
Suppose you are a resident of USA, and have traveled to China. And ordering
burgers from a local store. But, the shopkeeper does not understand your
language.

And someone else noticed your difficulty and helped you buy a burger, who
knows both English and Chinese. And convert your language English to
Chinese.

Similarly, translator programs can understand both human


instruction (source code) and object code (binary code). This
translator converts source code into object code (binary code).
Fundamentals of Computations
Convert into Binary Language
Machine language, or machine code is comprised of binary digits (0 and 1).

CODE developed by humans is in text form and not binary. So, to process
these instructions, text CODE must be converted into the binary form.

This can be achieved via

Assembler, compiler, and interpreter are programs that translate


human-readable code into machine code (0s and 1s) that a
computer can understand and execute.
Fundamentals of Computations
Convert into Binary Language
Assemblers: translate low-level assembly language into
machine code. It Performs a one-to-one translation of assembly
language instructions into machine code.

Compilers: translate entire high-level programs into machine


code at once before execution. C++.

Interpreters: Simultaneously convert and execute the instructions of


the CODE. This is done in a line-by-line manner.

Example: Python, Matlab, Perl


Fundamentals of Computations
Compiled CODEs are generally faster. However, they have platform
dependence on the generated machine code.

Interpreter CODEs are generally more flexible and dynamic but are usually
slower.

Components of programing language: A programming language is a set of


instructions and syntax used to create software programs. Some of the key
features of programming languages include
1. Data type: The type of values that can be stored in a program, such as
numbers, strings, and booleans.

2. Data structure: Are the ways to organize, store, retrieve, and manage
data efficiently. Data can be represented in complex form. Example:
Arrays.

3. Variables: Place to store the data or values


4. Operators: Symbols used to perform operations on values, such as addition,
subtraction, and comparison.
Fundamentals of Computations
Components of programing language
5. Assignments: It is a statement that sets and/or re-sets the value stored in the
variable Example: a=3+1.

5. Function: It is a command that accomplishes a specific task. It generally


takes an argument and returns a value

Example: print “hello world” ---> hello world

6. Control structure: Control structures are specific commands that dictate


which part of the program the computer will execute next.

Example: If command
Data visualization
Plotting

➢ A plot is a graphical technique for


representing a data set, exhibiting
the relationship between two or
more variables.
➢ They are an important post-
processing method.
➢ Data sets can graphically be
represented in various forms

Line chart
Pie chart
Log plot
Data visualization

Line chart

Also known as line plot or curve plot or


line graph.

This displays information as a series of


data points called 'markers'.

A line chart is often used to visualize a


trend in data.

A multi-line chart can be used to compare


the behavior of same variables in different
environments or conditions.
Data visualization

Bar Graphs

Bar graph or bar chart shows the comparison


between the discreet categories.

Example
Data visualization

Pie (Circle) Chart


It is a circular statistical graph, with each slice
representing the numerical proportion of the category.

Election Result
Data visualization

The central angle = (component value/100) × 360°


The central angle for each category is calculated as follows

Items Percentage of Central angle


cops
Wheat 125/3 [(125/3)/100] ×
360° = 150°
Pulses 125/6 [(125/6)/100] ×
360° = 75°
Jowar 25/2 [(25/2)/100] ×
360° = 45°
Groundnuts 50/3 [(50/3)/100] ×
360° = 60°
Vegetables 25/3 [(25/3)/100] ×
360° = 30°
Total 100 360°
Data visualization
Histogram

Histogram represent the distribution of a


numerical data. X-axis and the Y-axis represent the
range and the frequencies respectively.

Example: The following table gives the lifetime of 400 neon


lamps. Draw the histogram for the below data.
Lifetime (in Number of
hours) lamps
300 – 400 14
400 – 500 56
500 – 600 60
600 – 700 86
700 – 800 74
800 – 900 62
900 – 1000 48
Histogram of marks in a Physics class
Scatter plots

A scatter plot uses dots to represent


values for two different numeric
variables.
Data visualization: Python Programing
Basic programming: Let us start with the very basics of Python
programming.

Example 1: Basic
mathematical operations
on numbers
a = 3.0
b = 5.0
Then, a+b = 8.0
a-b = -2.0
a*b = 15.0
a/b = 0.6
Data visualization: Python Programing
Basic programming: Let us start with the very basics of Python
programming.

Example 2: Magnitude of
a vector.

𝒓 = 𝟏𝒊Ƹ + 𝟑 𝒋Ƹ + 𝟏𝟎 𝒌
The magnitude of the
vector is
𝒓 = 𝟏𝟐 + 𝟑𝟐 + 𝟏𝟎𝟐
𝒓 = 𝟏𝟎. 𝟒𝟖

Homework: Repeat the exercise for some other vectors.


Data visualization: Python Programing
Basic programming: Let us start with the very basics of Python
programming.

Example 3: Know the


state of water using a
given temperature (T).
SOLID if T is less than 0
degrees.

LIQUID if T is greater
than 0 and less than 100
degrees.

GAS if T is greater than


100 degrees.
Data visualization: Python Programing
Basic programming: Let us start with the very basics of Python
programming.

Example 4: Work on the


array.
How can we create a
number array?

Can we add a string in


the number array?

Length of the array.

Sum of the array


Data visualization: Python Programing
Python is a popular computer language because it provides many libraries.
These libraries have several functions, and we can use them easily without
much effort. Here, I will list some of the important libraries.

Numpy: The name “Numpy” stands for “Numerical Python”. It is the most
commonly used library. It is a popular machine learning library that supports
large matrices and multi-dimensional data.

Matplotlib: This library is responsible for plotting numerical data. And that’s
why it is used in data analysis. It is also an open-source library and plots high-
defined figures like pie charts, histograms, scatterplots, graphs, etc.

SciPy: The name “SciPy” stands for “Scientific Python”. It is an open-source


library used for high-level scientific computations. This library is built over an
extension of Numpy. It works with Numpy to handle complex computations.
Homework: Read about various other librariess of Python.
Data visualization: Python Programing
Basic programming: We can import any library in our program, for
example, we need matplotlib for line plot. Now, let us discuss the line plot.

Add some more important


keywords to make the plot more
meaningful.

Add axis name and


title to plot
Data visualization: Python Programing
Two lines in the same plot.
Data visualization: Python Programing
Bar Plot.
Data visualization: Python Programing
Bar Plot.
Data visualization: Python Programing
Histogram
Data visualization: Python Programing
Pie Chart
Data visualization

Scatter plots
A scatter plot uses dots to represent values for
two different numeric variables. If the points
are color codes can be used here to display
one additional variable.

Positive correlation: When the points in the graph are rising, moving from left to
right, then the scatter plot shows a positive correlation. It means the values of one
variable are increasing with respect to another. Now positive correlation can further be
classified into three categories:
Data visualization
Negative correlation: When the points in the Example: The scatter plot for
scatter graph fall while moving left to right, the given data shows the
then it is called a negative correlation. It means number of games played and
the values of one variable are decreasing with
scores obtained in each instance.
respect to another. These are also of three
types:
Data visualization
Contour plot
A contour plot is a graphical technique for
representing a 3-dimensional surface by plotting
constant z slices, called contours, on a 2-dimensional
format.

•Vertical axis: Independent variable 2


•Horizontal axis: Independent variable 1
•Lines: iso-response values.

The contour plot tells us how the third


dimension (Z) changes as a function of
X and Y.
Data visualization
3-D plots
It is the three-dimensional representation of data. They are also known as surface plots.

Here, individual data points show the relationship between t h e designated dependent
variable (Y), and two independent variables (X and Z).
Data visualization: Python Programing
Scatter Plot
Data visualization: Python Programing
Contour Plot: Contour plots are an effective way to visualize three-
dimensional data on a two-dimensional plane.
Data visualization: Python Programing
3-D Plot
Data interpretation
Data interpretation is the process of reviewing data through some
predefined processes which will help assign some meaning to the data
and arrive at a relevant conclusion.
It involves taking the result of data analysis, making inferences on the relations
studied, and used them to conclude.

Data analysis is the process of ordering, categorizing, manipulating, and


summarizing data to obtain answers to research questions. It is usually the first step
taken towards data interpretation.

There are two main methods of data interpretation


[Link] methods
[Link] methods
Data interpretation
Qualitative methods
Qualitative data, also known as categorical data, is a collection of information that is
divided into groups.
Qualitative data can take on numerical values, but those numbers have little
mathematical meaning. Qualitative data can be classified into two categories:
[Link] data: This is a type of data used to name variables without providing any
numerical value.
What is your gender? Male
Female
Other
Prefer not to answer
Do you own a Yes [Link] is your student ID number?
smartphone? No [Link] is your zip code?
What is your favorite Romance
movie genre? Action
[Link] is your native language?
Mystery
Animation
Musical
Comedy
Thriller
Data interpretation
Qualitative methods
2. Ordinal data: This is a data type with a set order or scale to it. However, this order does
not have a standard scale on which the difference in variables in each scale is measured.

Ordinal data are usually collected via surveys or questionnaires. Any type of question
that ranks answers using an explicit or implicit scale can be used to collect ordinal data.
An example might be:
•Question: Which best describes your knowledge of the Python programming
language? Possible answers: Beginner, Basic, Intermediate, Advanced, Expert.

•Question: To what extent do you agree that data analytics is the most important job for
the 21st century? Possible answers: Strongly agree, Agree, Neutral, Disagree, Strongly
Disagree.
Data interpretation
Qualitative methods: Qualitative data is analyzed using mean or distribution etc.
Qualitative data analyzed graphically using a bar chart and pie chart.
Q1. What kind of graph is the Figure?
This is a pie chart. Pie charts are used with
variables that have qualitative categories
(nominal scale of measurement) when you
want to compare proportions
(percentages).

Q2. What does the x-axis measure?


Trick questions! There are no axes in pie
charts! What is normally on the x-axis is
the category or each score, and that is what
the colors and labels identify.

Q3. What does the y-axis measure?


Another trick question! Again, there are no
axes in pie charts! What is normally on the
y-axis is the frequency; instead of having
the numbers on an axis, the size of the pie
slices indicates the size or portion of each
category.
Data interpretation
Q5. What do you notice from this pie chart?
The pie chart shows which groups have the most graduates with Associate of Arts for Transfer,
and that’s Social Sciences, Psychology, Humanities, and Fine and Applied Arts. There are also
a bunch of majors that show 0% of the pie. They don’t have 0%, or they wouldn’t be in the
chart. Instead, those are the ones with really small frequencies, and their percentage is a
decimal point (like 0.05% for Biological Sciences).

In most of the cases, qualitative data has to be converted into numerical data before the
processing. However, these values do not exhibit quantitative characteristics. Arithmetic
operations can not be performed on them.
Data interpretation
Quantitative methods
Quantitative data, also known as numerical data, is always collected in number form.

Quantitative data can be classified into two categories,

1. Discrete Data: This is a data type that represents countable items. It takes on values that
can be grouped into a list.

Examples: Number of students in the class, number of languages an individual speaks.

2. Continuous Data: It represents a data type that can assume any value. Continuous
data represents measurements and therefore their values can't be counted but they
can be measured.

Examples: the amount of time required to complete a project, amount of rain (in
inches) that falls in a storm.
Data interpretation
Quantitative methods
Continuous data can further be divided into two categories: (a) Interval data, and (b)
ratio data

a. Interval data: This is a data type measured along a scale, in which each point is
placed at an equal distance from one another.

b. Ratio data: This data type has the same properties as interval data, with an equal and
definitive ratio between each data and absolute “zero” being treated as a
point of origin. Thus, there can be no negative numerical value in ratio data.
Quantitative data can be analyzed using a variety of methods such as
➢ Descriptive analysis methods: mean, median, mode, etc.

➢ Inferential statistical methods: Trend analysis


Data fitting and (inter-extra)polation
Interpolation or extrapolation is a type
of estimation, a method of
constructing new data points within
the range of a discrete set of known
data points.

To interpolate/extrapolate, we
require a function that is most
suitable to represent the given
data set.
The function required for the
interpolation is obtained via
the curve fitting.

Curve/data fitting: A process of constructing a


mathematical function, that has the best fit to a
series of data points.
Data fitting and (inter-extra)polation
Linear interpolation is a method of curve fitting using linear polynomials (mathematical
function) to construct new data points within the range of a discrete set of known data
points. 𝑦2 − 𝑦1
𝑦 = 𝑦1 + (𝑥 − 𝑥1 )
𝑥2 − 𝑥1
Where, 𝑥1 and 𝑦1 are the first coordinates, and 𝑥2 and 𝑦2 are the second
coordinates. x is the point to perform the interpolation
y is the interpolated value..
Example 1: Find the value of y at x = 4 given some set of values (2, 4), (6, 7)?

7−4
Given the known values are, 𝑦 =4+ (4 − 2)
6−2
x = 4 ; x1 = 2 ; x2 = 6 ; y1 = 4
; y2 = 7 11
𝑦=
2
Data fitting and (inter-extra)polation
Example 2: Calculate the estimated height of the boy in the fourth position.
position(x) x 1 2 3 5

Height in
y 3 4.5 5 6
feet (y)

𝑦2 − 𝑦1
𝑦 = 𝑦1 + (𝑥 − 𝑥1 )
𝑥2 − 𝑥1

6−5
Given the known values are, 𝑦 =5+ (4 − 3)
5−3
x = 4 ; x1 = 3 ; x2 = 5 ; y1 = 5;
y2 = 6 𝑦 = 5.5
Data fitting and (inter-extra)polation
As we know, extrapolation is a process of
predicting the data point about the outside of a
curve when a few points are given. In the
example given below, the known data are x1, x2,
x3. Finding the point x4 is known as the
extrapolation point.

Example: The two given points that lie on the straight line is (1, 5) and (4, 10). Determine
the value of y at x = 5 on the straight line using a linear extrapolation method.
𝑦2 − 𝑦1
Given: x1 = 1 ,y1 = 5 𝑦 = 𝑦1 + (𝑥 − 𝑥1 )
and x2 = 4, y2 = 10 𝑥2 − 𝑥1
10 − 4
𝑦 =5+ (5 − 1)
4−1
𝑦 = 11.67
Data fitting and (inter-extra)polation

Any data is fitted using the polynomial


equation

y= f(x)

f(x) = ax + b – Linear fitting or first


degree polynomial

f(x) = ax + bx2 + c – Second degree


polynomial

f(x) = ax + bx2 + cx3 + d – Third degree


polynomial
Curve fitting

Linear fitting

What values of ‘a’ and ‘b’ are most appropriate?


Curve fitting
Curve fitting
Find the line of best fit for the following points: (2,1), (3,7), (6,5), and (7,9)
We will then find the values for each one
of the four elements in the A matrix:

Y = 1x+1 (Best fit line)

Now, elements in the B matrix:

Putting these values in matrix equation


Curve fitting
To find the values of the parameters (a and b), we need to calculate the inverse
matrix of A and then it should be multiplied with matrix B.

98 18 𝟎. 𝟓𝟖𝟖 −𝟎. 𝟐𝟔𝟒𝟗


→ [Inverse of Matrix A]
18 4 −𝟎. 𝟐𝟔𝟒𝟕 𝟏. 𝟒𝟒
Now this inverse matrix should be multiplied by matrix B.
0.588 −0.2649 116
−0.2647 1.44 22
Solve for a: Matrix multiplication> R1 ×C1
0.0588 × 116 + −0.2644 × 22
6.82 − 5.82 = 1.0
Solve for b: Matrix multiplication> R2 ×C1
−0.2647 × 116 + 1.44 × 22
−30.70 + 31.68 = 0.98 ~ 1.0
Curve fitting
Polynomial fitting

a0, a1, a2, a3, .........., aj ?


Curve fitting
Polynomial fitting
Curve fitting
Polynomial fitting
Curve fitting
Example: Find a 2-degree polynomial that best describes the following points:
(1,1)(2,3)(3,4)(4,8)

We will then find the values for


each one of the nine elements in
the A matrix.
Curve fitting
Example: Find a 2-degree polynomial that best describes the following points:
(1,1)(2,3)(3,4)(4,8)

Now find the values of B matrix.


Plugging these values into the matrix
equation.
Curve fitting

Homework: Find the inverse of matrix A and the product of the A-1 and
B matrix.
Data plotting - Python
import [Link] as plt

x = [1,2,3,4]

y = [1,2,3,4]

[Link](x, y)

[Link]()
Data ploting - Python
import [Link] as plt
ages =[2,5,70,40,30,45,50,45,43,40,44,60,7,13,57,18,90,77,32,21,20,40]
range = (0, 100)
bins = 10
[Link](ages, bins, range)
[Link]('age')
[Link]('# of people')
[Link]('My histogram')
[Link]()
Data ploting - Python
import [Link] as plt
ages =[2,5,70,40,30,45,50,45,43,40,44,60,7,13,57,18,90,77,32,21,20,40]
range = (0, 100)
bins = 10
[Link](ages, bins, range, edgecolor='black')
[Link]('age')
[Link]('# of people')
[Link]('My histogram')
[Link]()
Data ploting - Python
import [Link] as plt

activities = ['eat', 'sleep', 'work', 'play']

slices = [3, 7, 8, 6]

colors = ['r', 'y', 'g', 'b']

[Link](slices, labels = activities, colors=colors)

[Link]()
Data ploting - Python
import [Link] as plt

activities = ['eat', 'sleep', 'work', 'play']

slices = [3, 7, 8, 6]

colors = ['r', 'y', 'g', 'b']

[Link](slices, labels = activities, colors=colors,autopct = '%1.2f%%')

[Link]()
Data ploting - Python
import [Link] as plt
Import numpy as np
x = [1,2,3,4,5,6,7,8,9,10]
y = [2,4,5,7,6,8,9,11,12,12]
[Link](x, y)
[Link]('x – axis')
[Link]('y – axis')
[Link]('scatter plot')
[Link]([Link](min(x), max(x),1.0))
[Link]()
Data ploting - Python
import [Link] as plt

import numpy as np

feature_x = [Link](0, 3.0, 30)

feature_y = [Link](0, 3.0, 30)

[X, Y] = [Link](feature_x, feature_y)


Z = X ** 2 + Y ** 2

[Link](X,Y,Z)

[Link]()
Data ploting - Python
import [Link] as plt
import numpy as np
from mpl_toolkits import mplot3d
[Link](projection="3d")
x=[1,2,3,4,5]
y=[3,5,2,1,4]
z=[5,2,1,4,3]
[Link](x,y,z)
[Link]()
Data interpretation
Quantitative methods
Data interpretation
Quantitative methods
Quantitative data can be utilized for estimation or enumeration.

One can perform mathematical operation on the quantitative data.

Quantitative data may be visualized in different ways depending on the type of data
being investigated, e.g., histogram, scattered plot, or line plot etc.

Note: One of the key factor that influence a data is bias. Presence of nay bias may
lead to falsification of results. Thus it important to avoid bias during the data
collection and interpretation.
Curve
Polynomial fitting
fitting
a0, a1, a2, a3, .........., aj ?
Fundamentals of Computations
Computational Physics

Computer: USE Human:


Calculation Power Understanding

Solve the complex


problem easily
RESULT
Fundamentals of Computations
Computer Software

Problem Solving

Physics Math

We need mathematics to understand the laws of physics. Therefore, we will study


math and physics together in this course

You might also like