Evaluating a complex equation

2 次查看(过去 30 天)
Hi,
I'm trying to evaluate the following equation. Is there a way of doing it? I keep getting the following "Unable to perform assignment because the left and right sides have a different number of elements." Do I need to break it down further?
clc;
clear all;
close all;
N=8;
% for n=1:N
fc = 20; %CARRIER FREQUENCY
fs = 2000; %SAMPLING FREQUENCY
fm = 1; %MESSAGE FREQUENCY
tau = 0.2;
m=6;
T=1/fs;
t=0:1/fs:((2/fm)-(1/fs));
Y=0;
for n=1:8
Y(n)=(tau/T).*(1+m*sin(2*pi*fm*t))+(2/n*pi).*sin(n*2*pi*fs).*[tau/2+(m*tau/2).*sin(2*pi*fm*t)].*cos(n*2*pi*fs*t);
end
plot(Y)
Many thanks,
Andy

采纳的回答

Star Strider
Star Strider 2023-8-23
编辑:Star Strider 2023-8-23
Preallocate ‘Y’ as:
Y=zeros(8,numel(t));
add a second dimension to ‘Y’ in the assignment to it:
Y(n,:) = ...
and add ‘t’ to the plot call, and it works.
Try this —
N=8;
% for n=1:N
fc = 20; %CARRIER FREQUENCY
fs = 2000; %SAMPLING FREQUENCY
fm = 1; %MESSAGE FREQUENCY
tau = 0.2;
m=6;
T=1/fs;
t=0:1/fs:((2/fm)-(1/fs));
Y=zeros(8,numel(t));
for n=1:8
Y(n,:)=(tau/T).*(1+m*sin(2*pi*fm*t))+(2/n*pi).*sin(n*2*pi*fs).*[tau/2+(m*tau/2).*sin(2*pi*fm*t)].*cos(n*2*pi*fs*t);
end
figure
plot(t,Y)
EDIT — (23 Aug 2023 at 14:55)
Another option is to use surf to plot the matrix —
figure
surf(t, (1:8), Y, 'EdgeColor','none')
colormap(turbo)
xlabel('t')
ylabel('n')
zlabel('Y')
.

更多回答(1 个)

Voss
Voss 2023-8-23
Y(n)=(tau/T).*(1+m*sin(2*pi*fm*t))+(2/n*pi).*sin(n*2*pi*fs).*[tau/2+(m*tau/2).*sin(2*pi*fm*t)].*cos(n*2*pi*fs*t);
The left side Y(n) is a single element of Y. The right side is a vector the size of t. t has more than one element, so that assignment is not going to work because you can't put multiple elements into a slot that's only big enough for one element.
You need to change how the results are stored in Y. You need a way to store mutiple vectors together. Since all the vectors are the same size (the size of t), you can store them in a matrix.
Say you want to store each vector as a column of Y (instead of trying to force them into a single element). Then you can do something like this:
clc;
clear all;
close all;
N=8;
% for n=1:N
fc = 20; %CARRIER FREQUENCY
fs = 2000; %SAMPLING FREQUENCY
fm = 1; %MESSAGE FREQUENCY
tau = 0.2;
m=6;
T=1/fs;
t=0:1/fs:((2/fm)-(1/fs));
n_max = 8;
Y = zeros(numel(t),n_max);
for n=1:n_max
Y(:,n)=(tau/T).*(1+m*sin(2*pi*fm*t))+(2/n*pi).*sin(n*2*pi*fs).*(tau/2+(m*tau/2).*sin(2*pi*fm*t)).*cos(n*2*pi*fs*t);
end
plot(Y)

类别

Help CenterFile Exchange 中查找有关 Mathematics and Optimization 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by