help-octave
[Top][All Lists]

## Re: solving non-linear set of equations

 From: Olaf Till Subject: Re: solving non-linear set of equations Date: Wed, 28 Sep 2011 15:51:59 +0200 User-agent: Mutt/1.5.20 (2009-06-14)

```On Wed, Sep 28, 2011 at 12:50:21PM +0200, Fritz Fischer wrote:
> Hello,
>
> I need to solve a set of non-linear equations and home
> that you can help me.
>
> The original plan is to write a function that recieves some
> variables:
>
> ---
> function heat_flux(irradiation, temperature)
> T_ambient_kelvin = 334
> T_sky_kelvin = 273
> ...
>
> ...
>
> end
> ---
>
> I want to solve a set of non-linear equations within
> this function. My plan was to use the following
> directly from the function above, but this
> does not seem to work.
>
> ----
> function y = myfun(x)
>
>  y(1) = x(3) -(( x(1) - T_ambient_kelvin ) * h_conv * area_pv_cell)
>  y(2) = x(4) - (( x(1)^4 - T_sky_kelvin^4) * h_pv_front_rad * area_pv_cell)
>  y(3) = x(5) - ((x(1) - x(2)) * thermal_conductivity_pv_cell * area_pv_cell)
>  y(4) = x(6) - (( x(2) - T_ambient_kelvin ) * h_conv * area_pv_cell)
>  y(5) = S_in - (x(3) + x(4) + x(5))
>  y(6) = x(5) - x(6)
>
> end

By the way: don't forget the semicolons after the assignments, eg:

y(1) = x(3) -(( x(1) - T_ambient_kelvin ) * h_conv * area_pv_cell);

or you will get much terminal output. The same probably for function
'heat_flux'.

>
> [x, info] = fsolve ("myfun", [10; 10; 10; 10; 10;10])
>
> ---
>
> I read somewhere

"somewhere"? There is an Octave manual!

> that I have to create a single file with myfunc. Is this
> correct?

This is one correct possibility.

> Or is there a way to use this from within the function heat_flux ?

1st possibility: Make a file with 'heat_flux' as main function and
'myfun' as subfunction and pass a _handle_ to 'myfun' in the call to
fsolve, so the file contains something like:

-- start of file --

function heat_flux (some arguments)

...

x = fsolve (@ myfun, [10; 10; 10; 10; 10; 10]);

endfunction

function y = myfun (x)

...

endfunction

-- end of file --

2nd possibility: Inside 'heat_flux', define an anonymous function
equivalent to 'myfun', e.g (note that the spaces within the
calculation are intetionally deleted here, though some of them would
not do harm):

function heat_flux (some arguments)

...

myfun = (x) [x(3)-(x(1)-T_ambient_kelvin)*h_conv*area_pv_cell; \
and so on for the other elements; \
...
...
...];

x = fsolve (myfun, [10; 10; 10; 10; 10; 10]);

endfunction

>
> If there is a way to use it from within, is it allowed to call the variables
> like T_sky_kelvin or T_ambient_kelvin ?
>
> If not, how do I pass the functions to myfun if i call is using
>
> [x, info] = fsolve ("myfun", [10; 10; 10; 10; 10;10])

Functions supplied by the user to 'fsolve' and others have a fixed
argument interface. The way to pass addiditonal variables to them is
by wrapping them into anonymous functions. For this, it is convenient
to have the additional variables as fields of one structure,
e.g. c.T_ambient_kelvin and c.T_sky_kelvin in your case, than one has
to pass only this structure (c).

For the wrapping, define 'my_fun' so that it accepts the additional
variables (e.g. with: function y = myfun (x, c)), define 'c' before
you call fsolve, and call fsolve like:

x = fsolve (@ (x) myfun (x, c), [10; 10; 10; 10; 10; 10]);

For a more complete example, the 1st possibility above changes to
this:

-- start of file --

function heat_flux (some arguments)

c.T_ambient_kelvin = some_value;
c.T_sky_kelvin = some_other_value;
...

x = fsolve (@ (x) myfun (x, c), [10; 10; 10; 10; 10; 10]);

endfunction

function y = myfun (x, c)

y = zeros (6, 1); # should be faster with that
y(1) = x(3) -(( x(1) - c.T_ambient_kelvin ) * c.h_conv * c.area_pv_cell);
y(2) = x(4) - (( x(1)^4 - c.T_sky_kelvin^4) * c.h_pv_front_rad *
c.area_pv_cell);
y(3) = x(5) - ((x(1) - x(2)) * c.thermal_conductivity_pv_cell *
c.area_pv_cell);
y(4) = x(6) - (( x(2) - c.T_ambient_kelvin ) * c.h_conv * c.area_pv_cell);
y(5) = S_in - (x(3) + x(4) + x(5));
y(6) = x(5) - x(6);

endfunction

-- end of file --

and the 2nd possibility above gets:

function heat_flux (some arguments)

c.T_ambient_kelvin = some_value;
c.T_sky_kelvin = some_other_value;
...

myfun = (x) [x(3)-(x(1)-c.T_ambient_kelvin)*c.h_conv*c.area_pv_cell; \
and so on for the other elements; \
...
...
...];

x = fsolve (myfun, [10; 10; 10; 10; 10; 10]);

endfunction

>
> Or am I doing something completly wrong??

No.

>
> Kind regards,
> Walter

> _______________________________________________
> Help-octave mailing list