Thursday, January 9, 2020

ЛР10 Программирование (1.7 В21) - IV

LabWork10
📑 IT Online Mini Campus. Task 1.7
IV. Программа SageMath (Python)
E=10^(-4); A,B=E,2-E;
def find_root_div2(f,a,b,e,var):
    pretty_print(html(latex(f)))
    if sign(f(a))==sign(f(b)):
        s='It is not possible to use '
        s+='this decision method for the segment'
        print(s)
    else:
        print('Iteration steps'); t=[['f(a)','f((a+b)/2)','f(b)']]
        while b-a>=e:            
            ab=a+(b-a)/2; t+=[[f(a).n(),f(ab).n(),f(b).n()]]
            if sign(f(a))!=sign(f(ab)):
                if f(ab)==0: break 
                b=ab
            else: a=ab
        show(table(t))
        print('var %d f(x)=0 =>\nprogram answer: x≈%.10f'%(var,ab))
        print('SageMath answer: x≈%.10f'%find_root(f21(x),0,2))
f21(x)=log(20/sqrt(.009))-1.5*(exp(x/3)+exp(-x/3))-x
find_root_div2(f21,A,B,E,21)


No comments:

Post a Comment