Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %% 1.1 Set Up
- % a)
- N = 30;
- % b)
- k = 1:N;
- a_denom = 2.^k;
- a = 1./a_denom;
- % c)
- x = transpose(linspace(0,4*pi,150));
- % d)
- f = zeros(150,1);
- f_prime = zeros(150,1);
- figure(1)
- stem(k,a)
- title("Fourier Coefficients");
- xlabel("Term Number"); ylabel("Coefficient");
- %% 1.2 Compute Function
- %fi = @(x) sum(a.*sin(k.*x));
- %f = fi(x);
- % I wasn't sure how to make element operations work, so I opted for a nested for loop
- % instead
- f = [];
- for i = 1:150
- f_new = [];
- sum_in = [];
- for j = k
- in = a(j)*sin(j*x(i));
- sum_in = [sum_in, in];
- end
- f_new = sum(sum_in);
- f = [f, f_new];
- end
- f = transpose(f);
- figure(2)
- plot(x,f)
- title("f (x)");
- xlabel("x");ylabel("y");
- %% 1.3 Compute Derivative
- f_prime = [];
- for i = 1:150
- f_new = [];
- sum_in = [];
- for j = k
- in = a(j)*j*cos(j*x(i));
- sum_in = [sum_in, in];
- end
- f_new = sum(sum_in);
- f_prime = [f_prime, f_new];
- end
- f_prime = transpose(f_prime);
- figure(3)
- plot(x,f, 'k')
- hold on
- plot(x,f_prime, 'b')
- title("f(x) and f'(x)");
- xlabel("x");ylabel("y");
- legend( "f (x)", "f ' (x)");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement