You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Errors while trying to setup equation for root finding.
2 views (last 30 days)
Show older comments
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 Comments
Tom Goodland
on 12 Jan 2022
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.
Tom Goodland
on 12 Jan 2022
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
on 12 Jan 2022
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
on 12 Jan 2022
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
Answers (3)
James Tursa
on 12 Jan 2022
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 Comment
Torsten
on 12 Jan 2022
Edited: Torsten
on 12 Jan 2022
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 Comments
Tom Goodland
on 12 Jan 2022
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
on 12 Jan 2022
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
on 12 Jan 2022
Edited: Torsten
on 12 Jan 2022
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
on 12 Jan 2022
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
on 13 Jan 2022
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
on 13 Jan 2022
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
on 13 Jan 2022
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
on 13 Jan 2022
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
on 13 Jan 2022
Edited: Torsten
on 13 Jan 2022
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
on 13 Jan 2022
Edited: Torsten
on 15 Jan 2022
%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
on 13 Jan 2022
@Walter Roberson I think you wanted to use "fsolve" instead of "fzero" in the above code ?
Tom Goodland
on 15 Jan 2022
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
Tom Goodland
on 15 Jan 2022
What are the missing constants? Are you talking about x_glycerol0 etc. and k_glycerol etc. or a and x_glycerol etc.
Walter Roberson
on 15 Jan 2022
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
on 15 Jan 2022
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
Tom Goodland
on 15 Jan 2022
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
on 15 Jan 2022
α is the vapour to feed ratio (α = FV / FCwhere FC is the feed flow rate and FV is the vapour flow rate)
Walter Roberson
on 16 Jan 2022
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
on 12 Jan 2022
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 =
See Also
Categories
Find more on Calculus 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)