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
古いコメントを表示
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
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 件)
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');
Shivam Gothi
2024 年 8 月 30 日
編集済み: Shivam Gothi
2024 年 8 月 30 日
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
2024 年 8 月 30 日
Note that the labels on the axes should be swapped, for proper display of info.
Shivam Gothi
2024 年 8 月 30 日
Thanks Sam Chak for pointing it out. I edited it.
Sam Chak
2024 年 8 月 30 日
Hi Shivam, you can actually copy/paste the code and run it online in the forum.
カテゴリ
ヘルプ センター および File Exchange で Title についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
