이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Errors while trying to setup equation for root finding.
조회 수: 6 (최근 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에서 Function Creation에 대해 자세히 알아보기
태그
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 (한국어)
