help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Solve multiple non-linear equations with function handles


From: Ben Abbott
Subject: Re: Solve multiple non-linear equations with function handles
Date: Mon, 27 Apr 2015 19:33:44 -0400

> 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







reply via email to

[Prev in Thread] Current Thread [Next in Thread]