Homework 3

Homework 3 - 8. A) > %This program uses the Composite...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
8. A) >> %This program uses the Composite Trapezoidal Rule to approximate the integral% >> x = sym('x'); >> f = x^2*exp(-x^2); >> a=0; b=2; n=8; >> h = (b-a)/n; >> trapsum = vpa(subs(f,x,a)) + vpa(subs(f,x,b)); >> for i=1:n-1, trapsum = trapsum + 2*vpa(subs(f,x,a+i*h)); end >> trapsum = .5*h*trapsum trapsum = .42158203719810195819750475720865 8. B) >> %This is a program that calculates the approximation of an integral using Simpson's% >> a=0; b=2; n=8; >> a=0; b=2; n=8; >> h = (b-a)/n; >> x = sym('x'); >> f = x^2*exp(-x^2); >> simpsum = vpa(subs(f,x,a)) + 4*vpa(subs(f,x,a+h))+vpa(subs(f,x,b)); >> for i = 1:(n-2)/2, simpsum = simpsum+4*vpa(subs(f,x,a+(2*i+1)*h)); simpsum = simpsum+2*vpa(subs(f,x,a+2*i*h)); end >> simpsum = h * simpsum/3 simpsum = .42271618793397650049037395092453
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8. C) >> %This is a program that calculates the approximation of an integral using the Midpoint Rule% >> a=0; b=2; n=8; >> h = (b-a)/n; >> x = sym('x'); >> f = x^2*exp(-x^2); >> midsum = vpa(subs(f,x,a)) + vpa(subs(f,x,b));
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

Homework 3 - 8. A) > %This program uses the Composite...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online