Using fsolve to solve a system of multi-variable equations

I am trying to solve a system of 4 equations which are summations of Eigenseries. There are 4 variables. The functions/governing equations are actually determine using several inputs which are pre-determined constants using a function. It seems that when I'm trying to use the function handle (@ funk) I am not allowed to put any inputs other than 'x', a vector that contains 4 the variables, into funk. How am I supposed to go around this? I need the equations to be inputs into the function or to somehow use fsolve on 4 equations direction without using a function.
Thank you for your time, Alex.

Réponses (2)

Torsten
Torsten le 16 Fév 2017
https://de.mathworks.com/help/optim/ug/passing-extra-parameters.html
Best wishes
Torsten.

5 commentaires

Wow you are very fast, thanks a lot. I just tried to use global variables however I got the error
Error using fsolve (line 269)
FSOLVE requires all values returned by functions to be of data type double.
and I can't convert the globals to doubles in the function. Any idea around this if this is the technique you were referring to ? If it is not I will try the other techniques, that is nested functions and anonymous functions, now. Thanks a lot Torsten
Could you please show us the relevant part of your code ?
Best wishes
Torsten.
I want to solve these 4 equations for the 4 variables which as syms: a_i, b_i, c_i and d_i. The functions just calculate a coefficient for the respective variables. I have tried vpasolve and solve and it doesn't work ): I am trying to work out how to do it with nested functions but am a little stuck.
EQ_1 = dphi1(a_inc, a_i, Cn, kf, nf, -B/2, B, w, t, n) - dphi2(b_i, c_i, Cn, kr, nr, -B/2, B, w, t, n);
EQ_2 = phi1(a_inc, a_i, Cn, kf, nf, -B/2, B, w, t, n) - phi2(b_i, c_i, Cn, kr, nr, -B/2, B, w, t, n);
EQ_3 = dphi2(b_i, c_i, Cn, kr, nr, B/2, B, w, t, n) - dphi3(d_i, Cn, kf, nf, B/2, B, w, t, n);
EQ_4 = phi2(b_i, c_i, Cn, kr, nr, B/2, B, w, t, n) - phi3(d_i, Cn, kf, nf, B/2, B, w, t, n);
Thanks for your time
I think I may have fixed it but I have a different error now. I used the code
x = [a_i, b_i, c_i, d_i];
fun = @(x) funk(a_inc, x, Cn, kf, kr, nf, nr, B, w, n, t);
x0 = double([a_inc*0.3, a_inc*0.7, a_inc*0.1, a_inc*0.6]);
x = fsolve(fun, x0);
Where the function funk is
function fun = funk(a_inc, x, Cn, kf, kr, nf, nr, B, w, n, t)
EQ_1 = dphi1(a_inc, x(1), Cn, kf, nf, -B/2, B, w, t, n) - dphi2(x(2), x(3), Cn, kr, nr, -B/2, B, w, t, n);
EQ_2 = phi1(a_inc, x(1), Cn, kf, nf, -B/2, B, w, t, n) - phi2(x(2), x(3), Cn, kr, nr, -B/2, B, w, t, n);
EQ_3 = dphi2(x(2), x(3), Cn, kr, nr, B/2, B, w, t, n) - dphi3(x(4), Cn, kf, nf, B/2, B, w, t, n);
EQ_4 = phi2(x(2), x(3), Cn, kr, nr, B/2, B, w, t, n) - phi3(x(4), Cn, kf, nf, B/2, B, w, t, n);
fun = [EQ_1, EQ_2, EQ_3, EQ_4];
end
However now I get the error: "Error using fsolve (line 269) FSOLVE requires all values returned by functions to be of data type double." Once again I tried to convert each of the inputs excluding 'x' into double but the function double didn't let me. Any ideas ? Thanks so much
fsolve doesn't work with symbolic variables - EQ_1, EQ_2, EQ_3 and EQ_4 must be of type double.
Best wishes
Torsten.

Connectez-vous pour commenter.

try to define the 4 variables as vectors (x(1),x(2),...) instead of syms: a_i, b_i, c_i and d_i. See if it works

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by