How can I call my function myfourier again with inputs specified and not entered from the user?

1 view (last 30 days)
%Q1
f = input('Please enter function rules for variable segments of the function f:')
flim= input('Please enter real numbers to define the limits of the segments of f:')
N= input('Please enter an intiger to specifie the max order of Fourier coefficients of f:')
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
for k= 2:length(flim)
a0= (2/T)*int(f(k-1),t,flim(k-1),flim(k));
disp('a0=')
disp(a0)
end
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
%Q2
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);
  3 Comments
Torsten
Torsten on 14 Dec 2022
Edited: Torsten on 14 Dec 2022
Why don't you sum the an and bn to arrive at ann and bnn ? You just set ann(i+1) and bnn(i+1) to the integral over the last interval [flim(end-1):flim(end)], not over the complete interval [flim(1):flim(end)].
Why don't you divide ann(1) by 2 in the if-statement for the case i=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
Why do you repeat the calculation of a0 that has already been done in the loop before for i=0 ?
for k= 2:length(flim)
a0= (2/T)*int(f(k-1),t,flim(k-1),flim(k));
disp('a0=')
disp(a0)
end
Image Analyst
Image Analyst on 14 Dec 2022
Original (I think) question below (for when he deletes it like he did for his other questions).
%Q1
f = input('Please enter function rules for variable segments of the function f:')
flim= input('Please enter real numbers to define the limits of the segments of f:')
N= input('Please enter an intiger to specifie the max order of Fourier coefficients of f:')
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
for k= 2:length(flim)
a0= (2/T)*int(f(k-1),t,flim(k-1),flim(k));
disp('a0=')
disp(a0)
end
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
%Q2
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);

Sign in to comment.

Answers (1)

Askic V
Askic V on 14 Dec 2022
If I understood your question correctly, sometimes you want to call a function when user needs to enter inputs, and sometimes you want to call same function with specified inputs.
In that case, you should write a function with with variable number of input arguments.
Just look this small example:
res1 = my_add()
res2 = my_add(3, 4)
function c = my_add(varargin)
if nargin == 2
a = varargin{1};
b = varargin{2};
else
a = input('Enter first number: ');
b = input('Enter second number: ');
end
c = a + b; % or any other processing
end
  1 Comment
Stefanos
Stefanos on 14 Dec 2022
This is what I want to do. Ask the user to enter inputs and then call again the function with inputs f, flim and N the ones shown in the description. Can you provide more explenations please?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!