I was plotting some data in loglog scale "Errors against step sizes" but my curve is not connected , and i can not fix it, what is the problem?

1 次查看(过去 30 天)
clear all;
% Define the integrand function
f = @(z) 1./(1+z.^2);
% Exact value of the integral
exact_val = pi/4;
% Initialize vectors for storing errors and step sizes
h = zeros(1, 16);
simp_err = zeros(1, 16);
% Compute approximations and errors for each step size
for k = 1:16
h(k) = 2.^(-k);
% Simpson's rule
x = 0 : h(k) : 1;
simp_approx = h(k)/3 * (f(x(1)) + 4*sum(f(x(2:2:end-1))) + 2*sum(f(x(3:2:end-1))) + f(x(end)));
le(k)=simp_approx;
simp_err(k) = abs(simp_approx - exact_val);
end
% Plot errors against step sizes in log-log scale
loglog(h, simp_err, '.-');
hold on;
loglog(h,h.^4,'-r');
hold off
xlabel('$Step\;size\;(h)$','Interpreter','latex');
ylabel('$Absolute\;error$','Interpreter','latex');
legend('$Simpsons\;rule$','$Line\;of\;O(h^4)$','Interpreter','latex');
grid on;

回答(1 个)

Walter Roberson
Walter Roberson 2023-3-16
Some of your simp_err are exactly 0 but log plots cannot draw exact 0 (or negative) so the plot disconnects there.
This happens at smaller step sizes that go along with larger k, but smaller values are more towards the left so the gaps appear more towards the left instead of more towards the right (increasing k)

类别

Help CenterFile Exchange 中查找有关 Visual Exploration 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by