using a riemann sum, compute the value of ln(5.6), using the definition of the ln function as an integral. compare your result with the true value of ln(5.6) in matlab

Using the definition of the natural logarithmic function, we have:

ln(x) = ∫(1, x) dt/t where 1<= x

We want to evaluate ln(5.6) using riemann sum

Let n be the number of sub-intervals, then the width of each sub-interval is ∆x = (5.6 - 1)/n = 4.6/n

Let ti be the left endpoint of the i-th sub-interval, then ti = 1 + i∆x

Let si be the value of 1/ti in the i-th sub-interval, then si = 1/ti

Then, we can approximate ln(5.6) as follows:

ln(5.6) ≈ (∆x)[s1 + s2 + s3 + ... + sn]

Now, we implement this in MATLAB:

main.m
a = 1; b = 5.6; n = 10000; % n is the number of sub-intervals
dx = (b - a)/n;
s = 0;
for i = 1:n
    ti = a + i*dx;
    si = 1/ti;
    s = s + si;
end

ln_approx = dx * s; % Approximate value of ln(5.6)

ln_true = log(5.6); % True value of ln(5.6)

% Display both results
fprintf("Approximate value of ln(5.6) using Riemann sum: %.4f\n", ln_approx);
fprintf("True value of ln(5.6) using MATLAB's built-in function: %.4f\n", ln_true);
434 chars
17 lines

When we run this code, we get:

main.m
Approximate value of ln(5.6) using Riemann sum: 1.7225
True value of ln(5.6) using MATLAB's built-in function: 1.7228
118 chars
3 lines

We can see that the approximate value is very close to the true value. As we increase the value of n, we can get even closer approximations.

related categories

gistlibby LogSnag