You are on page 1of 1

0001 // Define the function log(x)

0001 function y=my_function(x)


0002 y = log(x);
0003 endfunction
0005
0006 // Define the interval [a, b]
0007 a = 4;
0008 b = 5.2;
0009
0010 // Define the number of subintervals
0011 n = 1000; // Adjust for desired accuracy
0012
0013 // Define the width of each subinterval
0014 h = (b - a) / n;
0015
0016 // Calculate the integral using the trapezoidal rule
0017 integral = 0.5 * my_function(a) + 0.5 * my_function(b); //
Trapezoidal rule
0018 for i = 1:(n-1)
0019 integral = integral + my_function(a + i*h);
0020 end
0021
0022 integral = integral * h;
0023
0024 disp('Approximate integral using the trapezoidal rule:');
0025 disp(integral);
0026
0027 // Plot the graph of the function
0028 x = linspace(a, b, 1000);
0029 y = my_function(x);
0030 plot(x, y, color='blue');
0031 xlabel('x');
0032 ylabel('log(x)');
0033 title('Graph of log(x)');
0034 xtitle('Trapezoidal Rule');
0035
0036 // Mark the area under the curve for the integral
0037 x_integral = linspace(a, b, 100);
0038 y_integral = my_function(x_integral);
0039
patch([a, x_integral, b], [0, y_integral, 0], color=[0.8, 0.8, 0.8]);
0040 legend('log(x)', 'Approximate Integral');

You might also like