필터 지우기
필터 지우기

Evaluating a complex equation

조회 수: 1 (최근 30일)
Andy
Andy 2023년 8월 23일
댓글: Star Strider 2023년 8월 23일
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')
.
  댓글 수: 2
Andy
Andy 2023년 8월 23일
Thank you for your help, it makes sense now.
Star Strider
Star Strider 2023년 8월 23일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (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)
  댓글 수: 2
Andy
Andy 2023년 8월 23일
Thank you for your help, it makes sense now.
Voss
Voss 2023년 8월 23일
You're welcome!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Objects에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by