Untitled1
December 12, 2025
[1]: #Numpy is used for working with arrays (1D, 2D & 3D)
#Numpy is mosty used for algebra, fourier transforms and matrices.
[2]: #pip install numpy (through terminal or command prompt)
#import numpy
[4]: import numpy as np
arr = [Link]({1,2,3,4,5})
print(arr)
{1, 2, 3, 4, 5}
[5]: #numpy provide an array object that is faster than python lists
#array object in numpy is called ndarray
[6]: import numpy as np
arr= [Link](0)
print(arr)
print([Link])
0
0
[8]: #array types
#1d
import numpy as np
arr= [Link]([1,2,3,4,5])
print(arr)
print([Link])
print([Link])
[1 2 3 4 5]
1
(5,)
[11]: import numpy as np
arr= [Link]([[1,2,3,],
[4,5,6]])
print(arr)
1
print([Link])
print([Link])
[[1 2 3]
[4 5 6]]
2
(2, 3)
[12]: import numpy as np
arr= [Link]([[[1,2,3],[4,5,6]],
[[1,2,3], [4,5,6]]])
print(arr)
print([Link])
print([Link])
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
3
(2, 2, 3)
[17]: import numpy as np
#Creating arrays from Existing Data
T= [300,320,340,360]
T_arr= [Link](T)
print("Original list", T)
print("NumPy array", T_arr)
print(T_arr.ndim)
print(T_arr.shape)
Original list [300, 320, 340, 360]
NumPy array [300 320 340 360]
1
(4,)
[21]: #Creating Arrays from Ranges
#[Link]() is used to create an array with regular steps
#[Link](start, stop, step)
import numpy as np
p_range = [Link](1, 518979, 1000) #1 to [Link]., with step 1000
print("Pressure range (bar)\n", p_range)
print("\n")
#[Link]() is used to create evenly spaced numbers between two limits
#Time steps in a batch reactor
time_steps= [Link](0,10,15) # 0 to 10 seconds (15 values)
2
print("Time steps\n", time_steps)
Pressure range (bar)
[ 1 1001 2001 3001 4001 5001 6001 7001 8001 9001
10001 11001 12001 13001 14001 15001 16001 17001 18001 19001
20001 21001 22001 23001 24001 25001 26001 27001 28001 29001
30001 31001 32001 33001 34001 35001 36001 37001 38001 39001
40001 41001 42001 43001 44001 45001 46001 47001 48001 49001
50001 51001 52001 53001 54001 55001 56001 57001 58001 59001
60001 61001 62001 63001 64001 65001 66001 67001 68001 69001
70001 71001 72001 73001 74001 75001 76001 77001 78001 79001
80001 81001 82001 83001 84001 85001 86001 87001 88001 89001
90001 91001 92001 93001 94001 95001 96001 97001 98001 99001
100001 101001 102001 103001 104001 105001 106001 107001 108001 109001
110001 111001 112001 113001 114001 115001 116001 117001 118001 119001
120001 121001 122001 123001 124001 125001 126001 127001 128001 129001
130001 131001 132001 133001 134001 135001 136001 137001 138001 139001
140001 141001 142001 143001 144001 145001 146001 147001 148001 149001
150001 151001 152001 153001 154001 155001 156001 157001 158001 159001
160001 161001 162001 163001 164001 165001 166001 167001 168001 169001
170001 171001 172001 173001 174001 175001 176001 177001 178001 179001
180001 181001 182001 183001 184001 185001 186001 187001 188001 189001
190001 191001 192001 193001 194001 195001 196001 197001 198001 199001
200001 201001 202001 203001 204001 205001 206001 207001 208001 209001
210001 211001 212001 213001 214001 215001 216001 217001 218001 219001
220001 221001 222001 223001 224001 225001 226001 227001 228001 229001
230001 231001 232001 233001 234001 235001 236001 237001 238001 239001
240001 241001 242001 243001 244001 245001 246001 247001 248001 249001
250001 251001 252001 253001 254001 255001 256001 257001 258001 259001
260001 261001 262001 263001 264001 265001 266001 267001 268001 269001
270001 271001 272001 273001 274001 275001 276001 277001 278001 279001
280001 281001 282001 283001 284001 285001 286001 287001 288001 289001
290001 291001 292001 293001 294001 295001 296001 297001 298001 299001
300001 301001 302001 303001 304001 305001 306001 307001 308001 309001
310001 311001 312001 313001 314001 315001 316001 317001 318001 319001
320001 321001 322001 323001 324001 325001 326001 327001 328001 329001
330001 331001 332001 333001 334001 335001 336001 337001 338001 339001
340001 341001 342001 343001 344001 345001 346001 347001 348001 349001
350001 351001 352001 353001 354001 355001 356001 357001 358001 359001
360001 361001 362001 363001 364001 365001 366001 367001 368001 369001
370001 371001 372001 373001 374001 375001 376001 377001 378001 379001
380001 381001 382001 383001 384001 385001 386001 387001 388001 389001
390001 391001 392001 393001 394001 395001 396001 397001 398001 399001
400001 401001 402001 403001 404001 405001 406001 407001 408001 409001
410001 411001 412001 413001 414001 415001 416001 417001 418001 419001
420001 421001 422001 423001 424001 425001 426001 427001 428001 429001
430001 431001 432001 433001 434001 435001 436001 437001 438001 439001
440001 441001 442001 443001 444001 445001 446001 447001 448001 449001
3
450001 451001 452001 453001 454001 455001 456001 457001 458001 459001
460001 461001 462001 463001 464001 465001 466001 467001 468001 469001
470001 471001 472001 473001 474001 475001 476001 477001 478001 479001
480001 481001 482001 483001 484001 485001 486001 487001 488001 489001
490001 491001 492001 493001 494001 495001 496001 497001 498001 499001
500001 501001 502001 503001 504001 505001 506001 507001 508001 509001
510001 511001 512001 513001 514001 515001 516001 517001 518001]
Time steps
[ 0. 0.71428571 1.42857143 2.14285714 2.85714286 3.57142857
4.28571429 5. 5.71428571 6.42857143 7.14285714 7.85714286
8.57142857 9.28571429 10. ]
[23]: #Indexing and Slicing
#Selecting temperatures
T= [300, 320, 340, 360]
T_arr = [Link](T)
print("First temp", T_arr[0])
print("Last two temps", T_arr[-2:])
print("all values", T_arr[0:4])
First temp 300
Last two temps [340 360]
all values [300 320 340 360]
[25]: #array view
import numpy as np
# Original array
temp = [Link]([300, 320, 340, 360])
# Create a view
T_view = [Link]()
#modify the view
T_view[1] = 999
print("Original array", temp)
print("view array", T_view)
#Both (Original & view array) change
Original array [300 999 340 360]
view array [300 999 340 360]
[26]: #array copy
import numpy as np
# Original array
temp = [Link]([300, 320, 340, 360])
# Create a copy
T_view = [Link]()
#modify the copy
4
T_view[1] = 999
print("Original array", temp)
print("copy array", T_view)
#Both (Original & copy array) change
Original array [300 320 340 360]
copy array [300 999 340 360]
[28]: #Reshaping
#Converting 1D data to 2D matrix
data = [Link]([10,12,15,18,20,22,1,2,3])
reshape= [Link](3,3)
print("Original\n", data)
print("Reshaped to 3x3 matrix\n", reshape)
Original
[10 12 15 18 20 22 1 2 3]
Reshaped to 3x3 matrix
[[10 12 15]
[18 20 22]
[ 1 2 3]]
[29]: #trigonometric
import numpy as np
x= [Link]([0, [Link]/2, [Link]])
print([Link])
print("sin:", [Link](x))
print("cos:", [Link](x))
print("sqrt:", [Link]([4,9,16]))
3.141592653589793
sin: [0.0000000e+00 1.0000000e+00 1.2246468e-16]
cos: [ 1.000000e+00 6.123234e-17 -1.000000e+00]
sqrt: [2. 3. 4.]
[30]: #matrix
import numpy as np
A= [Link]([[1,2], [3,4]])
B= [Link]([[5,6], [7,8]])
#Matrix multiplication
print("Dot Product\n", [Link](A,B))
Dot Product
[[19 22]
[43 50]]
5
[32]: import numpy as np
v1 = [Link]([1,2,3])
v2 = [Link]([4,5,6])
cross_result = [Link](v1,v2)
print("Cross Product", cross_result)
Cross Product [-3 6 -3]
[36]: #Quiz
#To write a programme for cosine wave and find their relative maxima and minima
import numpy as np
# Create a simple sine wave signal
t = [Link](0, 60, 15)
# 't' is the time axis from 0-60 with 15 points/values
# It represents 15 time measurements taken in 60 second.
#sin(2pift)
# Create the signal using a sine wave with frequency of 5 Hz,
# 5 Hz means the wave completes 5 cycles in 1 second.
signal = [Link](2 * [Link] * 0.1 * t)
# Apply fast Fourier Transform (FFT)
# FFT convrts the signal from time domain to frequency domain.
# It tells us which frequencies exist in the signal.
fft_result = [Link](signal)
print("FFT Result:", fft_result)
import [Link] as plt
[Link](t, signal, "o:y", ms = 10, mec="r", mfc="y")
[Link]("Time Domain Signal (Sine Wave)")
[Link]("Time (seconds)")
[Link]("Amplitude")
[Link]()
FFT Result: [ 1.11022302e-16+0.j 1.03364354e-02-0.04862911j
4.57164823e-02-0.1026809j 1.23888344e-01-0.17051768j
3.00861770e-01-0.27089715j 8.11566260e-01-0.468558j
4.26801014e+00-1.38676056j -5.56037943e+00+0.58441943j
-5.56037943e+00-0.58441943j 4.26801014e+00+1.38676056j
8.11566260e-01+0.468558j 3.00861770e-01+0.27089715j
1.23888344e-01+0.17051768j 4.57164823e-02+0.1026809j
1.03364354e-02+0.04862911j]
6
[37]: import numpy as np
import [Link] as plt
#Create data using NumPy (flow rate & pressure drop)
# Flow rate (L/min)
flow_rate = [Link](1, 518979, 10000) # 10, 20, 30 ... 100
# Pressure drop (bar)
pressure_drop = 0.02 * flow_rate**2 + 1.5 * flow_rate + 10
print("Flow Rate", flow_rate)
print("\n")
print("Pressure Drop", pressure_drop)
print("\nMean Pressure", [Link](pressure_drop))
print("Max Pressure", [Link](pressure_drop))
print("Min Pressure", [Link](pressure_drop))
#Generate smooth curve using linspace for plotting
flow_smooth = [Link](flow_rate.min(), flow_rate.max())
7
pressure_smooth = 0.02 * flow_smooth**2 + 1.5 * flow_smooth + 10
#Trendline using NumPy polyfit (linear)
#For linear regression, polyfit is used with deg=1,
#which fits a first-degree polynomial (a straight line) to the data points
lin_slope, lin_intercept = [Link](flow_rate, pressure_drop,1)
lin_trend = lin_slope * flow_rate + lin_intercept
#y=mx+c
print("\nLinear Trend Slope", lin_slope)
print("Linear Trend Intercept", lin_intercept)
print("line_trend",lin_trend)
#Plotting
[Link](flow_rate, pressure_drop, label="Experimental Data", color='red')␣
↪#for dots
# Plot linear trendline
[Link](flow_rate, lin_trend, '--', label=f"Linear Trend (Slope={lin_slope:.
↪2f})")
# Smooth analytical curve
[Link](flow_smooth, pressure_smooth, label="Smooth Curve", linewidth=1) #for␣
↪line
#Label max & min points
max_p = [Link](pressure_drop)
min_p = [Link](pressure_drop)
[Link](flow_rate[max_p], pressure_drop[max_p], " Max", fontsize=10)
[Link](flow_rate[min_p], pressure_drop[min_p], " Min", fontsize=10)
[Link]("Flow Rate (L/min)")
[Link]("Pressure Drop (kPa)")
[Link]("Pressure Drop vs Flow Rate (Using NumPy Functions)")
[Link](True)
[Link]()
[Link]()
Flow Rate [ 1 10001 20001 30001 40001 50001 60001 70001 80001 90001
100001 110001 120001 130001 140001 150001 160001 170001 180001 190001
200001 210001 220001 230001 240001 250001 260001 270001 280001 290001
300001 310001 320001 330001 340001 350001 360001 370001 380001 390001
400001 410001 420001 430001 440001 450001 460001 470001 480001 490001
500001 510001]
Pressure Drop [1.15200000e+01 2.01541152e+06 8.03081152e+06 1.80462115e+07
3.20616115e+07 5.00770115e+07 7.20924115e+07 9.81078115e+07
1.28123212e+08 1.62138612e+08 2.00154012e+08 2.42169412e+08
2.88184812e+08 3.38200212e+08 3.92215612e+08 4.50231012e+08
5.12246412e+08 5.78261812e+08 6.48277212e+08 7.22292612e+08
8.00308012e+08 8.82323412e+08 9.68338812e+08 1.05835421e+09
8
1.15236961e+09 1.25038501e+09 1.35240041e+09 1.45841581e+09
1.56843121e+09 1.68244661e+09 1.80046201e+09 1.92247741e+09
2.04849281e+09 2.17850821e+09 2.31252361e+09 2.45053901e+09
2.59255441e+09 2.73856981e+09 2.88858521e+09 3.04260061e+09
3.20061601e+09 3.36263141e+09 3.52864681e+09 3.69866221e+09
3.87267761e+09 4.05069301e+09 4.23270841e+09 4.41872381e+09
4.60873921e+09 4.80275461e+09 5.00077001e+09 5.20278541e+09]
Mean Pressure 1751392711.5200005
Max Pressure 5202785411.52
Min Pressure 11.52
Linear Trend Slope 10201.539999999999
Linear Trend Intercept -850010190.0199991
line_trend [-8.49999988e+08 -7.47984588e+08 -6.45969188e+08 -5.43953788e+08
-4.41938388e+08 -3.39922988e+08 -2.37907588e+08 -1.35892188e+08
-3.38767885e+07 6.81386115e+07 1.70154012e+08 2.72169412e+08
3.74184812e+08 4.76200212e+08 5.78215612e+08 6.80231012e+08
7.82246412e+08 8.84261812e+08 9.86277212e+08 1.08829261e+09
1.19030801e+09 1.29232341e+09 1.39433881e+09 1.49635421e+09
1.59836961e+09 1.70038501e+09 1.80240041e+09 1.90441581e+09
2.00643121e+09 2.10844661e+09 2.21046201e+09 2.31247741e+09
2.41449281e+09 2.51650821e+09 2.61852361e+09 2.72053901e+09
2.82255441e+09 2.92456981e+09 3.02658521e+09 3.12860061e+09
3.23061601e+09 3.33263141e+09 3.43464681e+09 3.53666221e+09
3.63867761e+09 3.74069301e+09 3.84270841e+09 3.94472381e+09
4.04673921e+09 4.14875461e+09 4.25077001e+09 4.35278541e+09]
9
[ ]:
10