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
2022년 1월 12일
So you have 4 equations. What are the 4 unknowns ?
Tom Goodland
2022년 1월 12일
Tom Goodland
2022년 1월 12일
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일
답변 (3개)
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));
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일
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
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일
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일
Tom Goodland
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 ?
Walter Roberson
2022년 1월 13일
Yes, you are right, thanks.
Tom Goodland
2022년 1월 15일
Torsten
2022년 1월 15일
If we don't have the missing constants, we can't answer how to do better.
Tom Goodland
2022년 1월 15일
Torsten
2022년 1월 15일
The values for the parameters with ... in the code.
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일
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
2022년 1월 15일
Tom Goodland
2022년 1월 15일
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...
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]
sol = solve(eqns, [a, x_pdo, x_glycerol x_water])
sols = [sol.a, sol.x_pdo, sol.x_glycerol, sol.x_water]
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)
카테고리
도움말 센터 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


