# Hw 6 - Hw 6 function eval_integral(y,l,u%Problem 17.2%This...

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

Hw 6 function eval_integral(y,l,u) %Problem 17.2 % %This program is designed to pass any expression that is to be integrated %into this function and (a) solve analytically, (b) apply single %application of the trapezoidal rule (c) apply composite trapezoidal rule %with n=2 and n=4, (d) apply application of Simpson's 1/3 rule, (e) apply %composite Simpson's 1/3 rule with n=4, and (f) apply Simpson's 3/8 %rule. The percent relative errror will be calculated for steps (b)-(f). % %x is the expression that is passed into the function %by means of an anonymous function. %u=upper limit %l=lower limit if nargin<3, error( 'must enter a function, lower and upper limits' ); end %(a) Evaluate integral analytically syms x yy=int(y,x,l,u); a=eval(yy); %Actual value determined analy. fprintf( '\n(a) %10.9f\n\n' ,a); %(b) Single application of teh trapezoidal rule fb=y(u); fa=y(l); I=(u-l)*(fb+fa)/2; Et=a-I; %Error et=(Et/a)*100; %Percent relative error=(actual-theo)/act*100% fprintf( '(b) %6.5f, et=%4.2f\n\n' ,I,et); %(c) Composite trapezoidal rule, divide the integration interval from a % to b into a number of segments and apply the method to each segment. %n=2 & n=4 n=2; for i=1:2 h2=(u-l)/n; Isum_old=0; for j=1:n-1 h=h2*j; fh=y(h); Isum=fh+Isum_old; Isum_old=Isum;

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

View Full Document
end I=(u-l)*(fa+2*Isum+fb)/(n*2); Et=a-I; et=(Et/a)*100; if i==2 fprintf( ' n=%d,%6.5f, et=%4.2f\n\n' ,n,I,et); break end fprintf( '(c) n=%d,%6.5f, et=%4.2f\n\n' ,n,I,et); n=n+2; end %(d) Single appplication of Simpson's 1/3 rule, taking area under parabola %connecting three points. h=(u-l)/2; fh=y(h); I=h/3*(fa+4*fh+fb); et=((a-I)/a)*100; fprintf( '(d) %6.5f, et=%4.2f\n\n' ,I,et); %(e) Composite Simpson's 1/3 rule with n=4 n=4; feven_old=0; fodd_old=0; h=(u-l)/n; for i=1:n-1 if rem(i,2)==0 %Determines if i is an even number feven=y(i); sum1=feven+feven_old; feven_old=sum1; else %else, i is an odd number fodd=y(i); sum2=fodd+fodd_old; fodd_old=sum2; end end
I=(u-l)*(fa+4*sum2+2*sum1+fb)/(3*n); et=((a-I)/a)*100; fprintf( '(e) %6.5f, et=%4.2f\n\n' ,I,et); % (f) Simpson's 3/8 rule, consists of taking the area under a cubic % equation connecting four points. Single application requires four equally % spaced points ---> u/3 satisfies this. h=(u-l)/3; I=(3*h)/8*(fa+3*y(h)+3*y(h*2)+y(h*3)); et=((a-I)/a)*100; fprintf( '(f) %6.5f, et=%4.2f\n\n' ,I,et); end Function run in matlab EDU>> [email protected](x) 1-exp(-2*x); EDU>> eval_integral(y,0,4) (a) 3.500167731 (b) 1.99933, et=42.88 (c) n=2,2.96303, et=15.35 n=4,3.34370, et=4.47 (d) 3.28427, et=6.17 (e) 3.47059, et=0.84 (f) 3.38837, et=3.19

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

View Full Document
function spaced_data(y,a,b,xx,fx) %Problem 17.5 % %y=anonymous function to be integrated %a=lower limit, b=upper limit %xx=vector of spaced data %fx=cooresponding values for the elements of xx if nargin~=5,error( ); end % (a) Analytical means syms x %Integral with respect to x. yy=int(y,x,a,b);
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 14

Hw 6 - Hw 6 function eval_integral(y,l,u%Problem 17.2%This...

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

View Full Document
Ask a homework question - tutors are online