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 个)

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 个评论

Suppose I want to define a PERIODIC function which is as given below
f(t) = t { 0 < T0/3 }
= t^2 { T0/3< t < T0/2 }
= 0 { T0/2 < t < T0 }
where T0 is period.
How can i define this and use for INTEGRATION as given above ( int( f(t).cos(n*w0*t)) ) ?
Thanks in advance :)
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))
Hi Torsten,
The function seems to be working but when I try to integrate it, the matlab is stuck somewhere and is not displaying anything. Am I doing anything wrong?
clear all
close all
clc
%function definition
T0 = 2;
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));
%angular frequency
w0 = (2*pi)/T0;
N=input("enter the harmonics count");
%take only integer value for n
n=1:N;
%Fourier series coefficient calculation
syms k;
a0 = (1/T0)*((int(f,k,0,2));
an = (2/T0)*(int(f*cos(n*w0*k),k,0,2));
bn = (2/T0)*(int(f*sin(n*w0*k),k,0,2)) ;
%Fourier representation
t = -6:0.01:6;
figure
hold on
plot(t,f(t))
F = a0;
for i=1:N
F = F + an(i)*cos(i*w0*t) + bn(i)*sin(i*w0*t);
plot (t,F(t))
end
grid on
hold off
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
With below code I was able to generate the waveform, but after generating i see some alternate waveforms are not reconstructed. Any idea why this happens?
syms x
T0 = pi;
sum= 0;
y = @(x) x.*(x>=0 & x<=pi/4) + 3*(x>pi/4 & x<=pi/2) + 0*(x>pi/2 & x<=pi); %function you want
%ys is periodic function of y;
ys = @(x) y(mod(x,T0));
%calculating Fourier co-efficients
a0 = (1/pi)*(int(x,x,0,pi/4)+ int(3,x,pi/4,pi/2));
sum = a0;
% 5 harmonics
n=1:5
n = 1×5
1 2 3 4 5
an=(1/pi)*(int(x*cos(n*x),x,0,pi/4)+ int(3*cos(n*x),x,pi/4,pi/2) + int(0*cos(n*x),x,pi/2,pi)) ;
bn=(1/pi)*(int(x*sin(n*x),x,0,pi/4)+ int(3*sin(n*x),x,pi/4,pi/2) + int(0*sin(n*x),x,pi/2,pi)) ;
%plotting from -4pi to 4pi
x= -4*pi:0.01*pi:4*pi;
hold on;
fplot(ys);
%FOurier series representation
for i=1:5
sum= sum+ (an(i)*cos(i*x)+bn(i)*sin(i*x));
plot(x,sum);
end
hold off

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Mathematics 的更多信息

产品

版本

R2022b

提问:

2023-1-22

评论:

2023-1-24

Community Treasure Hunt

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

Start Hunting!

Translated by