I am aware matlab is unavailable for most, so please just dont answer the questi
ID: 3807182 • Letter: I
Question
I am aware matlab is unavailable for most, so please just dont answer the question if you dont have access to it... its that simple. I am only asking for a code to integrate this piecewise function (specifically using the simpsons rule and trapezoidal rule on matlab!!) you dont have to run the actual code ... you just need to provide one. How would I code this integration for MATLAB only, using BOTH TRAPEZOIDAL AND SIMPSONS RULE integration. Please dont reply unless have every aspect of question. F(x) = {1000 * e^(7x) * cos(0.3pix), Elementof [-5, 0] x^3 - 0.23x + 30.67, Elementof [0, 5]Explanation / Answer
Code for Trapezoidal Rule:
functional integral = cmptrap(a,b,n,f)
h = (b-a)/n;
x = [a+h:h:b-h];
integral = h/2*(2*sum(feval(f,x))+feval(f,a)+feval(f,b));
cmptrap(1,2,4,’f’)
where ’f’ is the name of the function definition file
function y = f(t)
y = t.*log(t); % pay attention to the dot
Matlab code for the Simpson’s rule -
function integral = cmpsimp(a,b,n,f)
h = (b-a)/n;
xi0 = feval(’f’,a)+feval(’f’,b);
xi1 = 0;
xi2 = 0;
for i = 1:n-1
x = a+i*h;
if mod(i,2) == 0
xi2 = xi2+feval(’f’,x);
else
xi1 = xi1+feval(’f’,x);
end
end
xi = h*(xi0+2*xi2+4*xi1)/3;
xi
As per given code,we can generate for both integration.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.