Trying to plot an approximation of x(t)

I am trying to answer this question where a=3 and b=2
and here is my code I have made so far
syms x k n;
b = @(f,x,k) int(f*cos(k*pi/2*x)/2,x,-3,1);
c = @(f,x,k) int(f*sin(k*pi/2*x)/2,x,-3,1);
xt = @(f,x,n) b(f,x,0) + symsum(b(f,x,k)*cos(k*pi/2*x)+c(f,x,k)*sin(k*pi/2*x),k,1,n);
time = -2:0.5:2;
f= sawtooth(pi*time);
numTerms=1; %1,10,25,50
ezplot(xt(f,x,numTerms),-2,2)
but I keep getting an error when i run it and I'm not exactly sure what I'm doing wrong.

Answers (1)

clear variables close all
M = 250; % The length of each function a =3; b =2; x = -2:4/(M-1):2;
y = (a/b)*x;
f1 = zeros(1,M); f2 = f1; f3 = f2; f4 = f3; f5 = f4;
for k = 1 ck = (-1)^(k-1)/(pi*k); f1 = f1+3*ck.*sin(pi*k*x); p1 = abs(mean(y-f1)); end
for k = 1:3 ck = (-1)^(k-1)/(pi*k); f2 = f2+3*ck.*sin(pi*k*x); p2 = abs(mean(y-f2)); end
for k = 1:5 ck = (-1)^(k-1)/(pi*k); f3 = f3+3*ck.*sin(pi*k*x); p3 = abs(mean(y-f3)); end
for k = 1:15 ck = (-1)^(k-1)/(pi*k); f4 = f4+3*ck.*sin(pi*k*x); p4 = abs(mean(y-f4)); end
for k = 1:100 ck = (-1)^(k-1)/(pi*k); f5 = f5+3*ck.*sin(pi*k*x); p5 = abs(mean(y-f5)); end
figure plot(x,f1,'linewidth',2,'color','r') hold on; plot(x,f2,'linewidth',2,'color','g') plot(x,f3,'linewidth',2,'color','b') plot(x,f4,'linewidth',2,'color','k') plot(x,f5,'linewidth',2,'color','m')
a = legend('f1:N =1','f2:N =3','f3:N =5','f4:N =15','f5:N =100'); set(a,'fontsize',14) a= title('f(t) : Sine fourier series'); set(a,'fontsize',14); a= xlabel('x [-2\pi 2\pi]'); set(a,'fontsize',20); a = ylabel('y'); set(a,'fontsize',20); a = zlabel('z'); set(a,'fontsize',20); grid
% Disply the residual average power disp('1. Average ridual power for N = 1') disp(p1) disp('2. Average ridual power for N = 3') disp(p2) disp('3. Average ridual power for N = 5') disp(p3) disp('4. Average ridual power for N = 15') disp(p4) disp('5. Average ridual power for N = 100') disp(p5)

1 Comment

Hi, I thought wise to do the math and then use the result for the coding, hope this helps.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 21 Feb 2018

Commented:

on 23 Feb 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!