Must all coefficients for fsolve be numeric???????

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Must all coefficients for fsolve be numeric???????

ttonon
This post was updated on .
I try to define a set of nonlinear simultaneous equations that have
coefficients that I defined in statements made previously to the statements
that define "function" definition.  These coefficients were defined as
algebraic constants calculated from previous numerically defined parameters,
and immediately after the line defining them, their proper numeric
evaluation appeared: for instance, A = 0.5.  

I then used A in the function definition as a coefficient.  For instance, one term in one of the
"function" equations appears as A*X(1), where X(1) is one of several
independent variables to be solved for using the fsolve command.  When I
make the fsolve command, I get an error message saying A is undefined,
despite the previous "A = 0.5" appearing in a previous statement, showing
that "A" is stored as a numeric value in memory.  Why?  

Even more, I would
like to define other coefficients, such as the form B = X(1)*X(20); i.e.,
coefficients that are themselves functions of the independent variables.  

My
question is, is fsolve restricted to equations that have only numeric
constants as coefficients of the terms involving the independent variables?
If that's the case, is there any command in Octave that can solve any
nonlinear set of algebraic equations with coefficients that are previously
stored in memory, and defined as sub functions of the independent variables
of the equation set?



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Must all coefficients for fsolve be numeric???????

siko1056
On 3/16/20 7:38 AM, ttonon wrote:

> I try to define a set of nonlinear simultaneous equations that have
> coefficients that I defined in statements made previously to the statements
> that define "function" definition.  These coefficients were defined as
> algebraic constants calculated from previous numerically defined parameters,
> and immediately after the line defining them, their proper numeric
> evaluation appeared: for instance, A = 0.5.  I then used A in the function
> definition as a coefficient.  For instance, one term in one of the
> "function" equations appears as A*X(1), where X(1) is one of several
> independent variables to be solved for using the fsolve command.  When I
> make the fsolve command, I get an error message saying A is undefined,
> despite the previous "A = 0.5" appearing in a previous statement, showing
> that "A" is stored as a numeric value in memory.  Why?  Even more, I would
> like to define other coefficients, such as the form B = X(1)*X(20); i.e.,
> coefficients that are themselves functions of the independent variables.  My
> question is, is fsolve restricted to equations that have only numeric
> constants as coefficients of the terms involving the independent variables?
> If that's the case, is there any command in Octave that can solve any
> nonlinear set of algebraic equations with coefficients that are previously
> stored in memory, and defined as sub functions of the independent variables
> of the equation set?
>

Thank you for your verbose description.  I think it is much clearer, if
you sent us a meaningful excerpt of your code you try to apply to fsolve.

Kai



Reply | Threaded
Open this post in threaded view
|

Re: Must all coefficients for fsolve be numeric???????

ttonon
Thanks Kai, I should've known better.  Here's the relevant code, where the
... lines indicate missing lines.  Let me know if you need more detail, and
thanks again!  - Tom

...
...
>> Y_e = cosh(Alpha*X_bar) - cos(Alpha*X_bar) + Eta*(sinh(Alpha*X_bar) -
>> sin(Alpha*X_bar))
Y_e =  1.5875
...
...
>> function y = f (x)
y = zeros (12, 1);
y(1) = Y_e*X(1)*sin(X(2)) +dStat*X(5)/2 + O;
y(2) = X(4) -X(3) + Og_o;
y(3) = X(7);
y(4) = X(1)*Tau^2*(Og_o + X(3))*X(4) +Cf*X(8);
y(5) = X(9) -X(3)*Tau*Y_e*X(1);
y(6) = X(10) -abs(X(3)*Tau*Y_e*X(1))*L_cm/Tau;
y(7) = X(11) -W_cm/X(10);
y(8) = X(12) -Thic_cm/X(10);
y(9) = X(6) -Ogto*X(3)*X(11);
y(10) = X(13) -Mpi*(-D1)*((X(3)*tau*Y_e*X(1)))*(1 - sin(X(2)));
...
...
y(51) = X(8) -X(39) -X(40) -X(41) -X(42) -X(43) -X(44) -X(45) -X(46) -X(47)
-X(48) -X(49) -X(50)
 -X(51);
endfunction
>> [x, fval, info] = fsolve (@f,
>> [0.1;0.01;1024;4;-1.0;0.001;0.0;0.5;71000;0.000007;0.000005])
error: 'Y_e' undefined near line 3 column 8
error: called from
    f at line 3 column 6
    fsolve at line 236 column 8
>> y(1) = 1.5875*X(1)*sin(X(2)) +dStat*X(5)/2 + O;
error: 'X' undefined near line 1 column 15
>> y(1) = Y_e*X(1)*sin(X(2)) +dStat*X(5)/2 + O;
error: 'X' undefined near line 1 column 12
>> [x, fval, info] = fsolve (@f,
>> [0.1;0.01;1024;4;-1.0;0.001;0.0;0.5;71000;0.000007;0.000005])
error: 'Y_e' undefined near line 3 column 8
error: called from
    f at line 3 column 6
    fsolve at line 236 column 8
>>
>>




--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Must all coefficients for fsolve be numeric???????

Doug Stewart-4


On Tue, Mar 17, 2020 at 11:44 AM ttonon <[hidden email]> wrote:
Thanks Kai, I should've known better.  Here's the relevant code, where the
... lines indicate missing lines.  Let me know if you need more detail, and
thanks again!  - Tom

...
...
>> Y_e = cosh(Alpha*X_bar) - cos(Alpha*X_bar) + Eta*(sinh(Alpha*X_bar) -
>> sin(Alpha*X_bar))
Y_e =  1.5875
...
...
>> function y = f (x)

Octave is case sensitive  and you have a small x above this line and a large X below this line!!!
Change to large X every where!

y = zeros (12, 1);
y(1) = Y_e*X(1)*sin(X(2)) +dStat*X(5)/2 + O;
y(2) = X(4) -X(3) + Og_o;
y(3) = X(7);
y(4) = X(1)*Tau^2*(Og_o + X(3))*X(4) +Cf*X(8);
y(5) = X(9) -X(3)*Tau*Y_e*X(1);
y(6) = X(10) -abs(X(3)*Tau*Y_e*X(1))*L_cm/Tau;
y(7) = X(11) -W_cm/X(10);
y(8) = X(12) -Thic_cm/X(10);
y(9) = X(6) -Ogto*X(3)*X(11);
y(10) = X(13) -Mpi*(-D1)*((X(3)*tau*Y_e*X(1)))*(1 - sin(X(2)));
...
...
y(51) = X(8) -X(39) -X(40) -X(41) -X(42) -X(43) -X(44) -X(45) -X(46) -X(47)
-X(48) -X(49) -X(50)
 -X(51);
endfunction
>> [x, fval, info] = fsolve (@f,
>> [0.1;0.01;1024;4;-1.0;0.001;0.0;0.5;71000;0.000007;0.000005])
error: 'Y_e' undefined near line 3 column 8
error: called from
    f at line 3 column 6
    fsolve at line 236 column 8
>> y(1) = 1.5875*X(1)*sin(X(2)) +dStat*X(5)/2 + O;
error: 'X' undefined near line 1 column 15
>> y(1) = Y_e*X(1)*sin(X(2)) +dStat*X(5)/2 + O;
error: 'X' undefined near line 1 column 12
>> [x, fval, info] = fsolve (@f,
>> [0.1;0.01;1024;4;-1.0;0.001;0.0;0.5;71000;0.000007;0.000005])
error: 'Y_e' undefined near line 3 column 8
error: called from
    f at line 3 column 6
    fsolve at line 236 column 8
>>
>>




--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html




--
DASCertificate for 206392



Reply | Threaded
Open this post in threaded view
|

Re: Must all coefficients for fsolve be numeric???????

ttonon
Thanks Doug.  I corrected the x problem and also realized I didn't supply
initial guesses for all the X variables.  But I'm still not there.  Here's
the code:

>> function y = f (X)
y = zeros (12, 1);
y(1) = Y_e*X(1)*sin(X(2)) +dStat*X(5)/2 + O;
y(2) = X(4) -X(3) + Og_o;
y(3) = X(7);
y(4) = X(1)*Tau^2*(Og_o + X(3))*X(4) +Cf*X(8);
---
---
---
y(50) = X(7) -X(26) -X(27) -X(28) -X(29) -X(30) -X(31) -X(32) -X(33) -X(34)
-X(35) -X(36) -X(37) -X(38);
y(51) = X(8) -X(39) -X(40) -X(41) -X(42) -X(43) -X(44) -X(45) -X(46) -X(47)
-X(48) -X(49) -X(50) -X(51);
endfunction
>> [X, fval, info] = fsolve
>> (@f,[0.1;0.01;1024;4.;-1.0;0.001;0.0;0.5;154.;71000;0.000007;0.000005;
0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0])
error: 'Y_e' undefined near line 3 column 8
error: called from
    f at line 3 column 6
    fsolve at line 236 column 8
>>

I'm pressed for time and so can't read all of the manual I should.  I
appreciate your help.

Tom










--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Must all coefficients for fsolve be numeric???????

BGreen


On Wed, Mar 18, 2020 at 12:52 PM ttonon <[hidden email]> wrote:
Thanks Doug.  I corrected the x problem and also realized I didn't supply
initial guesses for all the X variables.  But I'm still not there.  Here's
the code:

>> function y = f (X)
y = zeros (12, 1);
y(1) = Y_e*X(1)*sin(X(2)) +dStat*X(5)/2 + O;
y(2) = X(4) -X(3) + Og_o;
y(3) = X(7);
y(4) = X(1)*Tau^2*(Og_o + X(3))*X(4) +Cf*X(8);
---
---
---
y(50) = X(7) -X(26) -X(27) -X(28) -X(29) -X(30) -X(31) -X(32) -X(33) -X(34)
-X(35) -X(36) -X(37) -X(38);
y(51) = X(8) -X(39) -X(40) -X(41) -X(42) -X(43) -X(44) -X(45) -X(46) -X(47)
-X(48) -X(49) -X(50) -X(51);
endfunction
>> [X, fval, info] = fsolve
>> (@f,[0.1;0.01;1024;4.;-1.0;0.001;0.0;0.5;154.;71000;0.000007;0.000005;
0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0])
error: 'Y_e' undefined near line 3 column 8
error: called from
    f at line 3 column 6
    fsolve at line 236 column 8
>>

I'm pressed for time and so can't read all of the manual I should.  I
appreciate your help.

Tom


--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html

The variable Y_e is not passed into the function f, so f cannot access it. You need to pass that variable into the function f, or make Y_e a global variable.

Passing Y_e into f would mean you have to give f two arguments: f(X,Y) instead of f(X). I haven't used fsolve myself, so I'm not sure whether it accepts multiple arguments. However, there is a simple workaround that I think should do the trick.

First give f another argument in the place of Y_e
function y = f (X,Y)
y = zeros (12, 1);
y(1) = Y*X(1)*sin(X(2)) +dStat*X(5)/2 + O;
y(2) = X(4) -X(3) + Og_o;
y(3) = X(7);
y(4) = X(1)*Tau^2*(Og_o + X(3))*X(4) +Cf*X(8);
---
---
---
y(50) = X(7) -X(26) -X(27) -X(28) -X(29) -X(30) -X(31) -X(32) -X(33) -X(34)
-X(35) -X(36) -X(37) -X(38);
y(51) = X(8) -X(39) -X(40) -X(41) -X(42) -X(43) -X(44) -X(45) -X(46) -X(47)
-X(48) -X(49) -X(50) -X(51);
endfunction

Then define a supplementary function g with a single argument X which returns f(X,Y_e), and use fsolve on g instead.

Y_e = cosh(Alpha*X_bar) - cos(Alpha*X_bar) + Eta*(sinh(Alpha*X_bar) - sin(Alpha*X_bar));
g = @(X) f(X,Y_e);
[X, fval, info] = fsolve(@g,[0.1;0.01;1024;4.;-1.0;0.001;0.0;0.5;154.;71000;0.000007;0.000005;
0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0])


Reply | Threaded
Open this post in threaded view
|

Re: Must all coefficients for fsolve be numeric???????

José Abílio Matos
On Wednesday, 18 March 2020 17.24.53 WET Brett Green wrote:
> Y_e = cosh(Alpha*X_bar) - cos(Alpha*X_bar) + Eta*(sinh(Alpha*X_bar) -
> sin(Alpha*X_bar)); g = @(X) f(X,Y_e);
> [X, fval, info] =
> fsolve(@g,[0.1;0.01;1024;4.;-1.0;0.001;0.0;0.5;154.;71000;0.000007;0.000005
> ;
> 0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0
> ;0])

Just as Brett said. But you can skip the second step.

[X, fval, info] = fsolve(@(X) f(X,Y_e),
[0.1;0.01;1024;4.;-1.0;0.001;0.0;0.5;154.;71000;0.000007;0.000005;
0;
0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0])

If you have more parameters you must pass them all as arguments of the f
function and than only let X be the only variable.

IMHO A simple variant of this is to create a structure as pass all the
parameters as members of the struct. Something like

par.Y_e
par.dStat
...

You need to define those member outside and then it should be enough to call
like above:
[X, fval, info] = fsolve(@(X) f(X,par),


The other option is to make the parameters that you use as global variables
but then do not forget to declare those variables as global in the function f:
https://octave.org/doc/v5.2.0/Global-Variables.html#Global-Variables

I hope that this helps,
--
José Matos




Reply | Threaded
Open this post in threaded view
|

Re: Must all coefficients for fsolve be numeric???????

ttonon
B.Green and Jose, you both pointed me in the right direction.  I'm familiar
with Common statements and variables used in other programming such as
Fortran and C++, and it makes sense.  I'll read up on Global parameters, and
although there are many parameters in the equations I'll need to pass on,
it's doable.

I'm slowly understanding how Octave can be useful for my problems, and
indeed, even how this forum works.  Thanks for all the help.

Tom



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html