I have an equation with two unknowns and want to solve it iteratively.

26 views (last 30 days)
Hi,
I am a total noob to Matlab and I am struggling with a program I am trying to create. Any help or constructive advice would be gratefully appreciated. Basically I have an equation with two unknowns and want to solve it iteratively. H_max, Q_max and elevation will be specified by the user. I want to find the values for H_axis and Q_axis such that the equation equals H_max (a small error is ok). Can anyone point out were I have gone wrong?
H_max = 100;
Q_max = 75;
elevation = 22;
H_axis = 0;
K = 10;
iteration = 0;
while (iteration - H_max)^2 > 0.1
for Q_axis = 0:0.1:Q_max
iteration = elevation - H_axis + (K + (H_max/ Q_max^2))*Q_axis^2;
end
H_axis = H_axis + 0.01;
end
Thank you very much for your time and help :-)
  2 Comments
Karl
Karl on 17 Mar 2013
I also know that the value for H_axis has to be equal or less than H_max and the value for Q_axis has to be equal or less than Q_max. I am basically trying to run through every possible value for Q_axis and H_axis such that that equation is balanced. I did just randomly pick the numbers though so I think that is probably were I am going wrong. In terms of coding though does this look like it should do what I am intending? Thanks again

Sign in to comment.

Answers (2)

Matt J
Matt J on 18 Mar 2013
Edited: Matt J on 18 Mar 2013
The equation you've shown
iteration = elevation - H_axis + (K + (H_max/ Q_max^2))*Q_axis^2;
can be rewritten with H_axis a linear function of Q_axis^2,
H_axis = + (K + (H_max/ Q_max^2))*Q_axis^2 + (elevation - iteration);
= slope*Q_axis^2 + intercept
Thus, if Q_axis^2 can be anything in the interval [a,b], then H_axis can be anything in the interval [c,d] given by
Hrange = slope*[a,b]+intercept;
c=Hrange(1);
d=min(Hrange(2), H_max);

onur karakurt
onur karakurt on 25 Sep 2018
Function [ g ]=fun (T,P)
syms g T
EOS=((3138188779288823*T*g^2)/2305843009213693952 + (7768083267522915*T*g^3)/73786976294838206464 + (1419103455063989*T*g^4)/18889465931478580854784 - (5492730880387469*g^2)/2251799813685248 - (3209597054585569*g^3)/288230376151711744 + (2671748989973527*g^4)/1152921504606846976 + (1802332899834869*g^5)/36893488147419103232.........
g=((vpasolve(EOS == P, g, [0 inf])))
end
I want to obtain "g" variable accurately from the above equation. When I used "syms " (sembolic), I couldnt take the answer from the "Function file". How can I write the code for solving this type equation in function file.
thanks
  5 Comments
onur karakurt
onur karakurt on 25 Sep 2018
No in equation "EOS" there are 2 unknowns "g and T" , but T unknowns will come from this expression"[ g ] = fun (T,P)". EOS is non lineer and nonpolinomial equation. which code will I use for this
John D'Errico
John D'Errico on 26 Sep 2018
Edited: John D'Errico on 26 Sep 2018
In fun, there is ONE equation, that for EOS. It is a function of TWO variables. You are defining T in there as an unknown, symbolic. Even though you may think you are passing in T into fun, you then overwrite it as an unknown symbolic variable.
Then you try to solve for g. But EOS is a 5th order polynomial in g, even if T were known. There will be no analytical solution. And vpasolve CANNOT be used, since T is unknown.
Look more closely at EOS.
pretty(vpa(EOS,5))
2 3 -8 4 2 3 4 5
0.001361 T g + 0.00010528 T g + 7.5127 10 T g - 2.4393 g - 0.011136 g + 0.0023174 g + 0.000048852 g
Now, if T is known, supplied as a value to fun, then we can substitute it into EOS. In that case, we have a 5th degree polynomial in T. It is known
https://en.wikipedia.org/wiki/Abel–Ruffini_theorem
there is no solution to a general equation of that form (or one of higher order) in terms of roots and algebraic manipulations. Yes, there may actually be a solution to specific cases, but not for the fully general case. And worse, the ..... at the end of that line you show probably indicates higher order terms. That makes the solution even more improbable.
Of course, vpasolve can still handle it, as long as T is fully known. I.e., T has a value that will be substituted in.
But you then redefine T as symbolic. That steps on any value you would have supplied. It overwrites T. Don't believe me? TRY IT!!!!!!!!!!
T = 17;
syms T
T
T =
T
So here I defined T as the number 17, THEN I defined it as a sym. What value does T now have? NOTHING. It is not a number with value 17, even in symbolic form. T no longer has the original value it was supplied as. T is just an unknown symbolic.
That means that EOS is a GENERAL FUNCTION OF TWO UNKNOWN VARIABLES. That you passed in something is completely irrelevant. You then stepped on it.
There is NO solution for g, as a function of T. Once you supply a value for T, you cannot overwrite T and hope to solve the problem. You MAY be able to solve for g if T is supplied, IF you use vpasolve. Depending on the form of EOS, there may or may not be a solution. We cannot determine that, because you never showed us the complete form for EOS.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!