Hi I'm trying to plot using a for loop. don't see syntax errors but plot comes out empty. appreciate your guidance.

for t = 0:30/100:30
y1 = 6*exp(-0.2)*t*sin(7*t+3);
y2 = 4*exp(-0.1)*t*sin(3*t-1);
end
plot(y1,t);
hold on;
plot(y2,t);
hold off;
legend ('y1(t)=6e^-0.2tsin(7t+3)','y2(t)=4e^-0.1tsin(3t-1)');
axis([0 30 -6 6])

 Respuesta aceptada

The plot comes out empty because you're only plotting the last point. Try it like this:
t = 0:30/100:30;
y1 = 6*exp(-0.2)*t.*sin(7*t+3);
y2 = 4*exp(-0.1)*t.*sin(3*t-1);
plot(y1,t);
hold on;
plot(y2,t);
hold off;
legend ('y1(t)=6e^-0.2tsin(7t+3)','y2(t)=4e^-0.1tsin(3t-1)');
axis([0 30 -6 6])

4 comentarios

Great. it works now. Thanks so much. so i can use for loop? the assignment asks to practice loops.
Yeah, you can use a for loop:
t = 0:30/100:30;
y1 = zeros(size(t));
y2 = zeros(size(t));
for i = 1:numel(t)
y1(i) = 6*exp(-0.2)*t(i)*sin(7*t(i)+3);
y2(i) = 4*exp(-0.1)*t(i)*sin(3*t(i)-1);
end
plot(y1,t);
hold on;
plot(y2,t);
hold off;
legend ('y1(t)=6e^-0.2tsin(7t+3)','y2(t)=4e^-0.1tsin(3t-1)');
axis([0 30 -6 6])
Can you click the "Accept this answer" link to give him his "reptuation points"?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 1 de Feb. de 2022

Comentada:

el 2 de Feb. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by