Evaluating a complex equation

2 visualizaciones (últimos 30 días)
Andy
Andy el 23 de Ag. de 2023
Comentada: Star Strider el 23 de Ag. de 2023
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

Respuesta aceptada

Star Strider
Star Strider el 23 de Ag. de 2023
Editada: Star Strider el 23 de Ag. de 2023
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 comentarios
Andy
Andy el 23 de Ag. de 2023
Thank you for your help, it makes sense now.
Star Strider
Star Strider el 23 de Ag. de 2023
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

Voss
Voss el 23 de Ag. de 2023
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 comentarios
Andy
Andy el 23 de Ag. de 2023
Thank you for your help, it makes sense now.
Voss
Voss el 23 de Ag. de 2023
You're welcome!

Iniciar sesión para comentar.

Categorías

Más información sobre Mathematics and Optimization en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by