{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

solution_hw11

# solution_hw11 - function[f]=NewtonCoates(fun,xl,xu,n,tp...

This preview shows pages 1–5. Sign up to view the full content.

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

View Full Document

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

View Full Document
function [f]=NewtonCoates(fun,xl,xu,n,tp) h=(xu-xl)/n; x=xl:h:xu; for i=1:length(x) fx(i)=feval(fun,x(i)); end f=0; if (tp==1) %trapezoid integration f=fx(1)+fx(n+1); for i=2:n f=f+fx(i)*2; end f=f*h/2; elseif (tp==2) % Simpson rule if (mod(n,2)==0) %1/3 rule f=fx(1)+fx(n+1); for i=2:n if (mod(i,2)==0) f=f+fx(i)*4; else f=f+fx(i)*2; nd e end f=f*h/3; else % combined 1/3 and 3/8 rule m=n-3; f=fx(1)+fx(m+1); for i=2:m if (mod(i,2)==0) f=f+fx(i)*4; else f=f+fx(i)*2; end end f=f*h/3; % the 1/3 rule part f=f+3*h/8*(fx(n+1)+3*fx(n)+3*fx(n-1)+fx(n-2)); % the 3/8 part nd e end -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- function [f]=test(x) f=2/(1+2*x^2); We can also write a program to carry out the comparison required in the homework, but if you did it manually and didn’t present the figure in your homework, there is also no penalty.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 5

solution_hw11 - function[f]=NewtonCoates(fun,xl,xu,n,tp...

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

View Full Document
Ask a homework question - tutors are online