フィルターのクリア

Summation of a Fourier series using for loop

4 ビュー (過去 30 日間)
Shaun Struwig
Shaun Struwig 2017 年 11 月 1 日
編集済み: KSSV 2017 年 11 月 1 日
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:
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?

回答 (1 件)

KSSV
KSSV 2017 年 11 月 1 日
編集済み: KSSV 2017 年 11 月 1 日
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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by