This is my MATLAB code. The function trapezoidal() is defined separately and it works fine.
syms x;
f = 10 + 2 * x - 6 * (x^2) + 5 * (x^4);
a = 0;
b = 2;
ans_3points = trapezoidal(f, a, b, 3);
ans_5points = trapezoidal(f, a, b, 5);
ans_7points = trapezoidal(f, a, b, 7);
fprintf('Integral estimate for three equally spaced points is %f.\n', ans_3points);
fprintf('Integral estimate for five equally spaced points is %f.\n', ans_5points);
fprintf('Integral estimate for seven equally spaced points is %f.\n', ans_7points);
actual_ans = int(f, 0, 2);
error_3points = 100 * (actual_ans - ans_3points) / actual_ans;
error_5points = 100 * (actual_ans - ans_5points) / actual_ans;
error_7points = 100 * (actual_ans - ans_7points) / actual_ans;
fprintf('Percentage relative error for three equally spaced points is %f.\n', error_3points);
fprintf('Percentage relative error for five equally spaced points is %f.\n', error_5points);
fprintf('Percentage relative error for seven equally spaced points is %f.\n', error_7points);
But this gives the following error at the line that prints error_3points: ??? Error using ==> fprintf Function is not defined for 'sym' inputs.
I haven't put any 'sym' inputs in fprintf() have I? ans_3points, ans_5points, ans_7points are printed without any problem. The errors are calculated but when I checked they were displayed as fractions. What exactly is the problem in this code? I really can't figure it out. Thank you.
Function trapezoidal
:
function l = trapezoidal(f, a, b, n)
N = n - 1; % N - the number of segmets
syms x;
series_sum = 0;
for i = (0 : (N - 1))
series_sum = series_sum + subs(f, x, xterm(i, a, b, n)) + subs(f, x, xterm((i + 1), a, b, n));
end
l = series_sum * (b - a) / (2 * N);