フィルターのクリア

How do I create a user defined function using the fourier series?

3 ビュー (過去 30 日間)
macabe banchero
macabe banchero 2018 年 3 月 24 日
コメント済み: macabe banchero 2018 年 3 月 28 日
I am supposed to create a user defined function using the Fourier series function that will do the following: (1) accept input variables c, L, N, and vecx (2) approximate the step function f(x), according to the Fourier series, over the set range of x values and (3) return an output for f(x) evaluated at each value of x. What I am thinking is that I need to use nested loops but I have no idea how to start. This is what I have so far:
function myoutput = mystep( c, L, N, vecx)
lengthx = length(vecx)
fxvec = zeros(1, lengthx)
while vecx >= 0 && vecx <= 2L
f(x) = (c/L) + ((2/pi).*(((-1^N)/N)*sin((N*pi*c)/L)*cos((N*pi*vecx)/L)))
end
Am I correct so far or do I need to use for loops or while loops to make the Fourier series work?
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 3 月 24 日
Udf? User defined function?
macabe banchero
macabe banchero 2018 年 3 月 27 日
yes

サインインしてコメントする。

採用された回答

Abraham Boayue
Abraham Boayue 2018 年 3 月 27 日
I would recommend a for loop to solve this problem. Try something like this :
function fx = fseries(c,N,x0,x1)
% All the inputs are
% constants
% x0 and x1 are the start and values of
% x, N is the length of x;it should be
% large eniugh; say N= 100 for example.
L = x1-x0; % period of f(x)
deltax = (x1-x0)/(N-1);
x = x0:deltax:x1;% better to create
% the vector this way than to input it.
F = zeros(1,N);
a0 = c/L; % since a0 is a constant,
% no need to have in in the for loop.
for n = 1:N
an = 2/pi*(-1)^n*sin(n*pi*c/L)
F = an*cos(n*pi*x/L);
end
F = a0+F;
% your function is an odd function
% since bn = 0.
plot(x,F,'linewidth',2)
a = title('f(x)');
set(a,'fontsize',12);
a = xlabel('x');
set(a,'fontsize',12);
a = ylabel('y');
set(a,'fontsize',12);
end
  1 件のコメント
macabe banchero
macabe banchero 2018 年 3 月 28 日
so basically you're taking the first part of the fourier series and setting it to "an" then multiplying it by the second part for every n.

サインインしてコメントする。

その他の回答 (2 件)

Abraham Boayue
Abraham Boayue 2018 年 3 月 28 日
I made a little error, here is the correction. Replace the expression for F with the following line.
F = F + an*cos(n*pi*x/L);
I am simply performing the summation in the equation of the Fourier series. Check this link to a solution that I made for someone, you can improve your function by following the code. https://www.mathworks.com/matlabcentral/answers/388949-how-can-i-perform-a-fourier-series-on-this-function
  2 件のコメント
macabe banchero
macabe banchero 2018 年 3 月 28 日
great thanks!
macabe banchero
macabe banchero 2018 年 3 月 28 日

So if I was to create a script that would ask for inputs of c,L, and N then call my function and approximate it for x values between 0 and 2L and then graph it. Would I need a for loop in here as well or no? This is what I have so far:

clear;clc;close all
input('variable c = ')
input('variable L = ')
input('variable N = ')
for x>=0 && x<=2L

サインインしてコメントする。


Abraham Boayue
Abraham Boayue 2018 年 3 月 28 日
編集済み: Abraham Boayue 2018 年 3 月 28 日
Your function wasn't designed to ask the user for inputs as you are attempting to do, although we can alter it to do that. To run the function, simply create an mfile like this. Let me know if you want the input feature. (You will get an error when you run this code, just change function fx to function F.
c = 6;
N = 100;
x0 = 0;
x1 = 10;
F = fseries(c,N,x0,x1);
  5 件のコメント
Walter Roberson
Walter Roberson 2018 年 3 月 28 日
Sounds about right.
macabe banchero
macabe banchero 2018 年 3 月 28 日
Awesome thanks!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by