Error using fft Invalid data type.
4 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (2 件)
Walter Roberson
2023 年 12 月 15 日
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)
Paul
2023 年 12 月 16 日
編集済み: Paul
2023 年 12 月 16 日
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)
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 件のコメント
Paul
2023 年 12 月 19 日
編集済み: Paul
2023 年 12 月 19 日
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.
参考
カテゴリ
Help Center および File Exchange で Calculus についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!