Please translate the following matlab code to python: function z=Trapetsiy(eps) a=-1;…
- User
Please translate the following matlab code to python: function z=Trapetsiy(eps) a=-1; b=1; d=b-a; disp('Длина отрезка равняется:'); disp(d); R1=@(x)+(4*(x)^(3)+2*(x)^(2)-4x+2)^(sqrt(2)); R2=@(x)+asin(1/(-(x)^(2)+x+5)); disp('Эпсилон равняется:'); disp(eps); err=inf; n=5; I=inf; tic; while abs(err)>abs(eps) n=n2; disp('Число n равняется:'); disp(n); h=d/n; disp('Шаг интегрирования:'); disp(h); Iold=I; I=0; x=a; for i=1:n x=x+h; f1=R1(x)+R2(x)-5; f2=R1(x-h)+R2(x-h)-5; I=I+(f1+f2); end I=I*h/2; disp('Интеграл равняется:'); disp(I) err=abs(I-Iold); disp('Ошибка равняется:'); disp(err); end toc; z=I;
- Assistant
- User
Please translate the following matlab code to python: function z=Simpson(eps) a=-1; b=1; d=b-a; disp('Длина отрезка равняется:'); disp(d); R1=@(x)+(4*(x)^(3)+2*(x)^(2)-4x+2)^(sqrt(2)); R2=@(x)+asin(1/(-(x)^(2)+x+5)); disp('Эпсилон равняется:'); disp(eps); err=inf; I=inf; n=5; tic; while abs(err)>abs(eps) n=n2; disp('Число n равняется:'); disp(n); h=d/n; disp('Шаг интегрирования:'); disp(h); Iold=I; I=0; x=a; for i=1:n x=x+h; xpol=x-0.5h; f1=R1(x-h)+R2(x-h)-5; fpol=R1(xpol)+R2(xpol)-5; f2=R1(x)+R2(x)-5; I=I+(f1+4fpol+f2); end I=I*h/6; disp('Интеграл равняется:'); disp(I) err=abs(I-Iold); disp('Ошибка равняется:'); disp(err); end toc; z=I;
- Assistant
Below is the translation of the given MATLAB code implementing Simpson's rule for numerical integration into Python:
python