Saturday, October 29, 2022

Solving one transcendental equation via SciPy

Решение уравнения  x*np.cos(x)-(1-x)**(0.5) = 0

(.env) boris@UbuntuLTS:~/CURVE$ cat solveSci3.py

import numpy as np

import matplotlib.pyplot as plt

from scipy.optimize import fsolve

func = lambda x : (x*np.cos(x)-(1-x)**(0.5)) 

# Plot it

x = np.linspace(-5,1,100 )

plt.plot(x, func(x))

plt.xlabel("x")

plt.ylabel("expression value")

plt.grid()

plt.draw()

# Use the numerical solver to find the roots

x_initial_guess = -5

x_solution = fsolve(func, x_initial_guess)

print("The solution is x = %f" % x_solution)

print("at which the value of the expression is %f" % func(x_solution))

x_initial_guess = -3

x_solution = fsolve(func, x_initial_guess)

print("The solution is x = %f" % x_solution)

print("at which the value of the expression is %f" % func(x_solution))

x_initial_guess = 0

x_solution = fsolve(func, x_initial_guess)

print("The solution is x = %f" % x_solution)

print("at which the value of the expression is %f" % func(x_solution))


plt.show()



























Final draft

(.env) boris@UbuntuLTS:~/CURVE$ cat solveSci.py

import numpy as np

import matplotlib.pyplot as plt

from scipy.optimize import fsolve


func = lambda x : - (x*np.cos(x)-(1-x)**(0.5)) 


# Plot it

   x = np.linspace(-5,1,100 )

   fig, ax = plt.subplots()


   ax.plot(x, func(x))

   ax.hlines(y=0.0,xmin=-5, xmax=2, linewidth=2, color='r')

   ax.vlines(x=-5.0,ymin=-4, ymax=4,linewidth=2, color='r')

   ax.vlines(x=1.0,ymin=-4, ymax=4,linewidth=2, color='r')


  plt.xlabel("x")

  plt.ylabel("expression value")

  plt.grid()

  plt.draw()


# Use the numerical solver to find the roots

x_initial_guess = -5

x_solution = fsolve(func, x_initial_guess)

print("The solution is x = %f" % x_solution)

print("at which the value of the expression is %f" % func(x_solution))

ax.vlines(x=x_solution,ymin=-4, ymax=4,linewidth=2, color='blue')


x_initial_guess = -3

x_solution = fsolve(func, x_initial_guess)

print("The solution is x = %f" % x_solution)

print("at which the value of the expression is %f" % func(x_solution))

ax.vlines(x=x_solution,ymin=-4, ymax=4,linewidth=2, color='blue')


x_initial_guess = 0

x_solution = fsolve(func, x_initial_guess)

print("The solution is x = %f" % x_solution)

print("at which the value of the expression is %f" % func(x_solution))

ax.vlines(x=x_solution,ymin=-4, ymax=4,linewidth=2, color='blue')


plt.show()




















Monday, October 10, 2022

Division factorial(843) by 8

 Смотри код на Пайтон.

[boris@fedora FACTORIAL]$ cat fact.py

def recur_factorial(n):

   if n == 1:

       return n

   else:

       return n*recur_factorial(n-1)

num = 843 

rez,rez1 = 0,0

# check if the number is negative

if num < 0:

   print("Sorry, factorial does not exist for negative numbers")

elif num == 0:

   print("The factorial of 0 is 1")

else:

   rez = recur_factorial(num)

   print("The factorial of", num, "is rez = ",rez)

   print("rez has divider equal 1000 = 8*125")

   print("Thus rez1 = rez // 8 should be integer")

   rez1 = rez // 8 

   print("recur_factorial(num)//8 is rez1 = ", rez1)

[boris@fedora FACTORIAL]$ python3 fact.py

The factorial of 843 is rez =  167665541985785997664655346737318017649300484303647475316193541662648473912809115399596582641532956520148628241573718402797623458368067394176062892818023162799853857455331561951438496726020893299512227325953001279296920003926573430140190769250651106507877004239986843179453186279807599993540854710162594639020084960512386380150270012334581858605455264618455556532149285500379377276952410985041979026850254046346142553060341264056610586454566280852810458318240761651652741518282727396987359802669289860185314394528385737841317489264245063169617508312290708072728080167971483755161635826894743260671327982521822772164627038190380470595152705164687438943911000325787758048995536291774770878007406167844680923253079852993692343823045868281897257450839604077581936268272102882133201718416081590011847370441525312888399026495444988338717870741474056146961252082646027374314902748802362496278730906652290333180999747655212296956306776541923799893438312058166341763037214385518419530638177871870254791559198524948847949021812927342867826869650847577088745092231972603364844536777449733741304556897228214862725392536450932379984232899038716651680889742002230591613829060758783290146359779514370615552236943996643370789767296132407347914580735183890051799429581828347256374624828903393060242485834872713529823589352605960703325857094630617716587426558059299432416939309950187793564094074437844342657410574283079741814753628807047930182680054199809456099473939935287539467769984797908723648514517579735760508003099023676333331589814622823387428752622233909501869196728208532516916283882709109259009096341736530583447893101924345042698252404227026278255800332065111335288073343526490969242953505297914164941694538085045874566522845451942637382035828361413821535283806311083049876131944005107398908307789804341707291776627522959664287595252315377021267444714350747876269965183482190126577225931440225265909760000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

rez has divider equal 1000 = 8*125

Thus rez1 = rez // 8 should be integer

recur_factorial(num)//8 is rez1 =  20958192748223249708081918342164752206162560537955934414524192707831059239101139424949572830191619565018578530196714800349702932296008424272007861602252895349981732181916445243929812090752611662439028415744125159912115000490821678767523846156331388313484625529998355397431648284975949999192606838770324329877510620064048297518783751541822732325681908077306944566518660687547422159619051373130247378356281755793267819132542658007076323306820785106601307289780095206456592689785340924623419975333661232523164299316048217230164686158030632896202188539036338509091010020996435469395204478361842907583915997815227846520578379773797558824394088145585929867988875040723469756124442036471846359750925770980585115406634981624211542977880733535237157181354950509697742033534012860266650214802010198751480921305190664111049878311930623542339733842684257018370156510330753421789362843600295312034841363331536291647624968456901537119538347067740474986679789007270792720379651798189802441329772233983781848944899815618605993627726615917858478358706355947136093136528996575420605567097181216717663069612153526857840674067056366547498029112379839581460111217750278823951728632594847911268294972439296326944029617999580421348720912016550918489322591897986256474928697728543407046828103612924132530310729359089191227948669075745087915732136828827214573428319757412429052117413743773474195511759304730542832176321785384967726844203600880991272835006774976182012434242491910942433471248099738590456064314697466970063500387377959541666448726827852923428594077779238687733649591026066564614535485338638657376137042717066322930986637740543130337281550528378284781975041508138916911009167940811371155369188162239270617711817260630734320815355681492829672754478545176727691910475788885381234516493000638424863538473725542713411472078440369958035949406539422127658430589293843484533745647935273765822153241430028158238720000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000