Solving a system of nonlinear equations
Show older comments

I have a system of equation like this, and I want to find x1 and x2 with different value of C (from 1 to 10). What functions and commands should I use?
I tried something like this , but it is taking forever to run.
syms x y c
K1=1.930*10^-4;
K2=5.528;
k = 0:1/100:1
eq =((x -y)/(10+2*x)*((3*x+y)/(10+2*x))^3)/((c-x)/(10+2*x)*(10-c-x-y)/(10+2*x)==K1);
eq2 = (y/(10+2*(x))*(3*(x)+y)/(10+2*x))/(x-y/(10+2*x)*((10-c-x-y)/(10+2*x))==K2);
vars = [x y];
[solx,soly]= vpasolve([(subs(eq,c,k)),subs(eq2,c,k)],vars,true)
Answers (1)
Sulaymon Eshkabilov
on 12 May 2019
Edited: Sulaymon Eshkabilov
on 12 May 2019
Hi,
Here is the solution to your problem in which C is taken to have values C = 1:10 that can have any values.
clearvars; clc
syms x y
K1=1.930*10^-4;
K2=5.528;
C = 1:10;
for ii=1:numel(C)
eq1 =((x -y)/(10+2*x)*((3*x+y)/(10+2*x))^3)/((C(ii)-x)/(10+2*x)*(10-C(ii)-x-y)/(10+2*x)==K1);
eq2 = (y/(10+2*(x))*(3*(x)+y)/(10+2*x))/(x-y/(10+2*x)*((10-C(ii)-x-y)/(10+2*x))==K2);
xy = solve(eq1, eq2);
X_sol(ii, :) = double(xy.x);
Y_sol(ii, :) = double(xy.y);
end
% Note the solutions of x and y are X_sol and Y_sol. Some of their values are complex.
Good luck.
4 Comments
madhan ravi
on 12 May 2019
Why is k defined ? , never seemed to be used anywhere.
madhan ravi
on 12 May 2019
Sulaymon Eshkabilov‘S answer moved here:
I don't see k in your equation. Indeed, k is not needed at all ... in my code.
madhan ravi
on 12 May 2019
Edited: madhan ravi
on 12 May 2019
Well, see the fifth line in your code. By the way, there’s no need to keep adding answer to make a comment, use comment on this answer .
Walter Roberson
on 12 May 2019
Be careful here. MATLAB is able to find 11 solutions for symbolic c, each with a condition under which it is true. The conditions are not necessarily all consistently true in the range 1 to 10, so it is possible that for any given c value that the number of results might not be the same as for previous rows, so the assignment to the row could fail. Whether this happens or not could depend on the exact values used by the assignment to C such as by linspace or colon operator.
Also you can increase precision by adding 'maxdegree', 3 to the solve() call
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!