# want to change data on Script and combine Graph All-In-One

2 views (last 30 days)
재연 전 on 29 Mar 2022
Answered: Jaswanth on 10 Oct 2023
sorry for my poor english.
I want to change the Given properties 'C2' and want to show the result graph on same Figure.
under the scription, I want to combine 'if C2=0.15','if C2=0.18', 'if C2=0.20' graph, like an image.
==================================
% Given properties.
P1=80; P2=120;
T0=0.2;T=0.85;
R=1.0;L=0.011; C2=0.15;
a=R/L;b=1/R/C2;
f1=P1/L;f2=P2/L;
lambda=log(P2/P1)/(T-T0);
h1=(f2-f1)/T0; h2=f2*exp(lambda*T0);
w=sqrt(1/L/C2 - 1/(4*R^2*C2^2));
F1=-1/(R*C2)/(lambda*(lambda-1/(R*C2))+1/L/C2);
F2=-F1*(lambda-1/R/C2);
E1=-L/R; E2=-L/R^2/C2;
G1=-(L/R)^2; G2=L/R; G4=-L/R;
%% Calculation
syms n;
t=0:0.02:3;
A1 = [f1*E1,f1*(E2-E1*b/2)/w,-f1*E1,h1*G1,h1*G2,-h1*G1,h1*(G4+G1*b/2)/w];
A2 = [-f2*E1,-f2*(E2-E1*b/2)/w,f2*E1,-h1*G1,-h1*G2,h1*G1,-h1*(G4+G1*b/2)/w,h2*F1,h2*(F2-F1*b/2)/w,-h2*F1];
A3 = [-h2*F1,-h2*(F2-F1*b/2)/w,h2*F1];
t1 = t-n*T;t2=(t-n*T-T0); t3=t-(n+1)*T;
e1 = exp(-b*t1/2); c1=cos(w*t1); s1=sin(w*t1);
e2 = exp(-b*t2/2); c2=cos(w*t2); s2=sin(w*t2);
e3 = exp(-b*t3/2); c3=cos(w*t3); s3=sin(w*t3);
B1 = [ e1.*c1; e1.*s1; t.^0; t.^0; t1; e1.*c1; e1.*s1];
B2 = [ e2.*c2; e2.*s2; t.^0; t.^0; t2; e2.*c2; e2.*s2; ...
exp(-lambda*T0)*e2.*c2; exp(-lambda*T0)*e2.*s2; exp(-lambda*(t-n*T))];
B3 = [ exp(-lambda*T)*e3.*c3; exp(-lambda*T)*e3.*s3; exp(-lambda*(t-n*T))];
Q2 = double(symsum(heaviside(t1) .* (A1*B1) + heaviside(t2) .* (A2*B2) + ...
heaviside(t3) .* (A3*B3), n, 0, 4));
plot(t, Q2)
title('Q2'); xlabel('t [s]')
function y=u(x) %step function (vector I/O)
%y = boolean(x>1);
if x>0
y=0;
else
y=1;
end
end

Jaswanth on 10 Oct 2023
Greetings,
To vary the value of the given property and display the output graph on the same figure, you can use the "hold" function and a "for" loop in combination.
Please find the modified code below:
% Given properties.
P1 = 80; P2 = 120;
T0 = 0.2; T = 0.85;
R = 1.0; L = 0.011;
% Create a vector of given values of C2
C2 = [0.15, 0.18, 0.20];
% Use hold function before the for loop, through which each iteration
% of the loop will add a new line to the existing plot.
hold on
% Create a for loop. The code block within the for loop is executed three times,
% with X taking the values 1, 2, and 3. In each iteration, the corresponding value
% of C2 is used in the calculations.
for X = 1:3
% Using X as a loop counter, the code performs the calculations and plotting for each value of C2
a = R/L; b = 1/R/C2(X);
f1 = P1/L; f2 = P2/L;
lambda = log(P2/P1)/(T-T0);
h1 = (f2-f1)/T0; h2 = f2*exp(lambda*T0);
w = sqrt(1/L/C2(X) - 1/(4*R^2*C2(X)^2));
F1 = -1/(R*C2(X))/(lambda*(lambda-1/(R*C2(X)))+1/L/C2(X));
F2 = -F1*(lambda-1/R/C2(X));
E1 = -L/R; E2=-L/R^2/C2(X);
G1 = -(L/R)^2; G2=L/R; G4=-L/R;
%% Calculation
syms n;
t = 0:0.02:3;
A1 = [f1*E1, f1*(E2-E1*b/2)/w, -f1*E1, h1*G1, h1*G2, -h1*G1,h1*(G4+G1*b/2)/w];
A2 = [-f2*E1, -f2*(E2-E1*b/2)/w, f2*E1, -h1*G1, -h1*G2, h1*G1, -h1*(G4+G1*b/2)/w, h2*F1, h2*(F2-F1*b/2)/w, -h2*F1];
A3 = [-h2*F1, -h2*(F2-F1*b/2)/w, h2*F1];
t1 = t-n*T; t2=(t-n*T-T0); t3=t-(n+1)*T;
e1 = exp(-b*t1/2); c1=cos(w*t1); s1=sin(w*t1);
e2 = exp(-b*t2/2); c2=cos(w*t2); s2=sin(w*t2);
e3 = exp(-b*t3/2); c3=cos(w*t3); s3=sin(w*t3);
B1 = [ e1.*c1; e1.*s1; t.^0; t.^0; t1; e1.*c1; e1.*s1];
B2 = [ e2.*c2; e2.*s2; t.^0; t.^0; t2; e2.*c2; e2.*s2; ...
exp(-lambda*T0)*e2.*c2; exp(-lambda*T0)*e2.*s2; exp(-lambda*(t-n*T))];
B3 = [ exp(-lambda*T)*e3.*c3; exp(-lambda*T)*e3.*s3; exp(-lambda*(t-n*T))];
Q2 = double(symsum(heaviside(t1) .* (A1*B1) + heaviside(t2) .* (A2*B2) + ...
heaviside(t3) .* (A3*B3), n, 0, 4));
plot(t, Q2)
title('Q2'); xlabel('t [s]')
end
hold off
legend("C2 = 0.15","C2 = 0.18","C2 = 0.20")
function y = u(x) %step function (vector I/O)
%y = boolean(x>1);
if x > 0
y = 0;
else
y = 1;
end
end
Hope this helps.

### Categories

Find more on Function Creation in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!