How to solve for three answers from 3 equations using fsolve

7 views (last 30 days)
I have three equations:
I've attempted to solve them but i am unsure where i am going wromg
%function file
function n = moles(guess)
global NC NH NO NCH4 NCO NCO2 NH2 NH2O
mol1 = guess(1); mol2 = guess(2); mol3 = guess(3);
%equations
n(1) = NCH4 + NCO + NCO2 - NC;
n(2) = NCH4 + NH2 + NH2O - NH;
n(3) = NCO + 2*NCO2 + NH2;
end
%mainfile
%inputs
NC = 10;
NH = 50;
NO = 5
sol = fsolve('assignment1',guess);

Accepted Answer

Davide Masiello
Davide Masiello on 13 May 2022
Below, see an example of how to solve the system.
I had to make up the values of the constants since you have not specified them in your question.
clear,clc
% Constants (I made up the values)
molC = 1;
molH = 1;
molO = 1;
T = 600;
molCH4 = 1;
molCO = 1;
molCO2 = 1;
molH2 = 1;
molH2O = 1;
k1 = 1;
k2 = 1;
P = 1;
Po = 1;
% Initial guess
nguess = [2 1 1 3 2];
% Solver
sln = fsolve(@(x)moles(x,molC,molH,molO,T,k1,k2,molCH4,molCO,molCO2,molH2,molH2O,P,Po),nguess)
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
sln = 1×5
1.0e-15 * 0.0555 0.0278 0.0278 0.1110 0.0555
% Function
function n = moles(n,molC,molH,molO,T,k1,k2,molCH4,molCO,molCO2,molH2,molH2O,P,Po)
f = [ -n(1) + molCH4 + molCO + molCO2 - molC;...
-n(2) + 4*molCH4 + 2*molH2 + 2*molH2O - molH;...
-n(3) + molCO + 2*molCO2 + molH2;...
-n(4) + 3*log(molH2) + log(molCO) - log(molH2O) - log(molCH4) - log(k1);...
-n(5) + log(molH2) + log(molCO2) - log(molH2O) - log(molCO) - log(k2)
];
end

More Answers (0)

Categories

Find more on Periodic Waveform Generation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!