Overlaying three functions on a single plot

% Peter Daniel Bohlen
% Mech3080 hw2
% Part 2E
t = 0: 0.01:6;
x = (6 * exp(-0.5 * t)) * cos((5 * pi * t) + (pi/4));
y = -1.5 * sin(5.5 * pi *t);
z = (6 * exp(-0.5*t))* cos((5 * pi *t) + (pi/4)) - (1.5 * sin(5.5 * pi *t));
plot (t,x)
plot(t,y)
plot(t,z)
grid on
xlabel 'Time (sec)'
ylabel 'Displacement (in)'
title 'Homework 2 Part 2E - Displacement vs. Time'
it is returning an error of this :
Error in hw2part2E (line 6)
x = (6 * exp(-0.5 * t)) * cos((5 * pi * t) + (pi/4));

 Respuesta aceptada

To avoid the error, use element-wise multiplication (.*), because * is for matrix multiplication.
To plot multiple functions in a single plot, use hold on.
% Peter Daniel Bohlen
% Mech3080 hw2
% Part 2E
t = 0: 0.01:6;
x = (6 * exp(-0.5 * t)) .* cos((5 * pi * t) + (pi/4));
% ^^ element-wise
y = -1.5 * sin(5.5 * pi *t);
z = (6 * exp(-0.5*t)).* cos((5 * pi *t) + (pi/4)) - (1.5 * sin(5.5 * pi *t));
% ^^ element-wise
figure
hold on
plot (t,x)
plot(t,y)
plot(t,z)
grid on
xlabel 'Time (sec)'
ylabel 'Displacement (in)'
title 'Homework 2 Part 2E - Displacement vs. Time'

3 comentarios

Voss
Voss el 13 de Mayo de 2024
Editada: Voss el 13 de Mayo de 2024
In this case, since the x, y, and z vectors are all defined in terms of the same vector t, an alternative to using hold on is to plot them all in a single plot call:
% Peter Daniel Bohlen
% Mech3080 hw2
% Part 2E
t = 0: 0.01:6;
x = (6 * exp(-0.5 * t)) .* cos((5 * pi * t) + (pi/4));
y = -1.5 * sin(5.5 * pi *t);
z = (6 * exp(-0.5*t)).* cos((5 * pi *t) + (pi/4)) - (1.5 * sin(5.5 * pi *t));
plot(t,[x; y; z].')
grid on
xlabel 'Time (sec)'
ylabel 'Displacement (in)'
title 'Homework 2 Part 2E - Displacement vs. Time'
Peter Bohlen
Peter Bohlen el 13 de Mayo de 2024
Thank you very much!
Voss
Voss el 13 de Mayo de 2024
You're welcome! Any questions, let me know. Otherwise, please "Accept" this answer. Thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 13 de Mayo de 2024

Editada:

el 13 de Mayo de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by