Summation of a series without for loop

2 visualizzazioni (ultimi 30 giorni)
John il 28 Dic 2014
Commentato: Iyad Khuder il 12 Nov 2020
Fourier series of a square wave can be represented as:
F(t)=(4/pi)*sum(1/(2k+1)*sin((2k+1)pi*t/T)
Where, sum is over k=0 to inf, T is period = 1. I need to plot F(t) for first 10 terms in the series. I can do this using loops, but is there any way to do this without any loop?
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Risposta accettata

Shoaibur Rahman il 28 Dic 2014
Yes you can do this! Use vectorization technique. Try to understand the line F = ... in the following code. If you get any hard time with this, please let me know. This is a great question though.
T = 1; t = -3:0.01:3; % change time vector as what you need
n = 10; k = 0:n-1; % n is the number of terms
F = 4/pi * (1./(2*k+1)) * sin(((2*k+1)'*pi*t)/T);
plot(t,F)
3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Michael Golden il 15 Mar 2017
Awesome! Many thanks!!
Iyad Khuder il 12 Nov 2020
That's amazing! Many thanks!

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by