Dimensions do not match error during integration
이전 댓글 표시
Hi,
I was trying to implement Fourier series by creating a function. The function was created successfully but when i try to integrate the function, it is giving error of dimension mismatch. I'm not able to understand why this issue is happening. Could you please help me on this?
Thanks in advance
clear all
close all
clc
points = 201;
t3 = linspace(-10,10,points);
%taking mod of t3
t2 = 5*mod(t3, 2);
%figure
%plot(t3,t2)
%creating a periodic function
y(t2 <= 3) = t2(t2 <= 3 );
y(and((t2>3),(t2<=4))) = 3;
y(and((t2>4),(t2<=5))) = t2(and((t2>4),(t2<=5)));
This is the periodic signal y created w.rt to t3

%time period
T0 = 2;
%angular frequency
w0 = (2*pi)/T0;
syms t ;
%used to get number of harmonics
N=input("enter the harmonics count");
%take only integer value for n
n=1:N;
%Fourier series coefficient calculation
a0 = (1/T0)*(int(y,t,0,2));
an = (2/T0)*(int(y*cos(n*w0*t),t,0,2));
bn = (2/T0)*(int(y*sin(n*w0*t),t,0,2)) ;
%Fourier representation
figure
hold on
plot(t3,y)
F = a0;
for i=1:N
F = F + an(i)*cos(i*w0*t3) + bn(i)*sin(i*w0*t3);
plot (t3,F)
end
grid on
hold off
답변 (1개)
Walter Roberson
2023년 1월 22일
0 개 추천
your y is a numeric vector. You cannot use int() or integrate() with numeric vectors.
You have y*cos() of something involving y. That is an attempt to use the "inner product" operation between two row vectors. Element by element multiplication is the .* operator not the * operator
댓글 수: 5
NAKUL
2023년 1월 22일
T0 = 3;
fs = @(t) t.*(t>0 & t<=T0/3) + t.^2.*(t>T0/3 & t<=T0/2) + 0*(t>T0/2 & t<=T0);
f = @(t) fs(mod(t,T0));
t = -5:0.01:5;
plot(t,f(t))
NAKUL
2023년 1월 23일
Walter Roberson
2023년 1월 23일
is f(t) real valued? I would not expect it to be. You need to plot real() or imag() or abs() of it.
Furthermore, the fourier transform of time is frequency. That is, you should not be plotting the fourier transform with time as your independent axes
카테고리
도움말 센터 및 File Exchange에서 Mathematics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

