I've written the following code to find a value using the root_scalar function:
def goal_seek(demand_2029_peak, peak_demand_matrix, target_val):
def func(goal_seek_value):
demand_2029_reshaped = ((demand_2029_peak - peak_demand_matrix.loc['2029-2030','Bihar'] )* goal_seek_value) + peak_demand_matrix.loc['2029-2030','Bihar']
return demand_2029_reshaped.sum().iat[0] - target_val
result = root_scalar(func, bracket=[0.1, 2] )
if result.converged:
return result.root
else:
raise ValueError("Goal seeking failed to converge.")
target_val = projected_demand_bau_matrix.loc['2029-2030','Bihar']*1000
result = goal_seek(demand_2029_peak, peak_demand_matrix, target_val)
demand_2029_reshaped = (((demand_2029_peak - peak_demand_matrix.loc['2029-2030','Bihar'] )* result) + peak_demand_matrix.loc['2029-2030','Bihar'])
The code is running successfully but while applying the 'result', some values in demand_2029_reshaped are becoming negative. How do I ensure that the result is found ensuring that none of the final values in demand_2029_reshaped are negative?