Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Hey need assistance with fourier analysis questions

조회 수: 1 (최근 30일)
Christopher Carey
Christopher Carey 2018년 4월 14일
마감: MATLAB Answer Bot 2021년 8월 20일
function [x,f]=my_Fourier_series_generator(T,a0,an,bn,xrange,max_N)
x=linspace(xrange(1),xrange(2),1000);
% frequency wn
w=2*pi/T;
% initializing f at a0
f=a0;
syms n;
% Fourier series implementation
for n=1:max_N
aa=eval(an);
bb=eval(bn);
f=f+aa*cos(w*n*x)+bb*sin(w*n*x);
end
plot(x,f)
this is the code im trying to run
solution(1, 1/2, (2/(n*pi))*sin((n*pi)/2), 0, 0:10:200, 300)
this is the error I keep receiving
Error using eval
Must be a string scalar or character vector.
Error in solution (line 17)
bb=eval(bn);

답변 (1개)

Walter Roberson
Walter Roberson 2018년 4월 14일
You are passing (2/(n*pi))*sin((n*pi)/2) in the position you call an . You ask to eval(an), so you are asking to eval((2/(n*pi))*sin((n*pi)/2)) . However, eval() can only work on string objects or character vectors.
The likelihood that you are using eval() appropriately is... rather low. You should probably be passing in function handles and evaluating the function handles. Like
aa = an(n)
after you have passed in
@(n) (2/(n*pi))*sin((n*pi)/2)
in that position.
  댓글 수: 2
Christopher Carey
Christopher Carey 2018년 4월 14일
편집: Walter Roberson 2018년 4월 14일
Hi, Firstly thank you very much for your assistance i tried this but now i'm getting a different error
Index exceeds matrix dimensions.
Error in gui212>my_Fourier_series_generator (line 244)
an2=an(n);
Error in gui212>Run_Callback (line 226)
[x,f]=my_Fourier_series_generator(T0,a0,an,bn,xrange,max_N)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in gui212 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)gui212('Run_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Walter Roberson
Walter Roberson 2018년 4월 14일
I said,
"after you have passed in @(n) (2/(n*pi))*sin((n*pi)/2) in that position".
You have not passed a function handle in that position: you have passed a scalar or vector of values.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by