i try to put two plot in one graph using legend but one of them just looks been 'cut off', can someone help me to fix this problem

3 vues (au cours des 30 derniers jours)
x0=2.1;
g=9.81;
k=45;
m=7;
t=0:0.1:6;
x1=@(t) x0*cosd(t*(sqrt(k/m)));
x_t_undamped=x1(t);
%ii
figure
plot(x_t_undamped,t,'-b');
xlabel('Position(m)');
ylabel('Time (s)');
title('Position of mass vs time');
hold on
%b
%i%ii%iii
dA=23;
v0=0;
A = dA / (2 * m);
B = sqrt(k / m - (dA/2*m)^2);
C2 = x0;
C1 = C2*B/ A;
x2 = @(t) exp(-A * t) .* (C1 * sind(B* t) + C2 * cosd(B* t));
x_t_underdamped=x2(t);
plot(t,x_t_underdamped, 'r-');
legend('undamped','underdamped')
grid on7
fprintf('The system is underdamped because the damping ratio is greater than 1.\n');
  1 commentaire
Sam Chak
Sam Chak le 30 Août 2024
Although the answers showed to you how to correctly plot the result, you should review whether to use sind for degree, or sin() for radian.

Connectez-vous pour commenter.

Réponses (2)

Alan Stevens
Alan Stevens le 30 Août 2024
Plot both curves the same way (i.e. t then x)!
x0=2.1;
g=9.81;
k=45;
m=7;
t=0:0.1:6;
x1=@(t) x0*cosd(t*(sqrt(k/m)));
x_t_undamped=x1(t);
%ii
subplot(2,1,1)
plot(t,x_t_undamped,'-b');
xlabel('Time(s)');
ylabel('Position (m)');
grid
title('Position of mass vs time undamped');
%b
%i%ii%iii
dA=23;
v0=0;
A = dA / (2 * m);
B = sqrt(k / m - (dA/2*m)^2);
C2 = x0;
C1 = C2*B/ A;
x2 = @(t) exp(-A * t) .* (C1 * sind(B* t) + C2 * cosd(B* t));
x_t_underdamped=x2(t);
subplot(2,1,2)
plot(t,x_t_underdamped, 'r-');
xlabel('Time(s)');
ylabel('Position (m)');
grid
title('Position of mass vs time underdamped');
fprintf('The system is underdamped because the damping ratio is greater than 1.\n');
The system is underdamped because the damping ratio is greater than 1.

Shivam Gothi
Shivam Gothi le 30 Août 2024
Modifié(e) : Shivam Gothi le 30 Août 2024
Hello @Elisa,
According to my understanding, you are trying to plot responses of underdamped and undamped system on same graph.
I went through your code and found that following changes are needed to be made
1) There is a typing mistake in the below given line of code:
plot(x_t_undamped,t,'-b');
The time variable "t" should be on x-axis and "x_t_undamped" on y-axis. Therefore, change the line of code to:
plot(t,x_t_undamped,'-b');
2) Increase the time span of plot
consider the below given line of your code.
t=0:0.1:6;
Change it to the line as shown below:
t=0:0.1:200;
Run the script again, you will now get a plot as shown below:
I hope this is what you expected.
  3 commentaires
Shivam Gothi
Shivam Gothi le 30 Août 2024
Thanks Sam Chak for pointing it out. I edited it.
Sam Chak
Sam Chak le 30 Août 2024
Hi Shivam, you can actually copy/paste the code and run it online in the forum.

Connectez-vous pour commenter.

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by