Error using plot for Fourier series

4 views (last 30 days)
Tashanda Rayne
Tashanda Rayne on 17 Dec 2023
Edited: Torsten on 18 Dec 2023
This is not plotting. I tried applying fplot and plot and neither will work. I have assigned t so im not sure why I keep getting the error:
"Error using plot
Data must be numeric, datetime, duration or an array convertible to double.
Error in Fourier_example_3 (line 48)
plot(x,F3, 'b' )"
This is my code:
% Fourier Series
clear all
syms x k L t
c = 1
f1(x) = x;
f2(x) = L-x;
f1 = simplify(f1);
f2 = simplify(f2);
%Solving for Bn
b(k) = (2/L)*int(f1*sin(k*pi*x/L),[0,L/2]);
b1(k) = (2/L)*int(f2*sin(k*pi*x/L),[L/2,L]);
Bn(k) = b1 + b;
Bn = simplify(Bn);
N=100;
x = linspace(-L+0.000001,L,N);
t = [0 50 100 200];
for i = 1:1:N
F = Bn.*sin(pi.*x./L).*exp((-(c.*(pi.^2))/L.^2).*0);
end
for i = 1:1:N
F1 = Bn.*sin(pi.*x./L).*exp((-(c.*(pi.^2))/L.^2).*50);
end
for i = 1:1:N
F2 = Bn.*sin(pi.*x./L).*exp((-(c.*(pi.^2))/L.^2).*100);
end
for i = 1:1:N
F3 = Bn.*sin(pi.*x./L).*exp((-(c.*(pi.^2))/L.^2).*200);
end
plot(x,F3, 'b' )
plot(x,F,'--r')
plot(x,F1,'--p')
plot(x,F2,'-r')
xlabel('x')
ylabel('y')
legend('f','F(x) N=10', 'F(x) N=100','F(x) N=500','location','best')
grid on
hold off

Answers (2)

Star Strider
Star Strider on 17 Dec 2023
The fplot function is usually best for symbolic functions.
I am not certain what you want to plot.
Try this —
syms x k L t
c = 1;
L = 80;
f1(x) = x;
f2(x) = L-x;
f1 = simplify(f1);
f2 = simplify(f2);
%Solving for Bn
b(k) = (2/L)*int(f1*sin(k*pi*x/L),[0,L/2]);
b1(k) = (2/L)*int(f2*sin(k*pi*x/L),[L/2,L]);
Bn(k) = b1 + b;
Bn = simplify(Bn)
Bn(k) = 
figure
fplot(Bn,[-L L])
grid
xlabel('k')
ylabel('Bn(k)')
.
  4 Comments
Tashanda Rayne
Tashanda Rayne on 18 Dec 2023
I am attempting to have this code run for different t and k values. I took k out when the code wasnt working. It now looks like this, my apologies. I don't know how to get the code to run
tried fplot and it still is giving me an error :
syms x k L t
c = 1;
f1(x) = x;
f2(x) = L-x;
f1_simplified = simplify(f1);
f2_simplified = simplify(f2);
%Solving for Bn
b(x) = (2/L)*int(f1_simplified*sin(k*pi*x/L),[0,L/2]);
b1(x) = (2/L)*int(f2_simplified*sin(k*pi*x/L),[L/2,L]);
Bn(x) = b1 + b;
Bn_simplified = simplify(Bn);
N = 100;
x = linspace(-pi,pi,N);
t = [0;0.1;0.5;2];
k = 5
for i = 1:2:k
F = Bn_simplified.*sin(k*pi.*x./L).*exp((-(c.*(pi.^2))/L.^2).*t);
end
hold on
fplot(F , [-L,L])
Walter Roberson
Walter Roberson on 18 Dec 2023
x = linspace(-pi,pi,N);
That is a vector.
for i = 1:N
F = Bn_simplified.*sin(pi.*x./L).*exp((-(c.*(pi.^2))/L.^2).*0);
end
The right hand side involves the vector F, so the left hand side will be a vector.
Each iteration you overwrite all of F, so the result is going to be the last value assigned to it... which is the same as the first value assigned to it.
fplot(F , [-L,L])
F is a vector of symbolic values, so you are asking to fplot() a symbolic vector. That isn't going to work very well.

Sign in to comment.


Torsten
Torsten on 18 Dec 2023
Edited: Torsten on 18 Dec 2023
I'm not sure what you try to do. Maybe something like this:
clear all
syms x
L = 2;
f = piecewise(x<=L/2,x,x>L/2,(L-x));
N = 5;
b = 2/L*int(f*sin((1:N)*pi*x/L),x,[0,L]);
F{1} = 0;
for k = 1:N
F{k+1} = F{k} + b(k)*sin(pi*k*x/L);
end
hold on
fplot(F,[0 L])
fplot(f,[0 L])
hold off
grid on
  2 Comments
Tashanda Rayne
Tashanda Rayne on 18 Dec 2023
This worked but I need to specify the t. The complete equation for
F = Bn_simplified.*sin(k*pi.*x./L).*exp((-(c.*(pi.^2))/L.^2).*t);
Torsten
Torsten on 18 Dec 2023
Edited: Torsten on 18 Dec 2023
Why not simply multiplying F by the factor ?
syms x
t = 1;
c = 1;
L = 2;
f = piecewise(x<=L/2,x,x>L/2,(L-x));
N = 5;
b = 2/L*int(f*sin((1:N)*pi*x/L),x,[0,L]);
F{1} = 0;
for k = 1:N
F{k+1} = F{k} + b(k)*sin(pi*k*x/L)*exp(-c*pi^2/L^2*t);
end
hold on
fplot(F,[0 L])
fplot(f,[0 L])
hold off
grid on

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!