Why I'm I getting this error when I try to call the function myfourier in another m file?
조회 수: 2 (최근 30일)
이전 댓글 표시
myfourier.m file
function [a0 an bn fs] = myfourier(f, flim, N)
syms t
syms N
T = flim(end)-flim(1);
w0 = 2*pi/T;
fs = 0.0;
for i = 0:N
for k= 2:length(flim)
an= (2/T)*int(f(k-1)*cos(i*w0*t),t,flim(k-1),flim(k));
ann(i+1)=an;
bn= (2/T)*int(f(k-1)*sin(i*w0*t),t,flim(k-1),flim(k));
bnn(i+1)=bn;
fs= fs + an*cos(w0*i*t)+bn*sin(w0*i*t);
end
if i==0
fs = fs/2;
end
%a0 is calculated first (for i=0). The Fourier series starts with a0/2 ...
end
a0 = 0.0;
for k= 2:length(flim)
a0= a0+(2/T)*int(f(k-1),t,flim(k-1),flim(k));
end
disp('a0=')
disp(a0)
for i= 1:N
disp('an=')
disp(ann(i+1))
end
for i= 1:N
disp('bn=')
disp(bnn(i+1))
end
fs
title("Fourier series approximation of f against estimated function f with N="+N)
hold on
fourier = matlabFunction(fs);
grid on
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
xlabel('x')
ylabel('y')
t = flim(1):0.01:flim(end);
plot(t,fourier(t))
for k= 2:length(flim)
fplot(f(k-1),[flim(k-1), flim(k)])
end
legend('Fourier approximation of f')
end
Excercise4.m file
f= [-t- 2*pi, -t, 2*pi- t, -t-4*pi];
flim= [-3*pi,-pi,pi,3*pi,5*pi];
N= 5;
[a0 an bn fs] = myfourier(f, flim, N);
Error using symengine>@(t)(sin(t.*pi).*4.0)./pi+(sin(t.*pi.*3.0).*(4.0./3.0))./pi+(sin(t.*pi.*5.0).*(4.0./5.0))./pi+6.0
Too many input arguments.
Error in Excercise4 (line 5)
[a0 an bn fs] = myfourier(f, flim, N);
채택된 답변
Torsten
2022년 12월 14일
편집: Torsten
2022년 12월 14일
Give another name to the MATLAB function created here:
fourier = matlabFunction(fs);
There is already a MATLAB built-in function with the same name:
It won't produce an error in this case, but confuses and will lead to an error if you wanted to use MATLAB's "fourier" somewhere else in your code.
%Excercise4.m file
syms t
f= [-t-2*pi, -t, 2*pi-t, -t-4*pi];
flim= [-3*pi,-pi,pi,3*pi,5*pi];
N= 5;
[a0 an bn fs] = myfourier(f, flim, N);
%myfourier.m file
function [a0 an bn fs] = myfourier(f, flim, N)
syms t
T = flim(end)-flim(1);
w0 = 2*pi/T;
fs = 0.0;
ann = zeros(1,N+1);
bnn = zeros(1,N+1);
for i = 0:N
for k= 2:length(flim)
an= (2/T)*int(f(k-1)*cos(i*w0*t),t,flim(k-1),flim(k));
bn= (2/T)*int(f(k-1)*sin(i*w0*t),t,flim(k-1),flim(k));
ann(i+1) = ann(i+1) + an;
bnn(i+1) = bnn(i+1) + bn;
fs= fs + an*cos(w0*i*t)+bn*sin(w0*i*t);
end
if i==0
fs = fs/2;
a0 = ann(1);
end
%a0 is calculated first (for i=0). The Fourier series starts with a0/2 ...
end
disp('a0=')
disp(a0)
for i= 1:N
disp('an=')
disp(ann(i+1))
end
for i= 1:N
disp('bn=')
disp(bnn(i+1))
end
fs
title("Fourier series approximation of f against estimated function f with N="+N)
hold on
fourier = matlabFunction(fs);
grid on
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
xlabel('x')
ylabel('y')
t = flim(1):0.01:flim(end);
plot(t,fourier(t))
for k= 2:length(flim)
fplot(f(k-1),[flim(k-1), flim(k)])
end
legend('Fourier approximation of f')
end
댓글 수: 2
Walter Roberson
2022년 12월 18일
편집: Walter Roberson
2022년 12월 18일
fouriergraph = matlabFunction(fs);
When you use matlabFunction() with no 'vars' option, then the number of input parameters is determined by counting symvar() of the expression. If the symbolic engine cannot find any "unbound" variables in the expression then symvar() is empty and the generated function will expect no inputs.
A "bound" variable is, roughly speaking, one that could be entirely replaced in the expression without changing the result of the expression. But not exactly that. It is more that the variable is acting as a temporary variable with values to be filled in automatically by the symbolic engine. For example, int(f(x),x,a,b) could be replaced by int(f(DUMMY),DUMMY,a,b) and you would still get the same result provided that f(DUMMY) does not contain any references to x. Other functions with "bound" variables include symsum() and symprod()
So if you use matlabFunction() without any 'vars' option and the function is effectively constant on inputs, then matlabFunction would leave out defining any input parameter, creating a function with no inputs expected.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!