How to integrate Simpsons rule using Scipy to plot a 1D graph












1















i need some help, i have an assignment to code an integration of a function using simpsons rule. I need to use the inbuilt scipy integratesimps function to plot a 1D graph. I just don't know where to start. I think i have to get a list/array of each value of y for the function that corresponds to each values of x: e.g



if my function is x^2
then when
x is 0 y is 0,
x is 1 y is 1,
x is 2 y is 4,
and so on up to a huge limit...



and then use integrate.simps(y,x) where y are all the y values as shown above and x are all the corresponding x values.



However, i can't get it to work at all...has anyone got any examples of a graph plot for a function of x^2 using integrate.simps(y,x)?



here is what i've got so far:



import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt

x = np.linspace(-10,10,N)
N = 100

yarray =

def f(x):
return x**2

for i in x :
y = f(i)
yarray.append(y)

print(yarray)


E = integrate.simps(yarray,x)
print(E)

plt.plot(x,E)









share|improve this question























  • What do you mean by plotting a 1D graph, are you trying to plot x^2, the integration of x^2, or something else?

    – William Lee
    Nov 20 '18 at 1:22











  • i'm trying to plot the integration of x^2, using the integrate.simps()

    – M.B
    Nov 20 '18 at 1:49
















1















i need some help, i have an assignment to code an integration of a function using simpsons rule. I need to use the inbuilt scipy integratesimps function to plot a 1D graph. I just don't know where to start. I think i have to get a list/array of each value of y for the function that corresponds to each values of x: e.g



if my function is x^2
then when
x is 0 y is 0,
x is 1 y is 1,
x is 2 y is 4,
and so on up to a huge limit...



and then use integrate.simps(y,x) where y are all the y values as shown above and x are all the corresponding x values.



However, i can't get it to work at all...has anyone got any examples of a graph plot for a function of x^2 using integrate.simps(y,x)?



here is what i've got so far:



import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt

x = np.linspace(-10,10,N)
N = 100

yarray =

def f(x):
return x**2

for i in x :
y = f(i)
yarray.append(y)

print(yarray)


E = integrate.simps(yarray,x)
print(E)

plt.plot(x,E)









share|improve this question























  • What do you mean by plotting a 1D graph, are you trying to plot x^2, the integration of x^2, or something else?

    – William Lee
    Nov 20 '18 at 1:22











  • i'm trying to plot the integration of x^2, using the integrate.simps()

    – M.B
    Nov 20 '18 at 1:49














1












1








1








i need some help, i have an assignment to code an integration of a function using simpsons rule. I need to use the inbuilt scipy integratesimps function to plot a 1D graph. I just don't know where to start. I think i have to get a list/array of each value of y for the function that corresponds to each values of x: e.g



if my function is x^2
then when
x is 0 y is 0,
x is 1 y is 1,
x is 2 y is 4,
and so on up to a huge limit...



and then use integrate.simps(y,x) where y are all the y values as shown above and x are all the corresponding x values.



However, i can't get it to work at all...has anyone got any examples of a graph plot for a function of x^2 using integrate.simps(y,x)?



here is what i've got so far:



import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt

x = np.linspace(-10,10,N)
N = 100

yarray =

def f(x):
return x**2

for i in x :
y = f(i)
yarray.append(y)

print(yarray)


E = integrate.simps(yarray,x)
print(E)

plt.plot(x,E)









share|improve this question














i need some help, i have an assignment to code an integration of a function using simpsons rule. I need to use the inbuilt scipy integratesimps function to plot a 1D graph. I just don't know where to start. I think i have to get a list/array of each value of y for the function that corresponds to each values of x: e.g



if my function is x^2
then when
x is 0 y is 0,
x is 1 y is 1,
x is 2 y is 4,
and so on up to a huge limit...



and then use integrate.simps(y,x) where y are all the y values as shown above and x are all the corresponding x values.



However, i can't get it to work at all...has anyone got any examples of a graph plot for a function of x^2 using integrate.simps(y,x)?



here is what i've got so far:



import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt

x = np.linspace(-10,10,N)
N = 100

yarray =

def f(x):
return x**2

for i in x :
y = f(i)
yarray.append(y)

print(yarray)


E = integrate.simps(yarray,x)
print(E)

plt.plot(x,E)






python scipy numerical-methods integral simpsons-rule






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 0:55









M.BM.B

82




82













  • What do you mean by plotting a 1D graph, are you trying to plot x^2, the integration of x^2, or something else?

    – William Lee
    Nov 20 '18 at 1:22











  • i'm trying to plot the integration of x^2, using the integrate.simps()

    – M.B
    Nov 20 '18 at 1:49



















  • What do you mean by plotting a 1D graph, are you trying to plot x^2, the integration of x^2, or something else?

    – William Lee
    Nov 20 '18 at 1:22











  • i'm trying to plot the integration of x^2, using the integrate.simps()

    – M.B
    Nov 20 '18 at 1:49

















What do you mean by plotting a 1D graph, are you trying to plot x^2, the integration of x^2, or something else?

– William Lee
Nov 20 '18 at 1:22





What do you mean by plotting a 1D graph, are you trying to plot x^2, the integration of x^2, or something else?

– William Lee
Nov 20 '18 at 1:22













i'm trying to plot the integration of x^2, using the integrate.simps()

– M.B
Nov 20 '18 at 1:49





i'm trying to plot the integration of x^2, using the integrate.simps()

– M.B
Nov 20 '18 at 1:49












1 Answer
1






active

oldest

votes


















1














Basically, you need to calculate the integral value for every range of x, from [-10,-10] to [-10,10]



This example code plots



enter image description here



import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt

def f(x):
return x**2

N = 100
x = np.linspace(-10,10,N)


integrals =
x_range =
y_range =
for i in x:
x_range.append(i)
y_range.append(f(i))
integral = integrate.simps(y_range, x_range)
integrals.append(integral)

plt.plot(x, integrals)
plt.show()


To wrap it up



import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt

def integrals(f, xs):
x_range =
y_range =
results =
for x in xs:
x_range.append(x)
y_range.append(f(x))
integral = integrate.simps(y_range, x_range)
results.append(integral)
return results

def f(x, b):
return (x-b)**2

xs = np.linspace(-10, 10, 100)

plt.plot(xs, integrals(lambda x: f(x, 0), xs), label='b=0')
plt.plot(xs, integrals(lambda x: f(x, 2), xs), label='b=2')
plt.plot(xs, integrals(lambda x: f(x, 4), xs), label='b=4')
plt.title('$y(x) = int_{-10}^{x}(t-b)^2dt$')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()


and you getenter image description here






share|improve this answer


























  • Thanks! that works really well, what happens if i have a variable in my function? e.g (x-b)^2 where b is changing

    – M.B
    Nov 20 '18 at 11:50











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%2f53384732%2fhow-to-integrate-simpsons-rule-using-scipy-to-plot-a-1d-graph%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









1














Basically, you need to calculate the integral value for every range of x, from [-10,-10] to [-10,10]



This example code plots



enter image description here



import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt

def f(x):
return x**2

N = 100
x = np.linspace(-10,10,N)


integrals =
x_range =
y_range =
for i in x:
x_range.append(i)
y_range.append(f(i))
integral = integrate.simps(y_range, x_range)
integrals.append(integral)

plt.plot(x, integrals)
plt.show()


To wrap it up



import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt

def integrals(f, xs):
x_range =
y_range =
results =
for x in xs:
x_range.append(x)
y_range.append(f(x))
integral = integrate.simps(y_range, x_range)
results.append(integral)
return results

def f(x, b):
return (x-b)**2

xs = np.linspace(-10, 10, 100)

plt.plot(xs, integrals(lambda x: f(x, 0), xs), label='b=0')
plt.plot(xs, integrals(lambda x: f(x, 2), xs), label='b=2')
plt.plot(xs, integrals(lambda x: f(x, 4), xs), label='b=4')
plt.title('$y(x) = int_{-10}^{x}(t-b)^2dt$')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()


and you getenter image description here






share|improve this answer


























  • Thanks! that works really well, what happens if i have a variable in my function? e.g (x-b)^2 where b is changing

    – M.B
    Nov 20 '18 at 11:50
















1














Basically, you need to calculate the integral value for every range of x, from [-10,-10] to [-10,10]



This example code plots



enter image description here



import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt

def f(x):
return x**2

N = 100
x = np.linspace(-10,10,N)


integrals =
x_range =
y_range =
for i in x:
x_range.append(i)
y_range.append(f(i))
integral = integrate.simps(y_range, x_range)
integrals.append(integral)

plt.plot(x, integrals)
plt.show()


To wrap it up



import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt

def integrals(f, xs):
x_range =
y_range =
results =
for x in xs:
x_range.append(x)
y_range.append(f(x))
integral = integrate.simps(y_range, x_range)
results.append(integral)
return results

def f(x, b):
return (x-b)**2

xs = np.linspace(-10, 10, 100)

plt.plot(xs, integrals(lambda x: f(x, 0), xs), label='b=0')
plt.plot(xs, integrals(lambda x: f(x, 2), xs), label='b=2')
plt.plot(xs, integrals(lambda x: f(x, 4), xs), label='b=4')
plt.title('$y(x) = int_{-10}^{x}(t-b)^2dt$')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()


and you getenter image description here






share|improve this answer


























  • Thanks! that works really well, what happens if i have a variable in my function? e.g (x-b)^2 where b is changing

    – M.B
    Nov 20 '18 at 11:50














1












1








1







Basically, you need to calculate the integral value for every range of x, from [-10,-10] to [-10,10]



This example code plots



enter image description here



import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt

def f(x):
return x**2

N = 100
x = np.linspace(-10,10,N)


integrals =
x_range =
y_range =
for i in x:
x_range.append(i)
y_range.append(f(i))
integral = integrate.simps(y_range, x_range)
integrals.append(integral)

plt.plot(x, integrals)
plt.show()


To wrap it up



import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt

def integrals(f, xs):
x_range =
y_range =
results =
for x in xs:
x_range.append(x)
y_range.append(f(x))
integral = integrate.simps(y_range, x_range)
results.append(integral)
return results

def f(x, b):
return (x-b)**2

xs = np.linspace(-10, 10, 100)

plt.plot(xs, integrals(lambda x: f(x, 0), xs), label='b=0')
plt.plot(xs, integrals(lambda x: f(x, 2), xs), label='b=2')
plt.plot(xs, integrals(lambda x: f(x, 4), xs), label='b=4')
plt.title('$y(x) = int_{-10}^{x}(t-b)^2dt$')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()


and you getenter image description here






share|improve this answer















Basically, you need to calculate the integral value for every range of x, from [-10,-10] to [-10,10]



This example code plots



enter image description here



import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt

def f(x):
return x**2

N = 100
x = np.linspace(-10,10,N)


integrals =
x_range =
y_range =
for i in x:
x_range.append(i)
y_range.append(f(i))
integral = integrate.simps(y_range, x_range)
integrals.append(integral)

plt.plot(x, integrals)
plt.show()


To wrap it up



import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt

def integrals(f, xs):
x_range =
y_range =
results =
for x in xs:
x_range.append(x)
y_range.append(f(x))
integral = integrate.simps(y_range, x_range)
results.append(integral)
return results

def f(x, b):
return (x-b)**2

xs = np.linspace(-10, 10, 100)

plt.plot(xs, integrals(lambda x: f(x, 0), xs), label='b=0')
plt.plot(xs, integrals(lambda x: f(x, 2), xs), label='b=2')
plt.plot(xs, integrals(lambda x: f(x, 4), xs), label='b=4')
plt.title('$y(x) = int_{-10}^{x}(t-b)^2dt$')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()


and you getenter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 12:15

























answered Nov 20 '18 at 4:01









William LeeWilliam Lee

187110




187110













  • Thanks! that works really well, what happens if i have a variable in my function? e.g (x-b)^2 where b is changing

    – M.B
    Nov 20 '18 at 11:50



















  • Thanks! that works really well, what happens if i have a variable in my function? e.g (x-b)^2 where b is changing

    – M.B
    Nov 20 '18 at 11:50

















Thanks! that works really well, what happens if i have a variable in my function? e.g (x-b)^2 where b is changing

– M.B
Nov 20 '18 at 11:50





Thanks! that works really well, what happens if i have a variable in my function? e.g (x-b)^2 where b is changing

– M.B
Nov 20 '18 at 11:50


















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384732%2fhow-to-integrate-simpsons-rule-using-scipy-to-plot-a-1d-graph%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

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]