I need help with fourier matrix index integral, I can't find how to write it in matlab

1 view (last 30 days)
I don't know how to type this into matlab. Fourier function with matrix index
Here is my mathcad code and I'm trying to learn how to write it in matlab:
The first part of my matlab code is i which corresponds with i(t) from mathcad photo. I can't find how to write the integral with matrice index.
%%
%current i(t)
T=20*10^-3;
t=0:T/1000:T;
w=2*pi*1/T;
i=100.*(t>1*T/12).*(t<5*T/12)-100.*(t>7*T/12).*(t<11*T/12);
u=sin(w*t).*10.*(t>0).*(t<T);
%plot i
figure(1)
plot(t,i, 'DisplayName', 'i(t)')
hold on
legend
xlabel('Time','FontSize',16)
title('3.20','FontSize',16)
%until here the code is working
%a0, an, ab
n = linspace(0, 50);
T=20*10^-3;
t=0:T/1000:T;
w=2*pi*1/T;
a0 = @(i,T) integral(@(t) 2./T.*i, 0, T);
an = @(i,T) integral(@(t) 2./T.*i.*cos(n*w*t), 0, T);
ab = @(i,T) integral(@(t) 2./T.*i.*sin(n*w*t), 0, T);
F=sum(an.*cos(n*w*t).+bn.*sin(n*w*t)
fourier=(a0/2)+F
%plot fourier + current
figure(2)
plot(t,i+fourier, 'DisplayName', 'fourier')
hold on
legend
xlabel('Time','FontSize',16)
title('3.20','FontSize',16)
It should plot like this:
Any help is appreciated thanks.

Accepted Answer

Torsten
Torsten on 8 May 2022
Edited: Torsten on 8 May 2022
n = 50;
T=20*10^-3;
t=0:T/1000:T;
w=2*pi*1/T;
f=@(t)0+100.*(t>1*T/12).*(t<5*T/12)-100.*(t>7*T/12).*(t<11*T/12);
a0 = 2/T*integral(@(t)f(t),0,T);
for i=1:n
a(i) = 2/T*integral(@(t)f(t).*cos(w*i*t),0,T);
b(i) = 2/T*integral(@(t)f(t).*sin(w*i*t),0,T);
end
four = @(t) a0/2+sum((a(1:n).*cos((1:n)*w*t)+b(1:n).*sin((1:n)*w*t)));
four_eval = arrayfun(four,t)
plot(t,f(t))
hold on
plot(t,four_eval)

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!