I pretty sure there isn't a MATLAB function that does this. You're going to have to solve for these in a for-loop using the Fourier equations. I say this because I remember doing this in a partial diff eq. class. It was not that difficult. In fact, if I can find my code later today I'll post it here... but I haven't seen that code in years.
How about the fft function. Then after you take the fft choose the largest coefficients.
Thanks Sean,
I originally did it the way you have suggested and have favourable results with it, however the problem is that when I try to plot it, Matlab complains that the array is SYMBOLIC and it doesnt like that for plotting especially when the x axis is time and it is of type DOUBLE.
I need to figure out a way to convert the sym to Double.
Any suggestion??
look into ezplot, or to convert sym to double, use double() ?
may be if you post the final expression you can't plot, someone can help you
plot it.
--Nasser
Of course. Where should PLOT place the marker if the x coordinate is
2*t*cos(t), where t is a symbolic variable?
> I need to figure out a way to convert the sym to Double.
> Any suggestion??
If your symbolic expression doesn't contain any symbolic variables, use
DOUBLE. If it does, use SUBS (and perhaps DOUBLE after that.)
--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
Please see the code.
clear all, close all, clc
syms A0 Ak Bk Ck dt fn k t T0 W0 Y % declaring variables
%DEFINE FUNCTION
% fn=t;
%FIND FOURIER COEF's
W0=1; %define W0=1 fundamental frequency
T0=(2*pi); %Period of the function
AK=(2/T0)*int(t*cos(k*t*W0),t,-(T0/2),(T0/2));
BK=((2/T0)*int(t*(sin(k*t*W0)),t,-(T0/2),(T0/2)));
CK=(1/T0)*int(t*exp(-j*k*W0*t),t,-(T0/2),(T0/2));
%SIMPLIFY THE COEF's
Ak=simplify (AK)
Bk= round(BK)
Ck=simplify (CK)
%INITIALIZE VARIABLES
dt=T0/100;
%CALCULATE A0
A0=(1/T0)*int(t),t,-(T0/2),(T0/2);
t=[-pi:dt:pi]; %the variable limits and resolution
Y=zeros(size(t));
%PLOT FUNCTION
for k=[1:2:21];
y=A0/2+Ak*cos(k*t*W0)+Bk*sin(k*t*W0);
%double(y);
%EZPLOT(y);
% Y=Y+y
end
%plot(t,Y,'LineWidth',2);
axis([-4 4 -4 4]);
grid;
xlabel('time, s');
ylabel('v1(t), (V)');
*snip*
> Please see the code.
>
> clear all, close all, clc
> syms A0 Ak Bk Ck dt fn k t T0 W0 Y % declaring variables
> %DEFINE FUNCTION
> % fn=t;
> %FIND FOURIER COEF's
> W0=1; %define W0=1 fundamental frequency
> T0=(2*pi); %Period of the function
> AK=(2/T0)*int(t*cos(k*t*W0),t,-(T0/2),(T0/2));
> BK=((2/T0)*int(t*(sin(k*t*W0)),t,-(T0/2),(T0/2)));
> CK=(1/T0)*int(t*exp(-j*k*W0*t),t,-(T0/2),(T0/2));
> %SIMPLIFY THE COEF's
> Ak=simplify (AK)
> Bk= round(BK)
This doesn't work -- BK is a function of t and k, so there's no way to tell
how to round that symbolic expression until after you've substituted values
for k and t into it.
> Ck=simplify (CK)
> %INITIALIZE VARIABLES
> dt=T0/100;
> %CALCULATE A0
> A0=(1/T0)*int(t),t,-(T0/2),(T0/2);
I don't think this is parenthesized the way you intended.
> t=[-pi:dt:pi]; %the variable limits and resolution
> Y=zeros(size(t));
> %PLOT FUNCTION
> for k=[1:2:21];
> y=A0/2+Ak*cos(k*t*W0)+Bk*sin(k*t*W0);
Some or all of A0, Ak, and Bk are functions of the symbolic variables k and
t -- you should SUBS values for those symbolic variables into those
coefficients, and use .* to perform elementwise multiplication, when
computing y. If you do so, I believe Y will be either a double array or a
sym array that can be evaluated to return a double array using DOUBLE and
plotted using PLOT.
*snip*