How can I combine these two MATLAB codes? (MATLAB)

조회 수: 8 (최근 30일)
Santa Fe
Santa Fe 2021년 1월 7일
댓글: Adam Danz 2021년 1월 8일
I want to show step function and ramp function in one matlab code.
clc; %unit step function
clear;
G1 = tf([1], [5, 1]);
G2 = tf([1], [1, 0]);
G3 = series(5, G1);
G4 = feedback(G3, 0.8);
G = series(G4, G2);
sys = feedback(G, 1);
t = 0:0.001:25;
r = ones(1, length(t));
y2 = lsim(sys, r, t);
plot(t, y2, 'linewidth', 2);
grid;
xlabel('Time, t (s)');
ylabel('Amplitude');
title('Plot of y_2(t)');
clc; %ramp function
clear all;
close all;
s=tf('s');
C=5*(1+0.8*s)
P=1/(s*(5*s+1))
CL=feedback(C*P,1)
figure;
step(CL)
t=0:0.1:10;
r=t;
[y,t]=lsim(CL,r,t);
figure;
plot(t,y,'linewidth',2);
title('Ramp Response');

채택된 답변

Adam Danz
Adam Danz 2021년 1월 7일
편집: Adam Danz 2021년 1월 7일
This should get you started,
s=tf('s');
C=5*(1+0.8*s);
P=1/(s*(5*s+1));
CL=feedback(C*P,1);
figure
step(CL)
ax = gca();
hold(ax,'on')
G1 = tf([1], [5, 1]);
G2 = tf([1], [1, 0]);
G3 = series(5, G1);
G4 = feedback(G3, 0.8);
G = series(G4, G2);
sys = feedback(G, 1);
t = 0:0.001:25;
r = ones(1, length(t));
y2 = lsim(sys, r, t);
plot(t, y2, 'linewidth', 2,'DisplayName','y2');
t=0:0.1:10;
r=t;
[y,t]=lsim(CL,r,t);
plot(t,y,'linewidth',2,'DisplayName','Ramp Response');
ylim(ax,[-inf,max(y)])
% The first legend string is based on your variable name "CL".
legend('Location','NorthWest')
Alternatively,
[yResp, TOut] = step(CL);
plot(TOut, yResp)

추가 답변 (1개)

the cyclist
the cyclist 2021년 1월 7일
Use the hold command
figure
hold on
plot(t, y2, 'linewidth', 2);
plot(t,y,'linewidth',2);
and then your other commands for labels, etc
  댓글 수: 1
Adam Danz
Adam Danz 2021년 1월 8일
Unfortunately there's no way to prevent the step() function from producing its own figure unless outputs are included which won't produce a plot at all.

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

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by