Solving Non-Linear Equation in a loop
2 views (last 30 days)
Show older comments
Alakesh Upadhyaya
on 17 Nov 2020
Commented: Alakesh Upadhyaya
on 18 Dec 2020
I want to solve this equation for different values of E0, I want to vary the value of E0 from 1 to 100, for which I will get 99x3 equations+1 as E0 is present in 3 out of 4 equations given below. I can solve the equation for a fix value of E0 using VPA solve but I can't write a program to iterate the value of E0 for which I should get 99 different values of E,P,F and S from the equation for 99 different values of E0.
clc;clear;close;
syms E F S P
E00=1; %Total concetration of E0
F0=100; %TotalCOncentration of F0
k1=4; %rate constants
k2=6;
k3=4;
k4=1;
k5=6;
k6=4;
k7=5;
k8=8;
for i=1:99
E0(i)=E00+i;
eqn1=(E0-E)*(k1+k2)-k1*E*S-k4*E*P==0; % First equation
eqn2=(E0-E)*k3+(F0-F)*k6-E*S*k1-k8*S*F==0; %Second equation
eqn3=(F0-F)*(k7+k6)-F*P*k5-k8*S*F==0; %Third equation
eqn4=k2*(E0-E)+(F0-F)*k7-k5*F*P-k4*E*P==0; %Fourth Equation
sol(i)=solve([eqn1,eqn2,eqn3,eqn4,E>0,S>0,P>0,F>0,E<100,F<100,S<100,P<100],[E,P,S,F]);
end
heres my second code for a single value of E0
clc;clear;close;
%% Initial conditons and values of rate constants
syms E S F P
E0=1; %Total concetration of E0
F0=100; %TotalCOncentration of F0
k1=4; %rate constants
k2=6;
k3=4;
k4=1;
k5=6;
k6=4;
k7=5;
k8=8;
%% The equations
eqn1=(E0-E)*(k1+k2)-k1*E*S-k4*E*P==0; % First equation
eqn2=(E0-E)*k3+(F0-F)*k6-E*S*k1-k8*S*F==0; %Second equation
eqn3=(F0-F)*(k7+k6)-F*P*k5-k8*S*F==0; %Third equation
eqn4=k2*(E0-E)+(F0-F)*k7-k5*F*P-k4*E*P==0; %Fourth Equation
equations=[eqn1 eqn2 eqn3 eqn4]; %Total equations
vars=[ E S P F];
range=[0 E0-0.1;0.1 E0-0.1;0.1 E0-0.1;0.1 F0-0.1]; %Range of variables to omit unwanted values
% Range has been taken from 0 to E0-0.1 so that the value of E does not
% exceed the value of E0, same is done for S P and F
%% Solver
sol=vpasolve(equations,vars,range);
[sol.E sol.S sol.F sol.P];
%% Values
E=sol.E %Numerical value of E
P=sol.P %Numerical value of P
S=sol.S %Numerical value of S
F=sol.F %Mumerical value of F
digits(4);
x=P/(P+S)
b=E0/F0; %E0/F0
I just can't write a code for E0=1 to 99, I tried to put vpa into loop, but it doesn't work. Please help me out
0 Comments
Accepted Answer
Mahesh Taparia
on 17 Dec 2020
Hi
The function vpasolve returns the structure which can be stored in a cell array. Also there were some missing initialization. The below code will work for your problem:
clc;clear;close;
syms E F S P
E00=1; %Total concetration of E0
F0=100; %TotalCOncentration of F0
k1=4; %rate constants
k2=6;
k3=4;
k4=1;
k5=6;
k6=4;
k7=5;
k8=8;
E0=zeros(100,1);
sol=cell(100,1);
for i=1:99
E0(i)=E00+i;
eqn1=(E0(i)-E)*(k1+k2)-k1*E*S-k4*E*P==0; % First equation
eqn2=(E0(i)-E)*k3+(F0-F)*k6-E*S*k1-k8*S*F==0; %Second equation
eqn3=(F0-F)*(k7+k6)-F*P*k5-k8*S*F==0; %Third equation
eqn4=k2*(E0(i)-E)+(F0-F)*k7-k5*F*P-k4*E*P==0; %Fourth Equation
equations=[eqn1 eqn2 eqn3 eqn4]; %Total equations
vars=[ E S P F];
range=[0 E0(i)-0.1;0.1 E0(i)-0.1;0.1 E0(i)-0.1;0.1 F0-0.1]; %Range of variables to omit unwanted values
sol{i}=vpasolve(equations,vars,range);
end
Hope it will help!
More Answers (0)
See Also
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!