Error using plot for Fourier series
4 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
Star Strider
on 17 Dec 2023
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)
figure
fplot(Bn,[-L L])
grid
xlabel('k')
ylabel('Bn(k)')
.
4 Comments
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.
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
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
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!