Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. function y = mybessel(x)
  2. %
  3. % Computes the 0th-order Bessel function of the first kind
  4. %
  5.  
  6. K = 32;
  7.  
  8. bessel_coef = zeros(1,K);
  9.  
  10. kfac = 1;
  11. toggle = 1;
  12. two_to_k = 1;
  13.  
  14. for k = 1:K
  15. toggle = -toggle;
  16. kfac = kfac*k;
  17. two_to_k = two_to_k * 2;
  18. bessel_coef(k) = toggle/(kfac*two_to_k)^2; % compute power series coefficients in advance
  19. end
  20.  
  21. x = x.^2;
  22.  
  23. y = x .* bessel_coef(K);
  24. for k = K-1:-1:1
  25. y = x .* (bessel_coef(k) + y); % Horner's method
  26. end
  27.  
  28. y = 1 + y;
  29.  
  30. end
  31.  
  32. x = linspace (-14, 14, 28*4096+1);
  33.  
  34. J_0 = besselj(0, x); % MATLAB's bessel
  35.  
  36. I_0 = mybessel(x); % my bessel
  37.  
  38. figure(1)
  39. plot(x, J_0)
  40. hold on
  41. plot(x, I_0)
  42. hold off
  43.  
  44. figure(2)
  45. plot(x, I_0 - J_0) % plot error
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement