Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using MATLAB: Make a script (int.m) that accepts a function f(x) and stepsize h

ID: 3811380 • Letter: U

Question

Using MATLAB: Make a script (int.m) that accepts a function f(x) and stepsize h then calculates the integral using the Midpoint rule, Trapezoid rule and Simpson’s rule. Run this script for h = 1, 0.1, 0.01, 0.001 and write this data to a file (int.txt). The data file should have the following header values:

Here are the formulas for midpoint, trapezoid, and simpson's rule

Please only solve using MATLAB. Thanks!

f method (x) midpoint cos(x) -3 trapezoid cos(x) 3 Simpson (x) 0.1 0.01 0.001 theoretical int (f) %error

Explanation / Answer

Solution:

% function for integeration
function fx = f(x)
fx = cos(x);
end

% h value
h=[1 0.1 0.01 0.001];
a=-3;
b=3;
% file to store result
fileID = fopen('in.txt','w');
fprintf(fileID,'h method      f(x)   a   b   theoretical   int(f)   %error ');
fprintf(fileID,'-------------------------------------------------------------- ')

fprintf('h method      f(x)   a   b   theoretical   int(f)   %error ');
fprintf('-------------------------------------------------------------- ')
%loop iteration
for i=1 : 4
   % compute mid point, trapezoid and simpson value
   %h=b-a
   M(i)=h(i)*f((a+b)/2);
   %h=b-a/2
   T(i)=h(i)*(f(a)+f(b));
   %h=b-a/6
   S(i)=h(i)*(f(a)+4*f((a+b)/2)+f(b));
   % theoretical valueis 2sin(3) or 0.10467
   Merror(i)=(0.2822 - M(i))*100;
   Terror(i)=(0.2822 - T(i))*100;
   Serror(i)=(0.2822 - S(i))*100;
   disp([num2str(h(i),'%d'),' midpoint cos(x) ',' ',num2str(a,'%d'),'   ',num2str(b,'%d'),'   0.2822','   ',num2str(M(i), '%1.5f'),'      ', num2str(Merror(i),'%d'),'%']);
   disp([num2str(h(i),'%d'),' trapezoid cos(x) ',' ',num2str(a,'%d'),'   ',num2str(b,'%d'),'   0.2822','   ',num2str(T(i), '%1.5f'),'      ', num2str(Terror(i),'%d'),'%']);
   disp([num2str(h(i),'%d'),' simpson cos(x) ',' ',num2str(a,'%d'),'   ',num2str(b,'%d'),'   0.2822','   ',num2str(S(i), '%1.5f'),'      ', num2str(Serror(i),'%d'),'%']);
   % write it into file
   fprintf(fileID,num2str(h(i),'%d'),' midpoint cos(x) ',' ',num2str(a,'%d'),'   ',num2str(b,'%d'),'   0.2822','   ',num2str(M(i), '%1.5f'),'      ', num2str(Merror(i),'%d'),'% ');
   fprintf(fileID,num2str(h(i),'%d'),' trapezoid cos(x) ',' ',num2str(a,'%d'),'   ',num2str(b,'%d'),'   0.2822','   ',num2str(T(i), '%1.5f'),'      ', num2str(Terror(i),'%d'),'% ');
   fprintf(fileID,num2str(h(i),'%d'),' simpson cos(x) ',' ',num2str(a,'%d'),'   ',num2str(b,'%d'),'   0.2822','   ',num2str(S(i), '%1.5f'),'      ', num2str(Serror(i),'%d'),'% ');
end
% clode the file
fclose(fileID);

Result:

h method      f(x)      a       b       theoretical     int(f)  

--------------------------------------------------------------                                                                                                                          

1  midpoint cos(x)  -3   3   0.2822   1.00000      -71.78%                                                                                                                                                                                              

1  trapezoid cos(x)  -3   3   0.2822   -1.97998      226.218%                                                                                                                                                                                           

1  simpson cos(x)  -3   3   0.2822   2.02002      -173.782%                                                                                                                                                                                             

0.1  midpoint cos(x)  -3   3   0.2822   0.10000      18.22%                                                                                                                                                                                             

0.1  trapezoid cos(x)  -3   3   0.2822   -0.19800      48.0198%                                                                                                                                                                                         

0.1  simpson cos(x)  -3   3   0.2822   0.20200      8.01985%                                                                                                                                                                                            

0.01  midpoint cos(x)  -3   3   0.2822   0.01000      27.22%                                                                                                                                                                                            

0.01  trapezoid cos(x)  -3   3   0.2822   -0.01980      30.2%                                                                                                                                                                                           

0.01  simpson cos(x)  -3   3   0.2822   0.02020      26.2%                                                                                                                                                                                              

0.001  midpoint cos(x)  -3   3   0.2822   0.00100      28.12%                                                                                                                                                                                           

0.001  trapezoid cos(x)  -3   3   0.2822   -0.00198      28.418%                                                                                                                                                                                        

0.001  simpson cos(x)  -3   3   0.2822   0.00202      28.018%     

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote