dsolve for multiple plots on a single figure

조회 수: 2(최근 30일)
Tony Rankin
Tony Rankin 2021년 6월 1일
댓글: Rik 2021년 6월 1일
I have the following code that works for Kc = 1 and plots well enough. But I want to also plot Kc for 2, 3, 4 and 5 as well, and I don't know how to do this.
clear all; clc; % clear workspace and editor, respectively
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A = 2; % cross-sectional area (m^2)
T = 0.1; % integral time constant (min)
Kc_1 = 1; % proportional gain (m^2/min)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
syms y(x) % symbolic variable with deviation in height of liquid, y, and time, x
dy = diff(y); % dy is the differential of the deviation in height of liquid, y
ODE = A * diff(y,x,2)+ Kc_1 * diff(y,x)+((Kc_1/T)*y) == 0; % second-order ODE for a PI controller
cond1 = y(0) == 0; % first condition
cond2 = dy(0) == 2; % second condition
conds = [cond1 cond2];
ySol(x) = dsolve(ODE,conds);
ySol = simplify(ySol);
fplot(ySol,'b')
legend('Kc = 1')
title('Proportional-Integral Control of Liquid Height in Tank')
xlabel('Time (min)')
xlim([0,25])
ylabel('Deviation Height (m)')
ylim([-0.6 0.8])
set (gca, 'fontsize', 20)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  댓글 수: 1
Rik
Rik 2021년 6월 1일
Backup of this question:
dsolve for multiple plots on a single figure
I have the following code that works for Kc = 1 and plots well enough. But I want to also plot Kc for 2, 3, 4 and 5 as well, and I don't know how to do this.
clear all; clc; % clear workspace and editor, respectively
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A = 2; % cross-sectional area (m^2)
T = 0.1; % integral time constant (min)
Kc_1 = 1; % proportional gain (m^2/min)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
syms y(x) % symbolic variable with deviation in height of liquid, y, and time, x
dy = diff(y); % dy is the differential of the deviation in height of liquid, y
ODE = A * diff(y,x,2)+ Kc_1 * diff(y,x)+((Kc_1/T)*y) == 0; % second-order ODE for a PI controller
cond1 = y(0) == 0; % first condition
cond2 = dy(0) == 2; % second condition
conds = [cond1 cond2];
ySol(x) = dsolve(ODE,conds);
ySol = simplify(ySol);
fplot(ySol,'b')
legend('Kc = 1')
title('Proportional-Integral Control of Liquid Height in Tank')
xlabel('Time (min)')
xlim([0,25])
ylabel('Deviation Height (m)')
ylim([-0.6 0.8])
set (gca, 'fontsize', 20)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

답변(1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 6월 1일
편집: KALYAN ACHARJYA 2021년 6월 1일
  1. Using Loop
A = 2; % cross-sectional area (m^2)
T = 0.1; % integral time constant (min)
Kc_1 = 1:5;
for i=1:length(Kc_1)
code
%replace Kc_1 with Kc_1(i)...........
legend(['Kc =',num2str(i)])
hold on
end
title('Proportional-Integral Control of Liquid Height in Tank')
xlabel('Time (min)')
xlim([0,25])
ylabel('Deviation Height (m)')
ylim([-0.6 0.8])
set (gca, 'fontsize', 20)
2. or Create an another function file & pass the Kc_1 as input argument.
3. or Create function handle & substitute Kc_1 value after solve.

Community Treasure Hunt

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

Start Hunting!

Translated by