Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

clear variables

clc

syms x(t) y(t)


Dx = diff(x);
Dy = diff(y);

[x_t,y_t] = dsolve(Dx==0.25+(2/30)*y-(7/30)*x,x(0)==1,Dy==(7/30)*x-(2/30)*y-
(5/30)*y,y(0)==6)

%find the amount of salt in both tanks after 5 minutes


%tank x
x_form = @(t)(14^(1/2)*exp(-(t*(14^(1/2) + 7))/30)*((14^(1/2)*exp((t*(14^(1/2)
+ 7))/30))/(16*(14^(1/2)/30 + 7/30)) + (5*14^(1/2)*(9*14^(1/2) + 7))/
(8*(14^(1/2) - 7)*(14^(1/2) + 7))))/7 - (14^(1/2)*exp((t*(14^(1/2) -
7))/30)*((14^(1/2)*exp(-(t*(14^(1/2) - 7))/30))/(16*(14^(1/2)/30 - 7/30)) -
(35*(14^(1/2) - 18))/(8*(14^(1/2) - 7)*(14^(1/2) + 7))))/7

%tank y
y_form = @(t) - exp(-(t*(14^(1/2) + 7))/30)*((14^(1/2)*exp((t*(14^(1/2)
+ 7))/30))/(16*(14^(1/2)/30 + 7/30)) + (5*14^(1/2)*(9*14^(1/2)
+ 7))/(8*(14^(1/2) - 7)*(14^(1/2) + 7))) - exp((t*(14^(1/2) -
7))/30)*((14^(1/2)*exp(-(t*(14^(1/2) - 7))/30))/(16*(14^(1/2)/30 - 7/30)) -
(35*(14^(1/2) - 18))/(8*(14^(1/2) - 7)*(14^(1/2) + 7)))

%evaluate
x_sltamt = x_form (5)
y_sltamt = y_form (5)

%illustrate the behavior of each tank concentration as time proceeds to


%from 0 to 50 minutes with 0.05 interval
t_interval = [0:0.05:50];

%tank x
x_interval = subs(x_t,t_interval);
V_x = 30;
C_px = x_interval./V_x;;
%tank y
y_interval = subs(y_t,t_interval);
V_y = 30;
C_py = y_interval./V_y;

hold on
plot (t_interval,C_px)
plot (t_interval,C_py)
xlabel ('time')
ylabel ('concentration')
legend ('tank x','tank y')

%find the time when tank 2 will reach 0.07 kg salt/liter


format short
t_y = [0:0.05:50];

1
V_y = 30;
C_py = subs(y_t./V_y,t_y);

x = double (C_py);
y = t_y;

time = interp1(x,y,0.07,'linear')

x_t =

(14^(1/2)*exp(-(t*(14^(1/2) + 7))/30)*((14^(1/2)*exp((t*(14^(1/2) +
7))/30))/(16*(14^(1/2)/30 + 7/30)) + (5*14^(1/2)*(9*14^(1/2) + 7))/
(8*(14^(1/2) - 7)*(14^(1/2) + 7))))/7 - (14^(1/2)*exp((t*(14^(1/2) -
7))/30)*((14^(1/2)*exp(-(t*(14^(1/2) - 7))/30))/(16*(14^(1/2)/30 - 7/30)) -
(35*(14^(1/2) - 18))/(8*(14^(1/2) - 7)*(14^(1/2) + 7))))/7

y_t =

- exp(-(t*(14^(1/2) + 7))/30)*((14^(1/2)*exp((t*(14^(1/2) + 7))/30))/


(16*(14^(1/2)/30 + 7/30)) + (5*14^(1/2)*(9*14^(1/2) + 7))/(8*(14^(1/2) -
7)*(14^(1/2) + 7))) - exp((t*(14^(1/2) - 7))/30)*((14^(1/2)*exp(-(t*(14^(1/2)
- 7))/30))/(16*(14^(1/2)/30 - 7/30)) - (35*(14^(1/2) - 18))/(8*(14^(1/2) -
7)*(14^(1/2) + 7)))

x_form =

function_handle with value:

@(t)(14^(1/2)*exp(-
(t*(14^(1/2)+7))/30)*((14^(1/2)*exp((t*(14^(1/2)+7))/30))/
(16*(14^(1/2)/30+7/30))+(5*14^(1/2)*(9*14^(1/2)+7))/
(8*(14^(1/2)-7)*(14^(1/2)+7))))/7-
(14^(1/2)*exp((t*(14^(1/2)-7))/30)*((14^(1/2)*exp(-(t*(14^(1/2)-7))/30))/
(16*(14^(1/2)/30-7/30))-(35*(14^(1/2)-18))/(8*(14^(1/2)-7)*(14^(1/2)+7))))/7

y_form =

function_handle with value:

@(t)-exp(-(t*(14^(1/2)+7))/30)*((14^(1/2)*exp((t*(14^(1/2)+7))/30))/
(16*(14^(1/2)/30+7/30))+(5*14^(1/2)*(9*14^(1/2)+7))/
(8*(14^(1/2)-7)*(14^(1/2)+7)))-exp((t*(14^(1/2)-7))/30)*((14^(1/2)*exp(-
(t*(14^(1/2)-7))/30))/(16*(14^(1/2)/30-7/30))-(35*(14^(1/2)-18))/
(8*(14^(1/2)-7)*(14^(1/2)+7)))

x_sltamt =

1.8110

2
y_sltamt =

2.9891

time =

10.9052

Published with MATLAB® R2021b

You might also like