Error using plot for Fourier series

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

回答 (2 件)

Star Strider
Star Strider 2023 年 12 月 17 日

0 投票

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 件のコメント

Tashanda Rayne
Tashanda Rayne 2023 年 12 月 17 日
I 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];
F = zeros(N, 1);
F1 = zeros(N, 1);
F2 = zeros(N, 1);
F3 = zeros(N, 1);
for i = 1:N
F = Bn_simplified.*sin(pi.*x./L).*exp((-(c.*(pi.^2))/L.^2).*0);
end
for i = 1:N
F1 = Bn_simplified.*sin(pi.*x./L).*exp((-(c.*(pi.^2))/L.^2).*50);
end
for i = 1:N
F2 = Bn_simplified.*sin(pi.*x./L).*exp((-(c.*(pi.^2))/L.^2).*100);
end
for i = 1:N
F3 = Bn_simplified.*sin(pi.*x./L).*exp((-(c.*(pi.^2))/L.^2).*200);
end
hold on
fplot(F , [-L,L])
Error using fplot>singleFplot
Input must be a function or functions of a single variable.

Error in fplot>@(f1,f2)singleFplot(cax,{f1,f2},limits,extraOpts,args) (line 208)
hObj = cellfun(@(f1,f2) singleFplot(cax,{f1,f2},limits,extraOpts,args),fn{1},fn{2},'UniformOutput',false);

Error in fplot>vectorizeFplot (line 208)
hObj = cellfun(@(f1,f2) singleFplot(cax,{f1,f2},limits,extraOpts,args),fn{1},fn{2},'UniformOutput',false);

Error in fplot (line 166)
hObj = vectorizeFplot(cax,fn,limits,extraOpts,args);
fplot(F1 , [-L,L])
fplot(F2 , [-L,L])
fplot(F3 , [-L,L])
hold off
xlabel('x')
ylabel('y')
legend('f','F(x) N=10', 'F(x) N=100','F(x) N=500','location','best')
grid on
Torsten
Torsten 2023 年 12 月 18 日
編集済み: Torsten 2023 年 12 月 18 日
I don't understand why you loop over i if your F, F1, F2 and F3 don't depend on i.
Tashanda Rayne
Tashanda Rayne 2023 年 12 月 18 日
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 2023 年 12 月 18 日
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
Torsten 2023 年 12 月 18 日
編集済み: Torsten 2023 年 12 月 18 日

0 投票

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 件のコメント

Tashanda Rayne
Tashanda Rayne 2023 年 12 月 18 日
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 2023 年 12 月 18 日
編集済み: Torsten 2023 年 12 月 18 日
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

サインインしてコメントする。

カテゴリ

質問済み:

2023 年 12 月 17 日

編集済み:

2023 年 12 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by