Skip to content

Instantly share code, notes, and snippets.

@mo271
Created February 17, 2024 10:46
Show Gist options
  • Save mo271/2331075d9e53aed26a55f758a398e4a9 to your computer and use it in GitHub Desktop.
Save mo271/2331075d9e53aed26a55f758a398e4a9 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 18,
"id": "7c875e23-7f7d-4304-b008-123666087088",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(a*c + a/(a - 1) - exp(c*(a - 1)),\n",
" -a/(a - 1)**2 - c*exp(c*(a - 1)) + c + 1/(a - 1))"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import sympy as sp\n",
"a = sp.Symbol('a')\n",
"c = sp.Symbol('c')\n",
"f = (a*c - sp.exp((a - 1)*c)) + a / (a - 1)\n",
"df = f.diff(a)\n",
"f, df"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "908ab12a-e945-4d60-993f-e74f75eeae5a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"matrix(\n",
"[['2.1369288434405983790865170951767170599904779730789449344794844361536205601773777424850797757178744377791613191684345515993224217927500645297697720151273214104825006580890633351903047069526592911249008787095329540018528815759529365889422114001441246630676080501223506784902879492880036329858560929964335662124026497564620860355272457674531031969580611216253150746613695782880428433635931431295253628038740275088034409943030315488104062364169189301199231361987377688743503812607758229121259834372619955'],\n",
" ['0.23713459431207597508706354607900278401265681434774266673126097338724911991228990266934243036798935194223645533810387885661440940351790095831018323258137352829262520560896803124938071351209375320453534670353177206912396151977812791123316428707544035276178661527257442086120225024946965458265036217141467384666365025494015422983264318011547916962649523773004295500254021250037985649171052751506746561720702451705917395535699987901839312542940686216580985327445186171036176784393182467933133666448004691']])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from mpmath import mp\n",
"mp.dps = 500\n",
"\n",
"def g(c, a):\n",
" return (a*c + a/(a - 1) - mp.exp(c*(a - 1)), -a/(a - 1)**2 - c*mp.exp(c*(a - 1)) + c + 1/(a - 1))\n",
"\n",
"# Provide an initial guess for c and a \n",
"initial_guess = (mp.mpf('2.1'), mp.mpf('0.5'))\n",
"\n",
"M = mp.findroot(g, initial_guess)\n",
"M"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "c6c44fd3-b031-4e77-a600-e8b756229815",
"metadata": {},
"outputs": [],
"source": [
"# c = 1/2*(sqrt(-4*a + 1) - 1)/(a^2 - a)\n"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "da7ed082-4377-41e0-8951-fec77e9aca0f",
"metadata": {},
"outputs": [],
"source": [
"def new_g(a):\n",
" return 1/2*(a - 1)*(a*(mp.sqrt(-4*a + 1) - 1)/(a**2 - a) + 2*a/(a - 1) - 2*mp.exp(1/2*(a - 1)*(mp.sqrt(-4*a + 1) - 1)/(a**2 - a)))\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "487011ec-2de7-41c6-9181-df4e6e7d4e32",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"mpc(real='0.23713459431207597508706354607900278401265681434774266673126097338724911991228990266934243036798935194223645533810387885661440940351790095831018323258137352829262520560896803124938071351209375320453534670353177206912396151977812791123316428707544035276178661527257442086120225024946965458265036217141467384666365025494015422983264318011547916962649523773004295500254021250305677129524158833577915913382148218637768189869230150976722986751505898906016816749784240116647277261694014694987458287430627001834', imag='9.0950494792575572707138980937408016523425350698951843679694332350785039736358254456467319091641444317185374422056420904232753166755354639876228131771856030100547712630314097895489256220786532926261678692186535647319600426589761493537833007746167033238993273349466230632295360068868552237264493769200625877396517558937306955349614180593567852331122926022048026983080185706550198586816842798788656254203280314073642469527382108338440455046268386499736502272847879847925510561687776839717929636228052181438e-558')"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = mp.findroot(new_g, mp.mpf('0.23'))\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "615109f6-3523-45af-9523-e501e27734e7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"mpc(real='2.1369288434405983790865170951767170599904779730789449344794844361536205601773777424850797757178744377791613191684345515993224217927500645297697720151273214104825006580890633351903047069526592911249008787095329540018528815759529365889422114001441246630676080501223506784902879492880036329858560929964335662124026497564620860355272457674531031969580611216253150746613695782988307850651144091668887428835106618882995878596226476299223141111493418156813379737696768501404949569449468568998360536258609302577', imag='1.6514315383478467918984856362468155740989767493981537895165860394696497887014141343667909899537884056395702350747026546562733265109375856539392673279344881902350922366834548341851697831181477919975948444729663209099981840587385080630428356381678117032932339530553325258176922470913654489755988632878323169770619290386848563457060349148128329844250473292037978902121863163383730527109398916333534187029548420076238746992169045499493536414730666064500610863016047151164947128937720208658786221081333013834e-556')"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c = 1/2*(mp.sqrt(-4*a + 1) - 1)/(a**2 - a)\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0f20a3f9-2f69-4832-a6c3-bd258e167fdb",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ebbcdcda-f96d-4635-9a24-b7e9bf7f8eb7",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment