but this be tedious. Instead, we note that we are evaluating the function at five and nine points on the interval [0, 4]: xs2 = linspace( 0, 4, 5 ); fxs2 = f62( xs2 ); xs3 = linspace( 0, 4, 9 ); fxs3 = f62( xs3 ); Now, we can factor out 0.5 and all the products are the same, so essentially, these simplify to: a2 = 0.5*(fxs2(1) + 2*sum(fxs2(2:end-1)) + fxs2(end))*(4 - 0)/4 a3 = 0.5*(fxs3(1) + 2*sum(fxs3(2:end-1)) + fxs3(end))*(4 - 0)/8 where = 1 and = 0.5, respectively. h h We must now implement this as a function: function intf = comp_trap( f, ab, h ) ab where is a row vector of two values: [a, The first step of the function is to assign b]. a b, these two entries to the variables and respectively. The next step is to determine from recall that and, therefore, . However, we have a n h: problem with numerical calculations: it be that the result is not exactly an integer. Thus, it would be wise to use the round function: n = round( (b - a)/h + 1 ); Next, we must determine linearly spaced points between and n a b. Then, evaluate the function at each of those points. n Finally, calculate and return . function intf = comp_trap( f, ab, h ) % enter your implemention here end These pairs of Matlab commands should produce identical results: xs2 = linspace( 0, 4, 5 ); f622 = f62( xs2 ); a2 = 0.5*(f622(1) + 2*sum(f622(2:end-1)) + f622(end))*(4 - 0)/4
but this be tedious. Instead, we note that we are evaluating the function at five and nine points on the interval [0, 4]: xs2 = linspace( 0, 4, 5 ); fxs2 = f62( xs2 ); xs3 = linspace( 0, 4, 9 ); fxs3 = f62( xs3 ); Now, we can factor out 0.5 and all the products are the same, so essentially, these simplify to: a2 = 0.5*(fxs2(1) + 2*sum(fxs2(2:end-1)) + fxs2(end))*(4 - 0)/4 a3 = 0.5*(fxs3(1) + 2*sum(fxs3(2:end-1)) + fxs3(end))*(4 - 0)/8 where h = 1 and h = 0.5, respectively. We must now implement this as a function: function intf = comp_trap( f, ab, h ) where ab is a row vector of two values: [a, b]. The first step of the function is to assign these two entries to the variables a and b, respectively. The next step is to determine n from h: recall that and, therefore, . However, we have a problem with numerical calculations: it be that the result is not exactly an integer. Thus, it would be wise to use the round function: n = round( (b - a)/h + 1 ); Next, we must determine n linearly spaced points between a and b. Then, evaluate the function at each of those n points. Finally, calculate and return . function intf = comp_trap( f, ab, h ) % enter your implemention here end These pairs of Matlab commands should produce identical results: xs2 = linspace( 0, 4, 5 ); f622 = f62( xs2 ); a2 = 0.5*(f622(1) + 2*sum(f622(2:end-1)) + f622(end))*(4 - 0)/4
ECE.204A.PreLab.7.uwuserid.docx