How to convert it from matlab to python? Function value_integral = boole (fun, a
ID: 3860975 • Letter: H
Question
How to convert it from matlab to python?
Function value_integral = boole (fun, a, b, n) if a > = b %error function error ('the integral lower bound a is greater or equal than integral upper bound b'): end if mod(n, 4) ~= 0 %error function error ('the number of subintervals n is not divisible by 4'): end h = (b-a)/n: sum=0.0: x=a: y=a: y=fun (a): integral=0: integral=0: for i=1: n/4 %boole's rule function x(i+1, :)=i*h: y(i+1, :)=fun (i*h): sum=sum+7*fun (a+4*i*h-4*h) + 32*fun (a+4*i*h-3*h_ + 12*fun(a+4*i*h) + 32*fun (a+4*i*h) + fun(a+4*i*h): integral (i+1, :)=2*h/45*sum: end value_integral=2*h/45*sum: plot (x, y, ' -b'): %ploting graph grid on: hold on: plot (x, integral, '--rx'): xlabel ('x'): ylabel ('f(x)'): close allExplanation / Answer
@mfunction("value_integral") def boole(fun=None, a=None, b=None, n=None): if a >= b: #error function error(mstring('the integral lower bound a is greated or equal than integral upper bound')) end if mod(n, 4) != 0: #error function error(mstring('the number of subintervals n is not divisible by 4')) end h = (b - a) / n sum = 0.0 x = a y = fun(a) integral = 0 for i in mslice[1:n / 4]: #boole's rule function x(i + 1, mslice[:]).lvalue = i * h y(i + 1, mslice[:]).lvalue = fun(i * h) sum = sum + 7 * fun(a + 4 * i * h - 4 * h) + 32 * fun(a + 4 * i * h - 3 * h) + 12 * fun(a + 4 * i * h - 2 * h) + 32 * fun(a + 4 * i * h - h) + 7 * fun(a + 4 * i * h) integral(i + 1, mslice[:]).lvalue = 2 * h / 45 * sum end value_integral = 2 * h / 45 * sum plot(x, y, mstring('-b')) #ploting graph grid(mstring('on')) hold(mstring('on')) plot(x, integral, mstring('--rx')) xlabel(mstring('x')) ylabel(mstring('f(x)')) close(mstring('all'))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.