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');
  댓글 수: 1
Sam Chak
Sam Chak 2024년 8월 30일
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.

댓글을 달려면 로그인하십시오.

답변 (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
Sam Chak
Sam Chak 2024년 8월 30일
Hi Shivam, you can actually copy/paste the code and run it online in the forum.

댓글을 달려면 로그인하십시오.

카테고리

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