Question

In: Computer Science

Using Python write a function that implements the following two-dimensional objective function: F (x, y) =...

Using Python write a function that implements the following two-dimensional objective function: F (x, y) = (x^2 + y − 11)^2 + (x + y^2 − 7 )^22 . Determine how many local minimums and maximums and their location for this objective function.

Solutions

Expert Solution

Solution :

Objective function from the question: F (x, y) = (x^2 + y − 11)^2 + (x + y^2 − 7 )^22

Function which implements the objective function :

Data Used :

Note this can be replace with any data and the solution will still

Step 1 : Calculating the value of the function

will be using the argrelextrema function from scipy.signals lib. This is the most effective , easy and simple method for finding minima and maxima for multi-var. objective functions. It takes two arguments the function values (in out case the array calculated in step 1) and the np.less and np.greater value for minima and maxima respectively. The function doesnt return values but the index.

 

Step 2 : Calculating the minima

step 3: Calculating Maxima

step 4: Combining all steps into a single function :

Code :

from scipy.signal import argrelextrema
import numpy as np

x = np.random.rand(20)
y = np.random.rand(20)

def get_min_max(x,y):
    def f_x_y(x,y):
        return (x**2 + y - 11)**2 + (x + y**2 - 7 )**22
    f_vals = []
    for _x,_y in zip(x,y):
        f_vals.append(f_x_y(_x,_y))
    #this will make our life more easier.
    f_vals = np.asarray(f_vals)
  
    #### Calculating Minima
    index = argrelextrema(np.asarray(f_vals),np.less)
    print(f'No. of minima = {len(index[0])}')
    print(f'values of minima = {f_vals[index[0]]}')
    ### Calculating Maxima
    index = argrelextrema(np.asarray(f_vals),np.greater)
    print(f'No. of maxima = {len(index[0])}')
    print(f'values of maxima = {f_vals[index[0]]}')
  
  
get_min_max(x,y)

Code in textformat:

from scipy.signal import argrelextrema
import numpy as np


x = np.random.rand(20)
y = np.random.rand(20)


def get_min_max(x,y):
    def f_x_y(x,y):
        return (x**2 + y - 11)**2  +  (x + y**2 - 7 )**22
    f_vals = []
    for _x,_y in zip(x,y):
        f_vals.append(f_x_y(_x,_y))
    #this will make our life more easier.
    f_vals = np.asarray(f_vals)
    
    #### Calculating Minima
    index = argrelextrema(np.asarray(f_vals),np.less)
    print(f'No. of minima = {len(index[0])}')
    print(f'values of minima = {f_vals[index[0]]}')
    ### Calculating Maxima
    index = argrelextrema(np.asarray(f_vals),np.greater)
    print(f'No. of maxima = {len(index[0])}')
    print(f'values of maxima = {f_vals[index[0]]}')
    
    
get_min_max(x,y)

Implementing Our Own Functions

from numpy import diff,sign

def get_minmax(f_vals):

    min_index = (diff(sign(diff(f_vals))) > 0).nonzero()[0] + 1
    max_index = (diff(sign(diff(f_vals))) < 0).nonzero()[0]  + 1

    return min_index,max_index


# remember the function returns an array

min_index,max_index = get_minmax(f_vals)

Explanation

let the function values be arr = [x,x2,x3,x4]

Say u had a a local minima at ith position and a maxima at (i+1)th position

then arr(i+i) - a(i) would be a +ve val indicating an minima

if now the positios of the minima and maxima were interchnaged it would produce a -ve number

So. conditions for finding minima and maxima are

if value > 0 then minima

if value < 0 then maxima

we also need to remove the flat area areas which dont have a minima or maxia and make them 0. this is done by the

***********************        If the answer helps then please Upvote.


Related Solutions

Given a module which implements the function Z= F(X, Y), defined as follows:          X +...
Given a module which implements the function Z= F(X, Y), defined as follows:          X + Y when 10<=X<=20, and 12<=Y<=30 Z =    X - Y when 0<=X<10, and 0<=Y<12                 0 under other conditions           where X and Y are integer parameters for F. 1. Identify the equivalence classes in [X, Y]. 2. List your test cases in [X, Y] based on your equivalence class analysis.
Write a MARIE program that implements the following logic. If X < Y Then X =...
Write a MARIE program that implements the following logic. If X < Y Then X = X + Y Else Y = 2X Assume the two numbers are X and Y and are entered by the user. Provide prompts to the user to enter the numbers and provide a meaningful output to the screen.
Write a python function that accepts two integer values corresponding to the point (x, y). Check...
Write a python function that accepts two integer values corresponding to the point (x, y). Check whether the point is within the rectangle centered at (0, 0) with width 20 and height 15. For example, (-9, 7) is inside the rectangle and (11, 4) is outside the rectangle, as shown in the figure. Return True if the point falls within the rectangle and False otherwise
Consider the following function: f (x , y , z ) = x 2 + y...
Consider the following function: f (x , y , z ) = x 2 + y 2 + z 2 − x y − y z + x + z (a) This function has one critical point. Find it. (b) Compute the Hessian of f , and use it to determine whether the critical point is a local man, local min, or neither? (c) Is the critical point a global max, global min, or neither? Justify your answer.
Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
Consider the optimization problem of the objective function f(x, y) = 3x 2 − 4y 2...
Consider the optimization problem of the objective function f(x, y) = 3x 2 − 4y 2 + xy − 5 subject to x − 2y + 7 = 0. 1. Write down the Lagrangian function and the first-order conditions. 1 mark 2. Determine the stationary point. 2 marks 3. Does the stationary point represent a maximum or a minimum? Justify your answer.
Consider a function f(x) which satisfies the following properties: 1. f(x+y)=f(x) * f(y) 2. f(0) does...
Consider a function f(x) which satisfies the following properties: 1. f(x+y)=f(x) * f(y) 2. f(0) does not equal to 0 3. f'(0)=1 Then: a) Show that f(0)=1. (Hint: use the fact that 0+0=0) b) Show that f(x) does not equal to 0 for all x. (Hint: use y= -x with conditions (1) and (2) above.) c) Use the definition of the derivative to show that f'(x)=f(x) for all real numbers x d) let g(x) satisfy properties (1)-(3) above and let...
A function f : X ------> Y between two topological spaces ( X , TX )...
A function f : X ------> Y between two topological spaces ( X , TX ) and ( Y , TY ) is called a homeomorphism it has the following properties: a) f is a bijection (one - to- one and onto ) b) f is continuous c) the inverse fucntion f  -1 is continuous ( f is open mapping) A function with these three properties is sometimes called bicontinuous . if such a function exists, we say X and Y...
Suppose that X and Y have the following joint probability density function. f (x, y) =...
Suppose that X and Y have the following joint probability density function. f (x, y) = (3/394)*y, 0 < x < 8, y > 0, x − 3 < y < x + 3 (a)   Find E(XY). (b)   Find the covariance between X and Y.
Consider the following production function: f(x,y)=x+y^0.5. If the input prices of x and y are wx...
Consider the following production function: f(x,y)=x+y^0.5. If the input prices of x and y are wx and wy respectively, then find out the combination of x and y that minimizes cost in order to produce output level q. Also find the cost function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT