Evaluating a complex equation

2 ビュー (過去 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 Center および File ExchangeMathematics and Optimization についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by