Hi,
I'm solving a system of two non linear equations involving the same number of variables. I'm using the command fsolve, so before I've defined my functions:
function F = eqns(x)
F = [x(1).*A + (1-x(1)).*x(2).*B + (1-x(1)).*(1-x(2)).*C - b1;
x(1).*D + (1-x(1)).*x(2).*E + (1-x(1)).*(1-x(2)).*F - b2]
and save it as nle.m. A,B,C,D,E,F are constant values and b1 and b2 are my own data stored in two vectors in the workspace.
The command
x0 = [0 0];
[x,toll] = fsolve(@nle,x0)
solve my system only if I write the values of b1 and b2 in the equations defined into the m-file. Since I've thousand of b1 and b2 pairs, I would that fsolve find the zero capturing the current values of b1 and b2 from the workspace. How can I do this?
Thanks for the help

 採用された回答

Titus Edelhofer
Titus Edelhofer 2011 年 12 月 12 日

1 投票

Hi,
add the b1, b2 to the function as variables:
function F = eqns(x, b1, b2)
F = ...
and call pass your b1, b2 to the call for fsolve:
b1 = 42;
b2 = 1;
fun = @(x) nle(x, b1, b2);
[x,toll] = fsolve(fun, x0);
Titus

5 件のコメント

gianluca
gianluca 2011 年 12 月 12 日
Hi Titus,
I added the b1 and b2 to the function as variables and this is a essential step and I thank you.
But when I pass b1 and b2 to the call for fsolve, I think fsolve searches the zero for all the values of b1 and b2 simultaneously and gives me the message:
Warning: Default trust-region dogleg method of FSOLVE cannot
handle non-square systems; switching to Gauss-Newton method.
> In fsolve at 232
Optimizer appears to be converging to a minimum that is not a root:
Sum of squares of the function values is > sqrt(options.TolFun).
The b1 and b2 are vectors of size (2000x1) but I would find the m-solutions for each pair of b1-b2. Do you have some ideas?
Sean de Wolski
Sean de Wolski 2011 年 12 月 12 日
Can you paste the code you used to run this? b1 =42,b2=1; were just for example and should not be copied to your code.
Titus Edelhofer
Titus Edelhofer 2011 年 12 月 12 日
That's exactly right. You will need to loop over b1 and b2, e.g.
b1 = ...; b2 = ...;
result = zeros(size(b1));
for i=1:length(b1)
fun = @(x) nle(x, b1(i), b2(i));
result(i) = fsolve(fun, x0);
end
gianluca
gianluca 2011 年 12 月 12 日
Hi,
this is the nle.m file:
function F = eqns(x,b1,b2)
F = [x(1).*0+(1-x(1)).*x(2).*30+(1-x(1)).*(1-x(2)).*150-b1;
x(1).*189+(1-x(1)).*x(2).*55.5+(1-x(1)).*(1-x(2)).*89.9-b2];
In the command windows:
b1 = CGR1;
b2 = S;
fun = @(x) nle(x, b1, b2);
[x,toll] = fsolve(fun, x0);
where CGR1 and S are the vector of size (28721 x 1).
I attach yuo a little part of the CGR1 and S vectors (e.g. 10 rows):
CGR1 = [37.6 38.6 39.7 40.7 41.8 42.8 43.9 44.9 46.0 47.1]
S = [67.4 67.7 67.9 68.1 68.3 68.5 68.7 69.0 69.2 77.3]
Titus Edelhofer
Titus Edelhofer 2011 年 12 月 14 日
Hi,
you need to put the call to fsolve into the loop as I did above.
Titus

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by