0% found this document useful (0 votes)
1 views6 pages

Python Program Nmrec

Uploaded by

yrk.srk.ece
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views6 pages

Python Program Nmrec

Uploaded by

yrk.srk.ece
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

3.(d).

import math

def normal_distribution(x, m, s):

exponent = -((x - m) ** 2) / (2 * s ** 2)

denominator = [Link](2 * [Link]) * s

result = [Link](exponent) / denominator

return result

# Example usage:

x = 0.5

mean = 0.5

standard_deviation = 1

probability = normal_distribution(x, mean, standard_deviation)

print(f"Probability density at x={x}: {probability}")

RESULT:

Probability density at x=0.5: 0.3989422804014327


4.(a).

import numpy as np

def create_random_matrix(m, n):

matrix = [Link](1, 99999+1, size=(m, n))


return matrix

# Example usage:

m=4 # Number of rows


n=2 # Number of columns

random_matrix = create_random_matrix(m, n)

print(random_matrix)

======================================
===============

RESULT:

[[84275 41762]

[32060 48544]

[87878 80391]

[43980 50353]]
4.(b).

import numpy as np

def matrix_addition(matrix1, matrix2):


return [Link](matrix1, matrix2)

def matrix_subtraction(matrix1, matrix2):


return [Link](matrix1, matrix2)

def matrix_multiplication(matrix1, matrix2):


return [Link](matrix1, matrix2)

def print_matrix(matrix):
print(matrix)

# Example matrices

matrix1 = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]])


matrix2 = [Link]([[9, 8, 7], [6, 5, 4], [3, 2, 1]])

# Prompt user for operation choice

operation = input("Enter the operation (addition, subtraction,


multiplication): ")
# Perform the selected operation

if operation == "addition":

result = matrix_addition(matrix1, matrix2)


print("Matrix Addition:")
print_matrix(result)

elif operation == "subtraction":

result = matrix_subtraction(matrix1, matrix2)


print("Matrix Subtraction:")
print_matrix(result)
elif operation == "multiplication":

result = matrix_multiplication(matrix1, matrix2)


print("Matrix Multiplication:")
print_matrix(result)

else:

print("Invalid operation.")

======================================
===============

RESULT:

Enter the operation (addition, subtraction, multiplication):


addition

[[84275 41762]
[32060 48544]
[87878 80391]
[43980 50353]]
======================================
===============

Enter the operation (addition, subtraction, multiplication):


subtraction

Matrix Subtraction:

[[-8 -6 -4]
[-2 0 2]
[ 4 6 8]]
======================================
===============
Enter the operation (addition, subtraction, multiplication):
multiplication

Matrix Multiplication:

[[ 30 24 18]
[ 84 69 54]
[138 114 90]]

4.(c).

import numpy as np

def solve_linear_equations(coefficients, constants):


# Convert the coefficient matrix and constants array to NumPy arrays
A = [Link](coefficients)
B = [Link](constants)

# Calculate the inverse of the coefficient matrix


A_inverse = [Link](A)

# Solve the system of equations using matrix multiplication


solution = [Link](A_inverse, B)
return solution

# Example system of equations:


# 2x + 3y = 8
# 4x - 2y = 2

# Coefficient matrix
coefficients = [[2, 3],
[4, -2]]
# Constants array
constants = [8, 2]

# Solve the system of equations


solution = solve_linear_equations(coefficients, constants)

# Print the solution


print("Solution:")
for i in range(len(solution)):
print("x{ } = { }".format(i+1, solution[i]))
======================================
===============
RESULT:
Solution:
x1 = 1.375
x2 = 1.75

You might also like