{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ps10solns

# ps10solns - %Engineering 6 Problem 10.1 Solution(Numerical...

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

%Engineering 6, Problem 10.1 Solution %(Numerical integration) %Suppress extra lines in output and set fixed short display format compact; format short; clc; %Clear command window %Trapezoidal method for integral of 1/x from x=2 to x=5 disp( 'Integral of 1/x from x=2 to x=5 using trapezoidal integration:' ); disp( ' ' ); %Three subintervals (i.e., 4 values of x, including 2 and 5) x = linspace(2,5,4); y = 1./x; result = trapz(x,y); %Convert result to a string and display everything on one line % using the disp command. (Note the square brackets.) disp([ 'Result for 3 subintervals is: ' ,num2str(result)]); disp( ' ' ); %Six subintervals (i.e., 7 values of x, including 2 and 5) x = linspace(2,5,7); y = 1./x; result = trapz(x,y); disp([ 'Result for 6 subintervals is: ' ,num2str(result)]); disp( ' ' ); %Twelve subintervals (i.e., 13 values of x, including 2 and 5) x = linspace(2,5,13); y = 1./x; result = trapz(x,y); disp([ 'Result for 12 subintervals is: ' ,num2str(result)]); disp( ' ' ); %Twenty-four subintervals (i.e., 25 values of x, including 2 and 5) x = linspace(2,5,25); y = 1./x; result = trapz(x,y); disp([ 'Result for 24 subintervals is: ' ,num2str(result)]); disp( ' ' ); %Forty-eight subintervals (i.e., 49 values of x, including 2 and 5) x = linspace(2,5,49); y = 1./x; result = trapz(x,y); disp([ 'Result for 48 subintervals is: ' ,num2str(result)]); disp( ' ' ); disp( ' ' );

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

View Full Document
%Simpson's method for integral of 1/x from x=2 to x=5. % Note: we first have to define a user-defined function for 1/x % in a separate file. Here we have named that file one_over_x. % The code for the function file is shown below.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}