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

2 次查看(过去 30 天)
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');

回答(2 个)

Alan Stevens
Alan Stevens 2024-8-30
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 2024-8-30
编辑:Shivam Gothi 2024-8-30
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 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by