PythonforScientificComputing AEC QuestionBank
PythonforScientificComputing AEC QuestionBank
import numpy as np
# Calculate the mean, median, variance, maximum, minimum, range, and standard
deviation
mean = np.mean(data)
median = np.median(data)
variance = np.var(data)
maximum = np.max(data)
minimum = np.min(data)
range_ = maximum - minimum
std_dev = np.std(data)
import pandas as pd
import numpy as np
1|Page
data = df["Data"].values
# Calculate the mean, median, variance, maximum, minimum, range, and standard
deviation
mean = np.mean(data)
median = np.median(data)
variance = np.var(data)
maximum = np.max(data)
minimum = np.min(data)
range_ = maximum - minimum
std_dev = np.std(data)
import numpy as np
import numpy as np
2|Page
inverse = np.linalg.inv(matrix)
5 Write a python program to do linear curve fitting for a given set of x and y
values in data.xlsx file
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
In this program, we first load the image file into a NumPy array using the imread()
function from Matplotlib. We then convert the image array to grayscale by taking the
mean value along the third axis (corresponding to the color channels).
3|Page
We choose a rank value for compression and set all singular values beyond this rank
value to zero. Using matrix multiplication, we then reconstruct the compressed image
from the compressed singular values.
Finally, we save the compressed image as a new file using the imsave() function from
Matplotlib. We also show the original and compressed images side by side using
Matplotlib's subplots() function.
import numpy as np
import matplotlib.pyplot as plt
# Compress the image by keeping only the first 'rank' singular values
S[rank:] = 0
#optional
# Show the original and compressed images side by side
fig, axs = plt.subplots(1, 2)
axs[0].imshow(img_gray, cmap="gray")
axs[0].set_title("Original Image")
axs[1].imshow(img_compressed, cmap="gray")
axs[1].set_title("Compressed Image (Rank {})".format(rank))
plt.show()
7 Write a python program to extract X and y data from excel and plot the
following functions as subplots (i) y=2sin(x)- x^2 (ii) y= x^3 + 3x+5 (iii) y =7^x
+8x+1
In this program, we first read the Excel file "data.xlsx" into a Pandas dataframe and
extract the columns of X and y values using the values attribute.
4|Page
We define three functions to plot: function1(x) which calculates y=2sin(x)-x^2,
function2(x) which calculates y=x^3+3x+5, and function3(x) which calculates
y=7^x+8x+1.
We create a subplot figure with three rows and one column using the subplots()
function from Matplotlib. We plot each function as a line plot on its own subplot
using the plot() function. We also plot the original data as blue dots on each subplot.
We add appropriate axis labels, titles, and legends to each subplot.
Finally, we use the tight_layout() function to improve the spacing between subplots
and show the plot using plt.show().
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def function2(x):
return x**3 + 3*x + 5
def function3(x):
return 7**x + 8*x + 1
5|Page
axs[1].set_xlabel("X")
axs[1].set_ylabel("y")
axs[1].set_title("Function 2")
axs[1].legend()
8 Write a python program to extract X and y data from excel and plot the
following functions in a single plot (i) y=2sin(x)- x^2 (ii) y= x^3 + 3x+5 (iii) y
=7^x +8x+1 as red dot, black star and blue diamond respectively
In this program, we first use the Pandas read_excel() function to read the Excel file
"data.xlsx" into a dataframe. We then extract the columns of x and y values from the
dataframe using the values attribute.
We define the three functions to plot (function1(), function2(), and function3()), and
evaluate each of them for the given x values using NumPy. We then plot the three
functions in a single plot using Matplotlib's plot() function. We use the 'ro', 'k*', and
'bd' format strings to plot the functions as red dots, black stars, and blue diamonds,
respectively. We also use the label argument to provide Latex-formatted legend for
each function.
Finally, we add axis labels and a plot title using Matplotlib's xlabel(), ylabel(), and
title() functions, and add the legend to the plot using the legend() function with the
loc argument set to 'upper left'
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
6|Page
def function1(x):
return 2*np.sin(x) - x**2
def function2(x):
return x**3 + 3*x + 5
def function3(x):
return 7**x + 8*x + 1
9 Write a Program to calculate the cross-product and dot product of two vectors
In this program, we first define the two vectors as NumPy arrays. We then calculate
the cross product of the two vectors using the cross() function from NumPy, and store
the result in the cross_product variable. We print out the cross product using the
print() function.
We also calculate the dot product of the two vectors using the dot() function from
NumPy, and store the result in the dot_product variable. We print out the dot product
using the print() function.
import numpy as np
7|Page
10 Write a program to find the lcm and hcf of three numbers using only the math
library
In this program, we first define the three numbers as variables num1, num2, and
num3.
We calculate the HCF of the three numbers using the gcd() function from the math
module. We use the gcd() function twice, first to calculate the GCD of num1 and num2,
and then to calculate the GCD of the result and num3. The final result is stored in the
hcf variable, which we print using the print() function.
We calculate the LCM of the three numbers using the formula LCM =
|num1*num2*num3| / HCF, where |num1*num2*num3| represents the absolute value
of the product of the three numbers. We calculate the absolute value using the abs()
function. As shown above, we divide the product by the HCF of the three numbers,
which we calculate using the gcd() function. The final result is stored in the lcm
variable, which we print using the print() function.
import math
8|Page