Errors while trying to setup equation for root finding.

I am trying to set up an equation for root finding to find a, however in the code at the bottom i get an error saying Parse error: Parse error at '=' . usage might be invalid syntax. Does anyone know how to fix this? I'd be grateful for any help.
x_pdo = z_pdo/(1 + a(k_pdo - 1));
x_water = z_water/(1 + a(k_water - 1));
x_glycerol = z_glycerol/(1 + a(k_glycerol - 1));
x_pdo + x_water + x_glycerol - 1 = 0;

6 件のコメント

Torsten
Torsten 2022 年 1 月 12 日
So you have 4 equations. What are the 4 unknowns ?
Tom Goodland
Tom Goodland 2022 年 1 月 12 日
all x_... are known, all z_... are known, all k_... are known, the only unknown is a, I am trying to get the function to equal 0 so i can do root finding to calculate a but im not too sure how.
The last equation includes the sum of the first 3 equations - 1 = 0.
Torsten
Torsten 2022 年 1 月 12 日
編集済み: Torsten 2022 年 1 月 12 日
But to determine one variable "a", you only need one equation.
Or do you want to determine "a" such that it satisfies all three equations approximately ?
Tom Goodland
Tom Goodland 2022 年 1 月 12 日
I just added the first 3 as a bit of background knowledge so people can see how the x_ was calculated, it is the bottom equation I am trying to use to determine a.
Torsten
Torsten 2022 年 1 月 12 日
If you have to insert the first three equations into the last to solve for a, also the x_... are unknown.
Otherwise, you could just pick one of the three equations at the top and solve for a.
Tom Goodland
Tom Goodland 2022 年 1 月 12 日
my mistake yeah the x_... are unknown, do you know what function I should use to try to determine a (constant) and x_...? I'm pretty sure fzero would work but I get the error a is an unrecognised function or variable. Do you know any code that would be able to calculate a from the code i've posted as information, if you need anymore information let me know.
Thanks for the help

サインインしてコメントする。

回答 (3 件)

James Tursa
James Tursa 2022 年 1 月 12 日

0 投票

Did you mean multiply by the "a"?
x_pdo = z_pdo/(1 + a*(k_pdo - 1));
x_water = z_water/(1 + a*(k_water - 1));
x_glycerol = z_glycerol/(1 + a*(k_glycerol - 1));

1 件のコメント

Tom Goodland
Tom Goodland 2022 年 1 月 12 日
yes do you have to put the * to multiply a by the bracket?

サインインしてコメントする。

Torsten
Torsten 2022 年 1 月 12 日
編集済み: Torsten 2022 年 1 月 12 日

0 投票

function main
a0 = 1;
a = fzero(@fun,a0)
end
function res = fun(a)
z_pdo = ...;
k_pdo = ...;
z_water = ...;
k_water = ...;
z_glycerol = ...;
k_glycerol = ...;
res = z_pdo/(1 + a*(k_pdo - 1)) + z_water/(1 + a*(k_water - 1)) + z_glycerol/(1 + a*(k_glycerol - 1)) -1.0;
end

22 件のコメント

Tom Goodland
Tom Goodland 2022 年 1 月 12 日
68 function main
69 a0 = 1;
70 a = fzero(@fun,a0);
71 end
72 function res = fun(a)
73 res = z_pdo/(1 + a*(k_pdo - 1)) + z_water/(1 + a*(k_water - 1)) + z_glycerol/(1 + a*(k_glycerol - 1)) -1.0;
74 end
I get 2 errors from this code it says: line 70 the value to variable 'a' might be unused and line 68 the function main might be unused.
I got rid of the k_glycerol = .. etc. because I have already written that higher up the script.
Do you know why these errors are showing up?
Thanks
Walter Roberson
Walter Roberson 2022 年 1 月 12 日
Those are both warnings, not errors.
The MATLAB code analyzer encourages the syntax
function main
a0 = 1;
a = fzero(@fun,a0);
disp(a)
end
Torsten
Torsten 2022 年 1 月 12 日
編集済み: Torsten 2022 年 1 月 12 日
The variables k_...,z_... are not visible in fun - you will have to transfer them to fun from your calling program.
Furthermore, you will have to call main from your program. The code then looks like
function [a] = main(z_pdo,k_pdo,z_water,k_water,z_glycerol,k_glyzerol,a0)
a = fzero(@(a)fun(a,z_pdo,k_pdo,z_water,k_water,z_glycerol,k_glyzerol),a0)
end
function res = fun(a,z_pdo,k_pdo,z_water,k_water,z_glycerol,k_glyzerol)
res = z_pdo/(1 + a*(k_pdo - 1)) + z_water/(1 + a*(k_water - 1)) + z_glycerol/(1 + a*(k_glycerol - 1)) -1.0;
end
or you can call fzero directly somewhere in your program:
...
a0 = 1.0;
a = fzero(@(a)fun(a,z_pdo,k_pdo,z_water,k_water,z_glycerol,k_glyzerol),a0)
...
Putting z- an k- values in arrays is also an option:
z(1) corresponds to z_pdo, e.g.
z(2) corresponds to z_water, e.g.
z(3) corresponds to z_glycerol, e.g
(same for k)
Tom Goodland
Tom Goodland 2022 年 1 月 12 日
I'm still getting the error unrecognised variable or function 'a' on this line: x_pdo = z_pdo/(1 + a*(k_pdo - 1));
I don't know how to fix this as I don't know how to define a without having a value for it. Do you know how?
Thanks
Torsten
Torsten 2022 年 1 月 13 日
I'm still getting the error unrecognised variable or function 'a' on this line: x_pdo = z_pdo/(1 + a*(k_pdo - 1));
I don't see this line anywhere in the code I submitted. So I can't give you advice what's going wrong.
Walter Roberson
Walter Roberson 2022 年 1 月 13 日
I'm still getting the error unrecognised variable or function 'a' on this line: x_pdo = z_pdo/(1 + a*(k_pdo - 1));
You should not be using that code.
You should either use the symbolic code that I posted, or you should use fsolve of a system of equations,
function residue = fun(xa, z_pdo,k_pdo,z_water,k_water,z_glycerol,k_glyzerol)
a = x(1);
x_pdo = x(2);
x_water = x(3);
x_gycerol = x(4);
eqn1 = x_pdo - z_pdo/(1 + a*(k_pdo - 1));
eqn2 = x_water - z_water/(1 + a*(k_water - 1));
eqn3 = x_glycerol - z_glycerol/(1 + a*(k_glycerol - 1));
eqn4 = x_pdo + x_water + x_glycerol - 1;
residue = [eqn1; eqn2; eqn3; eqn4];
end
Tom Goodland
Tom Goodland 2022 年 1 月 13 日
For the first line what is xa i don't see it anywhere else in the code and matlab says it's unused.
Thanks
Tom Goodland
Tom Goodland 2022 年 1 月 13 日
Also what should the function 'fun' be and do you know what the function handle should look like? Where should i define the function before calling it in on the top line?
Torsten
Torsten 2022 年 1 月 13 日
編集済み: Torsten 2022 年 1 月 13 日
Why don't you just open a MATLAB session, insert the values for z_... and k_... in "main" and run the below code ? If this does not give you a satisfactory result for "a" or some other problems appear, we can continue discussion.
"fun" is the function the root finder "fzero" expects where you define the equations that are to be solved.
To get used to MATLAB, maybe you should start with
function main
a0 = 1;
z_pdo = ...;
k_pdo = ...;
z_water = ...;
k_water = ...;
z_glycerol = ...;
k_glycerol = ...;
a = fzero(@(x)fun(x,k_pdo,k_water,k_glycerol,z_pdo,z_water,z_glycerol),a0)
end
function res = fun(a,k_pdo,k_water,k_glycerol,z_pdo,z_water,z_glycerol)
res = z_pdo/(1 + a*(k_pdo - 1)) + z_water/(1 + a*(k_water - 1)) + z_glycerol/(1 + a*(k_glycerol - 1)) -1.0;
end
Walter Roberson
Walter Roberson 2022 年 1 月 13 日
編集済み: Torsten 2022 年 1 月 15 日
%initialize as appropriate
z_pdo = ...;
k_pdo = ...;
z_water = ...;
k_water = ...;
z_glycerol = ...;
k_glycerol = ...;
a0 = ...;
x_pdo0 = ...;
x_water0 = ...;
x_gycerol0 = ...;
xa0 = [a0, x_pdo0, x_water0, x_gycerol0];
%now do the work
solution = fsolve(@(xa) fun(xa, z_pdo,k_pdo,z_water,k_water,z_glycerol,k_glyzerol), xa0);
%results
a = solution(1)
x_pdo = solution(2)
x_water = solution(3)
x_glycerol = solution(4)
function residue = fun(xa, z_pdo,k_pdo,z_water,k_water,z_glycerol,k_glyzerol)
a = xa(1);
x_pdo = xa(2);
x_water = xa(3);
x_glycerol = xa(4);
eqn1 = x_pdo - z_pdo/(1 + a*(k_pdo - 1));
eqn2 = x_water - z_water/(1 + a*(k_water - 1));
eqn3 = x_glycerol - z_glycerol/(1 + a*(k_glycerol - 1));
eqn4 = x_pdo + x_water + x_glycerol - 1;
residue = [eqn1; eqn2; eqn3; eqn4];
end
Torsten
Torsten 2022 年 1 月 13 日
@Walter Roberson I think you wanted to use "fsolve" instead of "fzero" in the above code ?
Walter Roberson
Walter Roberson 2022 年 1 月 13 日
Yes, you are right, thanks.
Tom Goodland
Tom Goodland 2022 年 1 月 15 日
When I ran your code walter matlab said:
Equation solved at initial point.
fsolve completed because the vector of function values at the initial point
is near zero as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
The x_glycerol, x_pdo etc. were the same values as x_glycerol0, x_pdo0 etc., do I have to set a tspan or something so that it doesn't solve at the initial point?
I would appreciate any help.
Thanks
Torsten
Torsten 2022 年 1 月 15 日
If we don't have the missing constants, we can't answer how to do better.
Tom Goodland
Tom Goodland 2022 年 1 月 15 日
What are the missing constants? Are you talking about x_glycerol0 etc. and k_glycerol etc. or a and x_glycerol etc.
Torsten
Torsten 2022 年 1 月 15 日
The values for the parameters with ... in the code.
Walter Roberson
Walter Roberson 2022 年 1 月 15 日
The constants that we are missing are:
z_pdo, k_pdo, z_water, k_water, z_glycerol, k_glycerol
and
a0, x_pdo0, x_water0, x_gycerol0
Tom Goodland
Tom Goodland 2022 年 1 月 15 日
z_pdo = 0.015, z_water = 0.975, z_glycerol = 0.01
a0 = 0, x_pdo0 = 0.015, x_water0 = 0.975, x_glycerol = 0.01
k_pdo = 0.1471 k_water = 4.4190 k_glycerol = 177.4713
Thanks
Torsten
Torsten 2022 年 1 月 15 日
編集済み: Torsten 2022 年 1 月 15 日
Yes, this data set gives
a = 0
and
x_pdo = z_pdo , x_water = z_water , x_glycerol = z_glycerol
using the code from above.
Tom Goodland
Tom Goodland 2022 年 1 月 15 日
The feed to the flash unit consists of 1 mol% glycerol, 1.5 mol% PDO and you can assume that the rest is water. The flash evaporator is operated at 2.75 bar and 185 °C.
X_ stands for liquid mole fraction, Z_ stands for mole fraction in the feed. Have I made a mistake in the discussed constants?
𝑥𝑗 = 𝑧𝑗/(1 + 𝛼(𝑘𝑗 − 1))
Tom Goodland
Tom Goodland 2022 年 1 月 15 日
α is the vapour to feed ratio (α = FV / FCwhere FC is the feed flow rate and FV is the vapour flow rate)
Walter Roberson
Walter Roberson 2022 年 1 月 16 日
if a is the same as α then α(kj-1) is 0 when α is 0, and 1+0 is 1, so xj = zj/stuff would be xj=zj/1...

サインインしてコメントする。

Walter Roberson
Walter Roberson 2022 年 1 月 12 日

0 投票

syms a k_pdo x_pdo x_glycerol z_pdo z_glycerol k_glycerol k_water x_water z_water
eqn1 = x_pdo == z_pdo/(1 + a*(k_pdo - 1));
eqn2 = x_water == z_water/(1 + a*(k_water - 1));
eqn3 = x_glycerol == z_glycerol/(1 + a*(k_glycerol - 1));
eqn4 = x_pdo + x_water + x_glycerol - 1 == 0;
eqns = [eqn1; eqn2; eqn3; eqn4]
eqns = 
sol = solve(eqns, [a, x_pdo, x_glycerol x_water])
sol = struct with fields:
a: [3×1 sym] x_pdo: [3×1 sym] x_glycerol: [3×1 sym] x_water: [3×1 sym]
sols = [sol.a, sol.x_pdo, sol.x_glycerol, sol.x_water]
sols = 
sol3 = solve(eqns, [a, x_pdo, x_glycerol x_water], 'maxdegree', 3);
sol3s = [sol3.a, sol3.x_pdo, sol3.x_glycerol, sol3.x_water];
vpa(sol3s)
ans = 

カテゴリ

ヘルプ センター および File ExchangeFunction Creation についてさらに検索

製品

リリース

R2021b

タグ

質問済み:

2022 年 1 月 12 日

コメント済み:

2022 年 1 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by