how to make for loop for sine waves and composite sine wave?

14 views (last 30 days)
X= [0.3, 0.5, 0.7, 2.5] ;
Y= [2, 4, 6, 8] ;
% Z= sum(Y*sin(X*t) --- would like each component overplotted
% SUM- copy-paste works - z= z1+ z2+ z3+ z4 (4 z's- how do we auto?)
t= linspace(0, 2*pi, 100) ;
z1= Y(1)*sin(X(1)*t) ;
z2= Y(2)*sin(X(2)*t) ;
z3= Y(3)*sin(X(3)*t) ;
z4= Y(4)*sin(X(3)*t) ;
figure
hold on
plot(t, z1)
plot(t, z2)
%etc.
hold off
I tried doing something like
t= linspace(0, 2*pi, 100) ; % need to make a t vector?
for i= 1: length(X) ;
for j= 1: length(Y) ;
Zn(ij)= Y*sin(X*t) ;
end
end
plot(t, Zn)
but it doesn't work. I understand why it is wrong but I don't really know how to fix it.

Answers (1)

Walter Roberson
Walter Roberson on 5 Mar 2020
X = [0.3, 0.5, 0.7, 2.5] ;
Y = [2, 4, 6, 8] ;
t = linspace(0, 2*pi, 100) ;
Z = sum(Y(:).* sin(X(:).* t));
plot(t,Z)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!