Arrays have incompatible sizes for this operation
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
my objective is to plot the FRF transform using method of partial sums
%Initialize Parameters
x0=1; %peak amplitude
t0=2; %period (sec)
k=-20:1:20; %range for index k
w0= 2*pi/t0; %frequency in
t= 0:0.001:5; %time range
nk=length(k);
j= 1i;
%Loop for coefficients
ck = [];
for i=1:length(k)
if k(i)~=0
ck=[ck ,(t*x0/t0).*(sin((t*k(i)*w0)/2) / (t*k(i)*w0)/2)];
end
if k(i)==0
ck=[ck , (t*x0) / t0];
end
end
%Partial Sum for Sawtooth Wave
x_t=[];
for n=1:length(t)
x_t = [x_t, sum(ck.* exp(j*k*w0*t(n)))];
end
%plot of coefficients
subplot(1,2,1)
stem(k,abs(ck))
title('Rectangular Coefficients (Bohlen)')
xlabel('k index value')
ylabel('Magnitude of ck')
grid on
%plot of partial sum
subplot(1,2,2)
plot(t,x_t)
title('Rectangular Wave for -20<k<20 (Bohlen)')
xlabel('Time (sec)')
ylabel('Amplitude of Full Wave Rectified')
grid on
Arrays have incompatible sizes for this operation.
Error in Rectangular_HW3 (line 29)
x_t = [x_t, sum(ck.* exp(j*k*w0*t(n)))];
>> whos
Name Size Bytes Class Attributes
ck 1x205041 1640328 double
i 1x1 8 double
j 1x1 16 double complex
k 1x41 328 double
n 1x1 8 double
nk 1x1 8 double
t 1x5001 40008 double
t0 1x1 8 double
w0 1x1 8 double
x0 1x1 8 double
x_t 0x0 0 double
0 Kommentare
Antworten (1)
Shivam
am 19 Feb. 2025
Hi Peter,
The error you're encountering is due to incompatible array sizes in the calculation of the partial sum for the sawtooth wave. Specifically, the issue arises from how ck is being constructed and subsequently used in the partial sum calculation.
You can use the modified code which correctly constructs the ck and calculates the partial sum.
% Initialize Parameters
% .
% ..
nk = length(k);
j = 1i;
% Loop for coefficients. Initialize ck as a vector of zeros
ck = zeros(size(k));
for i = 1:nk
if k(i) ~= 0
ck(i) = (x0/t0) * (sin(k(i)*w0*t0/2) / (k(i)*w0*t0/2));
else
ck(i) = x0 / t0;
end
end
% Partial Sum for Sawtooth Wave. Initialize x_t as a vector of zeros
x_t = zeros(size(t));
for n = 1:length(t)
x_t(n) = sum(ck .* exp(j*k*w0*t(n)));
end
% Plot of coefficients
subplot(1,2,1)
stem(k, abs(ck))
title('Rectangular Coefficients (Bohlen)')
xlabel('k index value')
ylabel('Magnitude of ck')
grid on
% Plot of partial sum
subplot(1,2,2)
plot(t, x_t)
title('Rectangular Wave for -20<k<20 (Bohlen)')
xlabel('Time (sec)')
ylabel('Amplitude of Full Wave Rectified')
grid on
1 Kommentar
Walter Roberson
am 19 Feb. 2025
Plotting the imaginary part separately
%Initialize Parameters
x0=1; %peak amplitude
t0=2; %period (sec)
k=-20:1:20; %range for index k
w0= 2*pi/t0; %frequency in
t= 0:0.001:5; %time range
nk = length(k);
j = 1i;
% Loop for coefficients. Initialize ck as a vector of zeros
ck = zeros(size(k));
for i = 1:nk
if k(i) ~= 0
ck(i) = (x0/t0) * (sin(k(i)*w0*t0/2) / (k(i)*w0*t0/2));
else
ck(i) = x0 / t0;
end
end
% Partial Sum for Sawtooth Wave. Initialize x_t as a vector of zeros
x_t = zeros(size(t));
for n = 1:length(t)
x_t(n) = sum(ck .* exp(j*k*w0*t(n)));
end
% Plot of coefficients
subplot(2,2,1)
stem(k, abs(ck))
title('Rectangular Coefficients (Bohlen)')
xlabel('k index value')
ylabel('Magnitude of ck')
grid on
% Plot of partial sum
subplot(2,2,3)
plot(t, real(x_t), 'b')
title('Rectangular Wave for -20<k<20 (Bohlen)')
xlabel('Time (sec)')
ylabel('Amplitude of Full Wave Rectified')
grid on
subplot(2,2,4)
plot(t, imag(x_t), 'r')
title('Imaginary Rectangular Wave for -20<k<20 (Bohlen)')
xlabel('Time (sec)')
ylabel('Amplitude of Full Wave Rectified')
grid on
Siehe auch
Kategorien
Mehr zu Spectral Measurements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

