Advertisement
mhxion

sci-com-exercise-4

Jun 18th, 2024
935
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.98 KB | Software | 0 0
  1. % Manual increment
  2. % increment = (2 * pi) / 100
  3. % x = [-pi:increment:pi]
  4. x = linspace(-pi, pi, 101);
  5. N = 1000;
  6. empty_mat = zeros(N+1, 101);
  7. for i = 0:N
  8.    expression = (sin((2*i+1).* x)) / (2*i + 1);
  9.    % disp(size(expression))
  10.    % empty_mat(i+1)
  11.    empty_mat(i+1, :) = expression;
  12. end
  13. SNf1 = empty_mat .* (4 / pi);
  14. % disp(SNf1)
  15. SNf = sum(SNf1);
  16. f = [0:100];
  17. f(find((x < 0) & (x >= -pi))) = -1;
  18. f(find((x <= pi) & (x >= 0))) = 1;
  19. % Error
  20. E = abs(f - SNf);
  21. % Plotting x against SNf and x against err
  22. plot(x, SNf)
  23. title("x-values against partial sum (SNf)");
  24. xlabel("x-values");
  25. ylabel("SNf");
  26. % Plotting x against SNf
  27. plot(x, E)
  28. title("x-values against partial sum (SNf)");
  29. xlabel("x-values");
  30. ylabel("SNf");
  31. % Plotting x against err
  32. plot(x, E)
  33. title("x-values against E (absolute error between Fourier series of f and N-th partial sum)");
  34. xlabel("x-values");
  35. ylabel("E");
  36. % Maximum and average error
  37. disp(["Maximum error:", max(E)])
  38. disp(["Average error:", mean(E)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement