Plot Fourier Series from a variable

2 ビュー (過去 30 日間)
Andres De Leon
Andres De Leon 2021 年 11 月 18 日
回答済み: Paul 2021 年 11 月 18 日
I have this program which calculates the Fourier Series of a function. In this case the function is just f(t)=x. It makes the calculation correctly, and it can compute as many terms of the series as I want (in this case only 5 terms). However, I can not plot the series because the whole series is stored in the variable sys_sum. Can someone please help me plotting the series?
Code:
clc; clear all; close all;
syms x k L n
evalin(symengine,'assume(k,Type::Integer)');
%Obtain an and bn
a = @(f,x,k,L) int(f*cos(k*pi*x/L)/L,x,-L,L);
b = @(f,x,k,L) int(f*sin(k*pi*x/L)/L,x,-L,L);
%Fourier Series is calculated
fs = @(f,x,n,L) a(f,x,0,L)/2 + ... %a0/2 a0=an in the interation number 0 [k or n = 0]
symsum(a(f,x,k,L)*cos(k*pi*x/L) + b(f,x,k,L)*sin(k*pi*x/L),k,1,n); %+an*cos(nwt)+bn*sin(nwt)
f = x %Function
pretty(fs(f,x,5,pi)); %fs(f(t), x, n, T) ----- fs(function, variable x, number of terms, period)
sys_sum = fs(f,x,5,pi);

採用された回答

Paul
Paul 2021 年 11 月 18 日
The easiest way would be to use fplot(). Also, use assume() instead of evalin
syms x k L n
% evalin(symengine,'assume(k,Type::Integer)');
assume(k,'integer')
assume(n,'integer')
%Obtain an and bn
a = @(f,x,k,L) int(f*cos(k*pi*x/L)/L,x,-L,L);
b = @(f,x,k,L) int(f*sin(k*pi*x/L)/L,x,-L,L);
%Fourier Series is calculated
fs = @(f,x,n,L) a(f,x,0,L)/2 + ... %a0/2 a0=an in the interation number 0 [k or n = 0]
symsum(a(f,x,k,L)*cos(k*pi*x/L) + b(f,x,k,L)*sin(k*pi*x/L),k,1,n); %+an*cos(nwt)+bn*sin(nwt)
f = x %Function
f = 
x
pretty(fs(f,x,5,pi)); %fs(f(t), x, n, T) ----- fs(function, variable x, number of terms, period)
sin(3 x) 2 sin(4 x) sin(5 x) 2 ---------- - sin(2 x) - -------- + ---------- + 2 sin(x) 3 2 5
sys_sum = fs(f,x,5,pi);
fplot(sys_sum)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCalculus についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by