Writing and plotting a function as a Fourier Series

15 views (last 30 days)
How do I get Matlab to plot my General Fourier series?
Here is my code so far
clear all;
clc;
type = input('What type of function are you solving \n a) General Fourier Series \n b) Heat Equation \n c) Wave Equation \n d) Rectangular Plate \n e) Circular Plate \n', 's')
if type == 'a'
syms x;
eq = input('Enter Function, with @(x) then the function \n', 's')
f = str2func(eq)
lowb = input ('What is the lower bound?\n');
upb = input ('What is the upper bound? \n');
i = 20;
u=0;
a0 = 1/upb * integral(f, lowb, upb);
for n = 1:2:i
f1 = f * cos(n*3.14*x);
an = int(f1, lowb, upb) * 1/upb;
f2 = f*sin(n*3.14*x);
bn = 1/ upb * int(f2, lowb, upb);
u = u+(an*cos(n*3.14*x) + bn*sin(n*3.14*x));
end
u=u+(a0/2);
plot(x,u, [lowb, upb]);
end
Here is what I am plugging in
What type of function are you solving
a) General Fourier Series
b) Heat Equation
c) Wave Equation
d) Rectangular Plate
e) Circular Plate
a
type =
'a'
Enter Function, with @(x) then the function
@(x)x+5
eq =
'@(x)x+5'
f =
function_handle with value:
@(x)x+5
What is the lower bound?
-5
What is the upper bound?
5
Error using plot
Data must be numeric, datetime, duration or an array convertible to double.
Error in Practice (line 31)
plot(x,u, [lowb, upb]);

Answers (1)

Sai Sri Pathuri
Sai Sri Pathuri on 27 Apr 2020
In the code attached, you are trying to plot the symbolic variables, due to which the error is being thrown. You may find the values of u for a set of values of x using subs function, convert it into double and plot them.
x = 1;
y = subs(u);
y = double(y);
You may set the limits of plot by xlim and ylim functions

Community Treasure Hunt

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

Start Hunting!