```python
import sympy as sym
from IPython.display import display, Math, Latex
import pandas as pd
import numpy as np
def func_str(function):
    return str(function)
    function_str = str(function)
    function_str = function_str.replace("**", "^")
    return function_str
class Iterative:
    def __init__(self, function, *args):
        self.variables = []
        self.function = function
        self.data_dict = {"iterate":[],"G_val":[]}
        for variable in args:
            self.variables.append(variable)
            self.data_dict["der_" + str(variable)] = []
            self.data_dict[str(variable)] = []
        self.partial_deravatives()

    def partial_deravatives(self):
         self.deravatives = []
         i = 0
         for variable in self.variables:
             self.deravatives.append(sym.diff(self.function, variable))
             display(Math(r"\frac{\partial G(\mathbf{Y_k})}{\partial y_%d}" % (i+1) + " = " + str(self.deravatives[i])))
             i += 1
    
    def calc_limit_state(self, numerical_vars):
        temp_dict = {}
        to_show = "G("
        for index,variable in enumerate(self.variables):
            temp_dict[variable] =numerical_vars[index]
            if index != (len(self.variables)-1):
                to_show += str(variable) + " = " + str(numerical_vars[index]) + ", "
            else:
                to_show += str(variable) + " = " + str(numerical_vars[index]) + ")"
        display(Math(to_show)) 
        return self.function.subs(temp_dict)


    def calc_gradient(self, numerical_vars):
        gradient = []
        temp_dict = {}
        for index, variable in enumerate(self.variables):
            temp_dict[variable] = numerical_vars[index]
        
        for deravative in self.deravatives:
            gradient.append(deravative.subs(temp_dict))
        
        return gradient


    def make_dataframe(self):
        return pd.DataFrame(data=self.data_dict)

    def iterate(self):
        i = 0
        self.ys = []
        while(True):
            if(i == 0):
                for j in range(len(self.variables)):
                    self.ys.append(0)
            self.data_dict["iterate"].append(i)
            i += 1
            gradient = np.array(self.calc_gradient(self.ys))
            limit_state = self.calc_limit_state(self.ys)
            ys = np.array(self.ys).reshape(-1,1)
            gradient_measurement = sum(gradient**2)
            self.ys = (1/gradient_measurement)*(gradient.dot(ys)[0] - limit_state) * gradient
            self.data_dict["G_val"].append(limit_state)
            for index in range(len(self.variables)):
                self.data_dict["der_" + str(self.variables[index])].append(gradient[index])
                self.data_dict[str(self.variables[index])].append(ys[index][0])

            if abs(limit_state) < 0.001:
                break
        
    
    def get_beta(self):
        self.beta = (sum(self.ys**2))**0.5
        display(Math(r"\beta = " + str(self.beta)))
        return self.beta
        
    

        


y_1, y_2 = sym.symbols('y_1 y_2')

G =  10*y_1 - 4*y_2 + 12


iterative = Iterative(G, y_1, y_2)
iterative.iterate()
iterative.get_beta()
iterative.make_dataframe().round(3)
```


$\displaystyle \frac{\partial G(\mathbf{Y_k})}{\partial y_1} = 10$



$\displaystyle \frac{\partial G(\mathbf{Y_k})}{\partial y_2} = -4$



$\displaystyle G(y_1 = 0, y_2 = 0)$



$\displaystyle G(y_1 = -30/29, y_2 = 12/29)$



$\displaystyle \beta = 1.11417202906231$





<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>iterate</th>
      <th>G_val</th>
      <th>der_y_1</th>
      <th>y_1</th>
      <th>der_y_2</th>
      <th>y_2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0</td>
      <td>12</td>
      <td>10</td>
      <td>0</td>
      <td>-4</td>
      <td>0</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1</td>
      <td>0</td>
      <td>10</td>
      <td>-30/29</td>
      <td>-4</td>
      <td>12/29</td>
    </tr>
  </tbody>
</table>
</div>



e3e7dc90e0d42549218c1a363cb8e4416047ada8
ipynb
Jupyter Notebook
limit-states/iterative/Python/Iterative.ipynb
amirhkiani77/Risk-Management
f6a868200fb90bdae21c14381084f8f01f6faf6c
MIT
limit-states/iterative/Python/Iterative.ipynb
amirhkiani77/Risk-Management
f6a868200fb90bdae21c14381084f8f01f6faf6c
MIT
limit-states/iterative/Python/Iterative.ipynb
amirhkiani77/Risk-Management
f6a868200fb90bdae21c14381084f8f01f6faf6c
MIT
Qwen/Qwen-72B
1. YES
2. YES