0% found this document useful (0 votes)
9 views4 pages

Arpit Py

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

Arpit Py

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

WORKSHEET 2

Student Name: Arpit Mahajan UID:22BCS16706


Branch: BE-CSE Section/Group:819 (b)
Semester:4th Date of Performance:04/03/24
Subject Name: Python Subject Code:22CSH-25

1. Aim:- A mobile company wants to optimize the production of a new type of


mobile phone their historical data on mobile phone project at different screen -size
and want to find optimal screen -size that maximize the profits.The screen -size
range is known to between 0 to 11 .the relationship between the profit and screen
-size is described by following equation:profit =sales*200 -100*(screen-size)^2 ,
where sales is function
of screen-size
sales=a*(screen-size)^2-b*(screen-size)+c
You have been to develop a python program using a secant method to find the
optimal screen-size.
a=coefficient of (screen-size)^2
b=coefficient of (screen-size)
c=constant

2. Source Code:

print("Secant formula: ((x1 - x) * f(x0) + (x - x0) * f(x1)) / (x1 - x0)")

# Define the sales function


def compute_sales(screensize, coeff_a, coeff_b, constant_c):
return screensize**2 - coeff_b * screensize + 2

# Define the profit function based on the sales function


def compute_profit(screensize, coeff_a, coeff_b, constant_c):
return compute_sales(screensize, coeff_a, coeff_b, constant_c) * 200 - 100 *
screensize**2

# Define the derivative of the profit function using numerical differentiation


def compute_derivative_profit(screensize, coeff_a, coeff_b, constant_c, delta=1e-5):
return (compute_profit(screensize + delta, coeff_a, coeff_b, constant_c) -
compute_profit(screensize, coeff_a, coeff_b, constant_c)) / delta

# Implement the secant method to find the optimal screensize that maximizes profit
def apply_secant_method(guess_one, guess_two, threshold, iterations_limit, coeff_a,
coeff_b, constant_c):
prev_guess = guess_one
current_guess = guess_two
iteration_count = 0

while abs(current_guess - prev_guess) > threshold and iteration_count <


iterations_limit:
next_guess = current_guess - (compute_derivative_profit(current_guess, coeff_a,
coeff_b, constant_c) * (current_guess - prev_guess)) / (
compute_derivative_profit(current_guess, coeff_a, coeff_b, constant_c) -
compute_derivative_profit(prev_guess, coeff_a, coeff_b, constant_c))
prev_guess, current_guess = current_guess, next_guess
iteration_count += 1

return current_guess

# Set the coefficients and constants for the sales and profit functions
a_coefficient = 1
b_coefficient = 1
constant_term = 2

# Set the initial guesses, tolerance, and maximum iterations for the secant method
first_guess = 0
second_guess = 10
precision_tolerance = 1e-6
iteration_cap = 1000

# Calculate the optimal screen size using the secant method


optimal_screensize = apply_secant_method(first_guess, second_guess,
precision_tolerance, iteration_cap, a_coefficient, b_coefficient, constant_term)

# Output the optimal screen size and the maximum profit


print(f"Optimal Screen Size: {optimal_screensize}")
print(f"Max Profit: {compute_profit(optimal_screensize, a_coefficient, b_coefficient,
constant_term)}")
3. Screenshot of Outputs:-

4. Learning Outcomes:-
(i). Understanding how to define and implement mathematical and numerical
functions in Python.
(ii). Familiarity with the secant method for finding roots of equations.
(iii). Ability to apply optimization techniques to maximize or minimize
objective functions.
(iv). Practicing problem-solving and algorithmic thinking skills.
(v). Reinforcing knowledge of basic programming concepts such as
loops, conditionals, and functions.

You might also like