MATLAB Answers

How to plot a (which is changing from 0-1 in 0.01 increments) vs x(2) (using a for loop and fsolve to find the solution of a nonlinear equation containing x(s) sol based on a

9 views (last 30 days)
Victor Jimenez Carrillo
Victor Jimenez Carrillo on 16 Sep 2021 at 5:16
Commented: Alan Stevens on 16 Sep 2021 at 14:17
V=1000; Q=50; Ca0=1; k=1;
for a=0:1:0.1
f=@(x) [Q*Ca0-Q*x(1)-k*x(1)^2*(a*V); Q*x(1)-Q*x(2)-k*x(2)^2*(1-a)*V];
fsolve(f,[0.5,0.5])
end
plot(a,x(2))

Answers (1)

Alan Stevens
Alan Stevens on 16 Sep 2021 at 9:35
Your first equation is a simple quadratic in x(1); your second is a quadratic in x(2) that depends on x(1), so, assuming you are only interested in the positive roots, these can be solved as follows:
V=1000; Q=50; Ca0=1; k=1;
a = 0:0.01:1;
x1 = zeros(1,numel(a));
x2 = zeros(1,numel(a));
for i=1:numel(a)
% assuming you want positive values of x1 and x2
if a(i) == 0
A = k*V;
x1(i) = Ca0;
x2(i) = (-Q + sqrt(Q^2 + 4*A*Q*x1(i)))/(2*A);
elseif a(i) == 1
A = k*V;
x1(i) = (-Q + sqrt(Q^2 + 4*A*Q*Ca0))/(2*A);
x2(i) = x1(i);
else
A1 = k*a(i)*V;
A2 = k*(1-a(i))*V;
x1(i) = (-Q + sqrt(Q^2 + 4*A1*Q*Ca0))/(2*A1);
x2(i) = (-Q + sqrt(Q^2 + 4*A2*Q*x1(i)))/(2*A2);
end
end
subplot(2,1,1)
plot(a,x1),grid
xlabel('a'),ylabel('x1')
subplot(2,1,2)
plot(a,x2),grid
xlabel('a'),ylabel('x2')
% The two equations can be expressed as:
% k*a*V*x1^2 + Q*x1 - Q*Ca0 = 0
% k*(1-a)*V*x2^2 + Q*x2 - Q*x1 = 0
  2 Comments
Alan Stevens
Alan Stevens on 16 Sep 2021 at 14:17
Your equations would then be cubic polynomials. Look up help on “roots” to see how to find the values.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by