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
댓글 수: 0
채택된 답변
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)
figure
surf(t, (1:8), Y, 'EdgeColor','none')
colormap(turbo)
xlabel('t')
ylabel('n')
zlabel('Y')
.
추가 답변 (1개)
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 Center 및 File Exchange에서 Mathematics and Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!