I am aware matlab is unavailable for most, so please just dont answer the questi
ID: 3807157 • 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.3 pi x), element [-5,0] x^3 - 0.23x + 30.67, element [0,5]Explanation / Answer
Matlab code
% Trapezoidal rule
f1 = @(x) 1000*exp(7*x).*cos(0.3*pi*x); % f1 for x in [-5,0]
f2 = @(x) x.^3-0.23*x+30.67; % f2 for x in [0 5]
% Split the intervels in to two part and find the integration seperatly
N = 50; % Number of sub intervels
a = -5;b = 5; % Integration limit
x1 = linspace(a,0,N); % Creating x vector for first integration
x2 = linspace(0,b,N); % Creating x vector for second integration
TI = -a*sum([2*f1(x1(2:N-1)),f1([x1(1),x1(N)])])/(2*N)+... % integraing first part
b*sum([2*f2(x2(2:N-1)),f2([x2(1),x2(N)])])/(2*N);
% Simpson's rule
SI = (x1(2)-x1(1))*sum([4*f1(x1(2:2:N-1)),2*f1(x1(3:2:N-1)),f1([x1(1),x1(N)])])/(3)+... % integraing first part
(x2(2)-x2(1))*sum([4*f2(x2(2:2:N-1)),2*f2(x2(3:2:N-1)),f2([x2(1),x2(N)])])/(3);
fprintf('The Result using Trapezoidal rule = %f ',TI);
fprintf('The Result using Simpsons rule = %f ',SI);
OUTPUT
The Result using Trapezoidal rule = 444.066582
The Result using Simpsons rule = 419.764500
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.