Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. %% 1.1 Set Up
  2.  
  3. % a)
  4. N = 30;
  5.  
  6. % b)
  7. k = 1:N;
  8. a_denom = 2.^k;
  9. a = 1./a_denom;
  10.  
  11. % c)
  12. x = transpose(linspace(0,4*pi,150));
  13.  
  14. % d)
  15. f = zeros(150,1);
  16. f_prime = zeros(150,1);
  17.  
  18. figure(1)
  19. stem(k,a)
  20. title("Fourier Coefficients");
  21. xlabel("Term Number"); ylabel("Coefficient");
  22.  
  23. %% 1.2 Compute Function
  24.  
  25. %fi = @(x) sum(a.*sin(k.*x));
  26. %f = fi(x);
  27. % I wasn't sure how to make element operations work, so I opted for a nested for loop
  28. % instead
  29.  
  30. f = [];
  31. for i = 1:150
  32. f_new = [];
  33. sum_in = [];
  34. for j = k
  35. in = a(j)*sin(j*x(i));
  36. sum_in = [sum_in, in];
  37. end
  38. f_new = sum(sum_in);
  39. f = [f, f_new];
  40. end
  41. f = transpose(f);
  42.  
  43. figure(2)
  44. plot(x,f)
  45. title("f (x)");
  46. xlabel("x");ylabel("y");
  47.  
  48. %% 1.3 Compute Derivative
  49.  
  50. f_prime = [];
  51. for i = 1:150
  52. f_new = [];
  53. sum_in = [];
  54. for j = k
  55. in = a(j)*j*cos(j*x(i));
  56. sum_in = [sum_in, in];
  57. end
  58. f_new = sum(sum_in);
  59. f_prime = [f_prime, f_new];
  60. end
  61. f_prime = transpose(f_prime);
  62.  
  63. figure(3)
  64. plot(x,f, 'k')
  65. hold on
  66. plot(x,f_prime, 'b')
  67. title("f(x) and f'(x)");
  68. xlabel("x");ylabel("y");
  69. legend( "f (x)", "f ' (x)");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement