—_———
Science and Numerical
Methods
Book: A Journey Through Computational Science and Numerical
Methods: An Undergraduate Guide
Prof. John B. Oladosu
Lecture 3: Programming FundamentalsIntroduction
* Programming is the heart of
computational science. It provides the
means to implement algorithms,
simulate scientific phenomena, and
analyze data. In this Lecture, we'll
explore the essential programming
fundamentals necessary for success in
computational science.3.1 Choosing a Programming Language
‘Selecting the right programming language is crucial in computational science. The choice often depends on factors
like application domain, ease of use, performance, and available libraries. Common languages used in computational
science include
41 Python: Knoven for its simplicity and readability, Python is a popular choice, it has a vast ecosystem of libraries for
scientific computing (e.g., NumPy, SciPy) and data analysis (e.g. Pandas).
Download Python -free latest version at:
https www anole com/uri?chentsinternal-element-cse&cx=0126843313801678081 Ode So}-mdt a
softonic com/downloadé.sa=Uaved=7ah UKEW|/kNzblouAxUzlokE HDRTCKWOF noECACOANBLSG=
‘OwVan2yEwivimv@yB4DEIHK36L
The Pthon Tela: hps:/é0cs pthon ory tone nex rd
C/C++: These languages provide high performance but demand more low-level control. They are commonly used in
‘numerical simulations and high-performance computing
os:f/python.en
J. MATLAB: MATLAB is renowned for its numerical computing capabilities and an extensive set of toolboxes. I's widely
used in academia and industry for mathematical modeling and simulation.
is a language specifically designed for statistical analysis and data visualization. i's a preferred choice for
Statisticians and data scientists,
|. Julia: Julia is gaining popularity for its high-performance numerical computing capabilities and ease of use. it's
designed for scientific computing
+The choice of language depends on the specific requirements of your computational tasks and your personal
preferences.3.2 Basics of Coding: Syntax and Semantics
+ Towrite effective code, you must understand the syntax (the rules for constructing
statements) and semantics (the meaning behind the statements) of the programming
language you're using, Here are some common elements:
{i Variables: Variables store data, and their names are case-sensitive. Declare and
initialize variables before using them.
{| Data Types: Different programming languages support various data types, such as
integers, floating-point numbers, strings, and arrays. Understanding data types is crucial
for memory management and type safety.
Operators: Operators allow you to perform operations on data. Common operators
include arithmetic (+, -, *, /), comparison (==, <, >), and logical (&&, ||).
Control Structures: Control structures, like loops (for, while) and conditionals (if else),
enable you to control the flow of your program.3.3 Variables, Data Types, and Operations
+ 3.3.1 Variables and Data Types
ii Integers: Whole numbers, both positive and negative.
Floating-Point Numbers: Numbers with decimal points,
representing real numbers.
§ Strings: Sequences of characters used for text processing.
i Boolean: Represents true or false values.
ii Arrays: Ordered collections of elements, often of the same data
type.3.3.2 Operations
Xt Arithmetic Operations: Addition (+), subtraction
(-), multiplication (*), division (/), modulus (%),
and exponentiation (**).
Comparison Operations: Equal to (==), not equal
to (!=), greater than (>), less than (<), greater than
or equal to (>=), and less than or equal to (<=).
Logical Operations: AND (&&), OR (||), and NOT
().3.4 Control Structures
+ 3.4.1 Conditional Statements
+ Conditional statements allow you to execute different code blocks based
on conditions:
4 if: Executes code if a condition is true.
1 else: Executes code if the condition in the corresponding if statement is
false.
4 elif (else if): Provides an alternative condition to check if the previous if/
elif conditions are false.
+ 3.4.2 Loops
+ Loops enable you to repeat code blocks multiple times:
4) for: Iterates over a sequence (e.g., a list) a predefined number of times.
1] while: Repeats code as long as a condition is true.3.5 Functions and Modularization
+ Modular programming involves breaking your code into smaller,
manageable pieces. Functions are a key component of
modularization:
{ Functions: Functions are blocks of code that perform a specific
task. They allow you to reuse code, improve readability, and
simplify debugging.
\j Parameters: Functions can take input parameters (arguments)
and return results.
§ Scope: Variables can have different scopes, such as local
(within a function) or global (accessible throughout the
program). Understanding scope is crucial for avoiding variable
conflicts.3.6 Debugging and Testing
+ Effective debugging is essential for identifying and fixing errors in your
code:
1) Debugging Tools: Familiarize yourself with debugging tools provided by
your programming environment. These tools help you inspect variables,
step through code, and pinpoint issues.
1) Error Messages: Pay attention to error messages and use them as clues
to identify problems in your code.
1) Testing: Develop a habit of writing test cases to ensure that your code
works as expected. Unit testing, integration testing, and regression testing
are common testing approaches.
1) Version Control: Use version control systems like Git to track changes to
your code, collaborate with others, and revert to previous versions if
needed.