Python Gaussian Quadrature and the Trapezoid rule Integral error












0














QUESTION DETAILS



Been trying to do this python. Question details are in the image link



Am getting this error when running the program



Any help is appreciated



   File "C:/Users/User/Desktop/Python/tapez.py", line 9, in exactIntegral
integral = ((4(math.cos(0.4*b)** 2)) * ((5(math.exp( -0.5 * b))) +

TypeError: 'int' object is not callable


Here is the code



import math
import numpy
import scipy.integrate.quadrature as quad

def fun(x):
return x*numpy.sin(x)

def exactIntegral(a, b):
integral = (((9)+4(math.cos(0.4*b)** 2)) * ((5(math.exp( -0.5 * b))) +
(2(math.exp( 0.15 * b)))))+ (((9)+4(math.cos(0.4*a)** 2)) *
((5(math.exp( -0.5 * a))) + (2(math.exp( 0.15 * a)))))

return integral

a = 0.0
b = 8.0

exact = exactIntegral(a, b)
estimate = quad(fun,a,b)
print("Gaussian Quadrature: ", exact)

# Trapazoid Rule
n = 100
h = (b-a)/(n-1)
x = numpy.linspace(a,b,num=n)
area = 0
for i in range(n-1):
area = area + h*(fun(x[i]) + fun(x[i+1]))/2.0
print("Trapazoid rule: ", area)









share|improve this question


















  • 2




    5(math.exp should probably be 5 * (math.exp, same with other places where you omitted the *
    – Mike Scotty
    Nov 12 at 18:57


















0














QUESTION DETAILS



Been trying to do this python. Question details are in the image link



Am getting this error when running the program



Any help is appreciated



   File "C:/Users/User/Desktop/Python/tapez.py", line 9, in exactIntegral
integral = ((4(math.cos(0.4*b)** 2)) * ((5(math.exp( -0.5 * b))) +

TypeError: 'int' object is not callable


Here is the code



import math
import numpy
import scipy.integrate.quadrature as quad

def fun(x):
return x*numpy.sin(x)

def exactIntegral(a, b):
integral = (((9)+4(math.cos(0.4*b)** 2)) * ((5(math.exp( -0.5 * b))) +
(2(math.exp( 0.15 * b)))))+ (((9)+4(math.cos(0.4*a)** 2)) *
((5(math.exp( -0.5 * a))) + (2(math.exp( 0.15 * a)))))

return integral

a = 0.0
b = 8.0

exact = exactIntegral(a, b)
estimate = quad(fun,a,b)
print("Gaussian Quadrature: ", exact)

# Trapazoid Rule
n = 100
h = (b-a)/(n-1)
x = numpy.linspace(a,b,num=n)
area = 0
for i in range(n-1):
area = area + h*(fun(x[i]) + fun(x[i+1]))/2.0
print("Trapazoid rule: ", area)









share|improve this question


















  • 2




    5(math.exp should probably be 5 * (math.exp, same with other places where you omitted the *
    – Mike Scotty
    Nov 12 at 18:57
















0












0








0







QUESTION DETAILS



Been trying to do this python. Question details are in the image link



Am getting this error when running the program



Any help is appreciated



   File "C:/Users/User/Desktop/Python/tapez.py", line 9, in exactIntegral
integral = ((4(math.cos(0.4*b)** 2)) * ((5(math.exp( -0.5 * b))) +

TypeError: 'int' object is not callable


Here is the code



import math
import numpy
import scipy.integrate.quadrature as quad

def fun(x):
return x*numpy.sin(x)

def exactIntegral(a, b):
integral = (((9)+4(math.cos(0.4*b)** 2)) * ((5(math.exp( -0.5 * b))) +
(2(math.exp( 0.15 * b)))))+ (((9)+4(math.cos(0.4*a)** 2)) *
((5(math.exp( -0.5 * a))) + (2(math.exp( 0.15 * a)))))

return integral

a = 0.0
b = 8.0

exact = exactIntegral(a, b)
estimate = quad(fun,a,b)
print("Gaussian Quadrature: ", exact)

# Trapazoid Rule
n = 100
h = (b-a)/(n-1)
x = numpy.linspace(a,b,num=n)
area = 0
for i in range(n-1):
area = area + h*(fun(x[i]) + fun(x[i+1]))/2.0
print("Trapazoid rule: ", area)









share|improve this question













QUESTION DETAILS



Been trying to do this python. Question details are in the image link



Am getting this error when running the program



Any help is appreciated



   File "C:/Users/User/Desktop/Python/tapez.py", line 9, in exactIntegral
integral = ((4(math.cos(0.4*b)** 2)) * ((5(math.exp( -0.5 * b))) +

TypeError: 'int' object is not callable


Here is the code



import math
import numpy
import scipy.integrate.quadrature as quad

def fun(x):
return x*numpy.sin(x)

def exactIntegral(a, b):
integral = (((9)+4(math.cos(0.4*b)** 2)) * ((5(math.exp( -0.5 * b))) +
(2(math.exp( 0.15 * b)))))+ (((9)+4(math.cos(0.4*a)** 2)) *
((5(math.exp( -0.5 * a))) + (2(math.exp( 0.15 * a)))))

return integral

a = 0.0
b = 8.0

exact = exactIntegral(a, b)
estimate = quad(fun,a,b)
print("Gaussian Quadrature: ", exact)

# Trapazoid Rule
n = 100
h = (b-a)/(n-1)
x = numpy.linspace(a,b,num=n)
area = 0
for i in range(n-1):
area = area + h*(fun(x[i]) + fun(x[i+1]))/2.0
print("Trapazoid rule: ", area)






python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 18:54









denim

244




244








  • 2




    5(math.exp should probably be 5 * (math.exp, same with other places where you omitted the *
    – Mike Scotty
    Nov 12 at 18:57
















  • 2




    5(math.exp should probably be 5 * (math.exp, same with other places where you omitted the *
    – Mike Scotty
    Nov 12 at 18:57










2




2




5(math.exp should probably be 5 * (math.exp, same with other places where you omitted the *
– Mike Scotty
Nov 12 at 18:57






5(math.exp should probably be 5 * (math.exp, same with other places where you omitted the *
– Mike Scotty
Nov 12 at 18:57














1 Answer
1






active

oldest

votes


















4














Your issue is here:



integral = (((9)+4(math.cos(0.4*b)** 2)) * ((5(math.exp( -0.5 * b))) +
(2(math.exp( 0.15 * b)))))+ (((9)+4(math.cos(0.4*a)** 2)) *
((5(math.exp( -0.5 * a))) + (2(math.exp( 0.15 * a))))


Python does not interpret things like 2(x+1) as 2*(x+1) automatically. You need to specify the multiplication operator * explicitly.



For Python 2() is a function call of a function named 2. But 2 is an int object and it cannot be called like a function






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268387%2fpython-gaussian-quadrature-and-the-trapezoid-rule-integral-error%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    Your issue is here:



    integral = (((9)+4(math.cos(0.4*b)** 2)) * ((5(math.exp( -0.5 * b))) +
    (2(math.exp( 0.15 * b)))))+ (((9)+4(math.cos(0.4*a)** 2)) *
    ((5(math.exp( -0.5 * a))) + (2(math.exp( 0.15 * a))))


    Python does not interpret things like 2(x+1) as 2*(x+1) automatically. You need to specify the multiplication operator * explicitly.



    For Python 2() is a function call of a function named 2. But 2 is an int object and it cannot be called like a function






    share|improve this answer


























      4














      Your issue is here:



      integral = (((9)+4(math.cos(0.4*b)** 2)) * ((5(math.exp( -0.5 * b))) +
      (2(math.exp( 0.15 * b)))))+ (((9)+4(math.cos(0.4*a)** 2)) *
      ((5(math.exp( -0.5 * a))) + (2(math.exp( 0.15 * a))))


      Python does not interpret things like 2(x+1) as 2*(x+1) automatically. You need to specify the multiplication operator * explicitly.



      For Python 2() is a function call of a function named 2. But 2 is an int object and it cannot be called like a function






      share|improve this answer
























        4












        4








        4






        Your issue is here:



        integral = (((9)+4(math.cos(0.4*b)** 2)) * ((5(math.exp( -0.5 * b))) +
        (2(math.exp( 0.15 * b)))))+ (((9)+4(math.cos(0.4*a)** 2)) *
        ((5(math.exp( -0.5 * a))) + (2(math.exp( 0.15 * a))))


        Python does not interpret things like 2(x+1) as 2*(x+1) automatically. You need to specify the multiplication operator * explicitly.



        For Python 2() is a function call of a function named 2. But 2 is an int object and it cannot be called like a function






        share|improve this answer












        Your issue is here:



        integral = (((9)+4(math.cos(0.4*b)** 2)) * ((5(math.exp( -0.5 * b))) +
        (2(math.exp( 0.15 * b)))))+ (((9)+4(math.cos(0.4*a)** 2)) *
        ((5(math.exp( -0.5 * a))) + (2(math.exp( 0.15 * a))))


        Python does not interpret things like 2(x+1) as 2*(x+1) automatically. You need to specify the multiplication operator * explicitly.



        For Python 2() is a function call of a function named 2. But 2 is an int object and it cannot be called like a function







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 18:58









        vencaslac

        1,002217




        1,002217






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268387%2fpython-gaussian-quadrature-and-the-trapezoid-rule-integral-error%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine