Effacer les filtres
Effacer les filtres

Error using fft Invalid data type.

4 vues (au cours des 30 derniers jours)
ZoZo
ZoZo le 15 Déc 2023
Modifié(e) : ZoZo le 22 Déc 2023
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.

Réponses (2)

Walter Roberson
Walter Roberson le 15 Déc 2023
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 commentaire
ZoZo
ZoZo le 16 Déc 2023
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.

Connectez-vous pour commenter.


Paul
Paul le 16 Déc 2023
Modifié(e) : Paul le 16 Déc 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 commentaires
ZoZo
ZoZo le 19 Déc 2023
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 le 19 Déc 2023
Modifié(e) : Paul le 19 Déc 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.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by