Error using fft Invalid data type.

Here is my code, I get an error while using the fft() function. It is probably because that 't' is a symbolic value. Without changing the code too much I wanna plot the F-w graph (frequency space)
clc;
clear;
syms t;
K = 35;
Per = 1.5;
f1 = 100*((2734*t^5) - (6718*t^4) + (5682*t^3) - (1931*t^2) + (228*t));
f2 = -500;
aCo = (1/Per)*( int(f1, t, 0, ((2*Per)/3)) + int(f2, t, ((2*Per)/3), Per) );
w = ((2*pi)/Per);
Fo = zeros(1, K);
for n=1:1:K
anCo = (1/Per)*(int(f1*cos(w*n*t),t , 0, ((2*Per)/3)) + int(f2*cos(w*n*t), t, ((2*Per)/3), Per) );
bnCo = (1/Per)*(int(f1*sin(w*n*t),t , 0, ((2*Per)/3)) + int(f2*sin(w*n*t), t, ((2*Per)/3), Per) );
Fo = Fo + ((anCo*cos(w*n*t)) + (bnCo*sin(w*n*t)));
end
Fo = Fo + aCo;
figure(1)
fplot(Fo(35),[0 5*Per],'LineWidth',1.5,'Color','#0072BD')
xlabel('Time (t) - [s]')
ylabel('Force (F) - [N]')
grid on
FoFreq = fft(Fo);
figure(2)
plot(FoFreq)
xlabel('Frequency (\omega) - [s^{ -1 }]')
ylabel('Force (F) - [N]')
grid on
Error using fft
Invalid data type. First argument must be double, single, int8, uint8, int16, uint16, int32, uint32, or logical.

Answers (2)

Fo is a symbolic expression.
fft() is only valid for numeric expressions.
You can call fourier(Fo) .
But... according to the comments, you are already constructing K terms of a fourier sequence, and it does not make sense to take the fourier transform of a fourier sequence. (It might make sense to construct the inverse fourier transform)

1 Comment

Well we had a piecewise function, therefore to make it into a single function we did Fourier Series Expansion. After acquiring Fo (which is on the time domain) we have to do Fourier transform in order to plot a graph in frequency domain. So am I doing all this right or is there a way to do Fourier Series Expansion of a piecewise function.

Sign in to comment.

Paul
Paul on 16 Dec 2023
Edited: Paul on 16 Dec 2023
Hi Koray,
It looks like you have a signal f(t) that is defined over the interval 0 <= t <= 1.5 as:
syms t;
K = 20; %35; Changed to 20 to meet runtime limit here on Answers.
Per = 1.5;
f1 = 100*((2734*t^5) - (6718*t^4) + (5682*t^3) - (1931*t^2) + (228*t));
f2 = -500;
f(t) = piecewise(0 <= t <= 1, f1, 1 < t <= 1.5,f2);
fplot(f(t),[0 Per])
axis padded
Now we want the Fourier series representation of that signal
aCo = (1/Per)*( int(f1, t, 0, ((2*Per)/3)) + int(f2, t, ((2*Per)/3), Per) );
Use sym(pi) here
w = ((2*sym(pi))/Per)
w = 
Because you're accumulating each term of the Fourier series in the loop, don't allocate an array. Instead, just initialize Fo to zero.
%Fo = zeros(1, K);
Fo = sym(0);
Here, the expressions for anCo and bnCo aren't correct (hint: they each have the same error)
for n=1:1:K
anCo = (1/Per)*(int(f1*cos(w*n*t),t , 0, ((2*Per)/3)) + int(f2*cos(w*n*t), t, ((2*Per)/3), Per) );
bnCo = (1/Per)*(int(f1*sin(w*n*t),t , 0, ((2*Per)/3)) + int(f2*sin(w*n*t), t, ((2*Per)/3), Per) );
Fo = Fo + ((anCo*cos(w*n*t)) + (bnCo*sin(w*n*t)));
end
Fo = Fo + aCo;
Add the Fourier series representation to the plot, we see it's not correct.
hold on
fplot(Fo,[0 1.5])
First, the coeficients anCo and bnCo need to be corrected. Also, the code could probably be made more efficient by only computing anCo and bnCo once as functions of n and then evaluating them over the range of n, instead of calling int four times through each pass of the loop. But that's not all that important at this point.
Then, before going any further into the frequency domain we need to know how f(t) is defined for t < 0 and t > 1.5.
Is f(t) periodic with period Per =1.5?
Of, is f(t) = 0 for t > 1.5 and t < 0?

4 Comments

So F(t) is a periodic force that is also an piecewise function. So yeah it repeats after 1.5 which is the period (Per)
What we have to do is to acquire a F(t) function with the Fourier expansion and plot it first. Then secondly we have to plot a Amplitude-Frequency graph of F(t) which will be just a bar graph with values on (w, 2w, 3w, …) This means taking f(t) to the frequency domain right?
What I don’t understand is: is doing Fourier expansion/transform on a function 2 times for this normal? Or is it wrong?
Paul
Paul on 17 Dec 2023
Edited: Paul on 18 Dec 2023
To be more precise, we should say that f(t) is periodic and one period is defined as a piecewise function.
Step 1 is to find the Fourier series, or expansion, representation of f(t). You will have that with the code corrections I showed above, AFTER you correct the computation of anCo and of bnCo.
Step 2 is to make an amplitude-frequency graph of f(t). Based on your description of what that graph should look like (don't forget to include 0*w, -1*w, -2*w, ...), it sounds like you need the continuous-time Fourier transform of f(t). Yes, that means taking f(t) to the frequency domain, and you can do that based on Aco and the (corrected) anCo and bnCo terms you found in step 1.
The Fourier expansion of f(t) is still a time domain signal. So it's perfectly acceptable to then find the Fourier transform of f(t) based on that Fourier expansion, which would typically be denoted as F(w).
So after correcting anCo and bnCo, I wanna take int() functions out of the for loop, but if I just take them out and paste them before the loop I get the error "n variable is undefined" how do I define n so that the for loop isnt disrupted. Also when I do fourier(Fo) there seems to be a flat y=0 graph showing when I plot it. May there be a problem?
Paul
Paul on 19 Dec 2023
Edited: Paul on 19 Dec 2023
Without seeing the code, I'm guessing you need the line
syms n
before the lines with the int() functions, or better yet
syms n integer positive
As for the rest of your question, I think I know the answer and I don't think there is problem (assuming Fo is formulated correctly), but it's probably best to post your updated code so we can see what's what. If you do post your code, please post it in a comment in this answer as opposed to editing/updating the original question.

Sign in to comment.

Asked:

on 15 Dec 2023

Edited:

on 22 Dec 2023

Community Treasure Hunt

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

Start Hunting!