Advertisement
jukaukor

Chinamath.py

Nov 12th, 2023 (edited)
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. #Chinamath
  2. # Juhani Kaukoranta 12.11.2023
  3. import numpy as np
  4. import scipy.optimize as opt
  5. # minimoitava funktio x^2 + y^2
  6. # huom muuttujat x = x[0], y = y[0]
  7. def f(x):
  8. return x[0]**2 + x[1]**2
  9. # rajoitusehto (x+5)^2+(y-12)^2 = 14^2
  10. def g(x):
  11. return (x[0] + 5)**2 + (x[1] - 12)**2 - 14**2
  12. # the rajoitusehdotyyppi, eq=yhtälö, ineg=epäyhtälö,fun=rajoitusehdon funktio
  13. cons = ({'type': 'eq', 'fun': g})
  14. x0 = np.array([-1, -1]) # lähtöarvot otettu "hatusta"
  15. result = opt.minimize(f, x0, constraints=cons)
  16. print(result.message) # tietoja laskennasta
  17. print("lausekkeen x^2 + y^2 minimiarvo = ",f(result.x))
  18. print("minimi saadaan arvoilla x = ",result.x[0]," ja y = ",result.x[1])
  19.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement