Summation of a Fourier series using for loop
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Good day, Matlab beginner here so i apologize in advance if this question is really silly.
I have a function x(t) which is the fourier series solution for a certain differential equation. I am trying to create some simple code in which the user can enter the value of t and the number of terms to which they would like to approximate the solution. The function is:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/169023/image.jpeg)
so far i have this:
format long
sum=0;
syms t
syms n
N = input('Please enter the number of terms:')
t = input('Please enter the value of t for which you would like to approximate:')
x = symfun((((-1)^(n+1))/(n*(n^2*pi^2+4)))*sin(n*pi*t),[t n]);
for i=1:N
sum = sum + (8/pi)*x(t,i)
end
fprintf('x(%t) for %N terms is.\n',name,age);
sum
I am getting back the error:
Error using symfun>validateArgNames (line 239)
Second input must be a scalar or vector of unique symbolic variables.
Error in symfun (line 42)
y.vars = validateArgNames(inputs);
I suspect the fact that Matlab makes use of vectors is what is confusing me - the error states that the symfun function wants a vector of symbolic variables and i'm currently trying to give it a scalar in the for loop. Could anyone give me a suggestion on what a good way to approach this problem would be? Would i have to populate n with values from 1 to the desired term N and then evaluate the symfun, returning a vector of length N and then add each position in that vector?
0 commentaires
Réponses (1)
KSSV
le 1 Nov 2017
Modifié(e) : KSSV
le 1 Nov 2017
You have defined t as a symbolic variable and later used it as a double..so the error.
format long
sum=0;
syms t
syms n
N = input('Please enter the number of terms:')
tt = input('Please enter the value of t for which you would like to approximate:')
x = symfun((((-1)^(n+1))/(n*(n^2*pi^2+4)))*sin(n*pi*t),[t n]);
for i=1:N
sum = sum + (8/pi)*x(tt,i)
end
% fprintf('x(%t) for %N terms is.\n',name,age);
sum
0 commentaires
Voir également
Catégories
En savoir plus sur Calculus dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!