Errors while trying to setup equation for root finding.
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
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.
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
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
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
2022-1-12
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 个评论
Torsten
2022-1-12
编辑:Torsten
2022-1-12
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
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
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
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
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
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
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
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
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
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
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
2022-1-13
@Walter Roberson I think you wanted to use "fsolve" instead of "fzero" in the above code ?
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
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.
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
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
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
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
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
2022-1-12
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 =
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)