help-octave
[Top][All Lists]

## Re: Solve multiple non-linear equations with function handles

 From: jmb Subject: Re: Solve multiple non-linear equations with function handles Date: Wed, 29 Apr 2015 07:54:17 -0400 User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0

```On 04/27/2015 07:33 PM, Ben Abbott wrote:
>> On Apr 27, 2015, at 6:33 PM, jmb <address@hidden> wrote:
>>
>>
>> Hello,
>>
>> I am trying to use Octave 3.8 (Ubuntu 12.04 64bit) to solve multiple
>> non-linear simultaneous equations, using a functional handles as seen in
>> the example below:
>>
>> =======================================================================
>> #!/usr/bin/octave -q
>>
>> # Test of Function Handles in solving simultaneous non-linear equations:
>> P = 29.5;       # [bar]
>> y = 0.3;        # [-]
>> T = 300;        # [K]
>> h_g = @(H_g) h_g_calc(y, P);
>>
>> # Try with just ONE equation:
>> #
>> # Find a value of Y that satisfies the fictional Eqn: h_g (@ T = 300 &
>> an unknown Y) = Y * 2309.6
>> # The value 2309.6 was chosen to be close to the enthalpy, which is:
>> #        2309.6 [kJ/kg] @ y=0.3 [-] & P=29.5 [bar]
>> # The solution for Y, should be close to 1.0; since
>> #        2309.6 - 1.0*2309.6 = 0
>> #
>> # eq = @(Y) h_g(y, P) - Y*2309.6;    # This works!
>>
>> # -------------------------------------------
>> # Now let's try with 2 equations:
>> function Y = eq(y)
>>  eq(1) = @(Y(1)) h_g(y, P) - Y(1)*2309.6;
>>  eq(2) = @(Y(2)) h_g(y, P) - Y(2)*2*2309.6;
>> endfunction
>> #--------------------------------------------
>>
>> options=optimset('FunValCheck','on', 'Display','iter-detailed',
>> 'MaxIter',4000, 'TolFun',1.0e-10, 'TolX',1.0e-10);
>>
>> [Y,fval,exitflag]=fsolve(eq,0,options)
>> #======================================================================
>>
>> The version for a single function which is commented out works well.
>> Note that 'h_g_calc(y, P)' is call to an external function that I have
>> not included here for brevity.  Next I am trying to make it work for 2
>> or more equations.  My attempt at defining a function with eq(1), eq(2)
>> & Y(1), Y(2) fails the parsing step.
>>
>> Can I and how do I do this in Octave?  Thanks in advance for any help
>> you can offer.
>>
>> Regards, JMB
> I don’t think the code works are you expect.
>
> P = 29.5;       # [bar]
> y = 0.3;        # [-]
> T = 300;        # [K]
> h_g = @(H_g) h_g_calc(y, P);
>
> This is equivalent to writing …
>
> function a = h_g (H_g)
> P = 29.5;       # [bar]
> y = 0.3;        # [-]
> T = 300;        # [K]
> a = h_g_calc(y, P);
> end
>
> Notice that P, y, and T are effectively constants.
>
> Ben
>
You are correct that the example I used makes h_g effectively a
constant.  In the real cases I intend pursuing the enthalpy is a
function of temperature & mole fraction of the components in a binary
mixture.  So let's disregard the fact that h-g in the example is
effectively a constant for the present.

What I I am trying to do, is solve a set of simultaneous equations for
mass & thermal balance, where some of the temperatures & mass flow rates
are unknowns.  These equations have enthalpy values as multipliers.
Enthalpy depends on temperature and thus my quest to use function
handles that calculate the multipliers while iteratively solving the
simultaneous non-linear equations.  There are about 30 of them.