Why I'm I getting this error when I try to call the function myfourier in another m file?

7 vues (au cours des 30 derniers jours)
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);

Réponse acceptée

Torsten
Torsten le 14 Déc 2022
Modifié(e) : Torsten le 14 Déc 2022
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);
a0= -12.5664 an= 11.3137 an= -8 an= 3.7712 an= 0 an= -2.2627 bn= -1.8385e-17 bn= 0 bn= 2.4689e-17 bn= -2 bn= 1.1469e-17
fs = 
%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 commentaires
Torsten
Torsten le 14 Déc 2022
Modifié(e) : Torsten le 14 Déc 2022
All in one function works ? Without the splitting in calling script and function ?
And the script part must be before the function definition.
Walter Roberson
Walter Roberson le 18 Déc 2022
Modifié(e) : Walter Roberson le 18 Déc 2022
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by