fslove Equation with variable coefficients

Using fsolve, I want to solve a non linear set of equations such as
k*exp(-exp(-(x(1)+x(2)))) - x(2)*(1+x(1)^2)=0
x(1)*cos(x(2)) + x(2)*sin(x(1)) - 0.5=0
Whereas k is a set of varying coefficients. I want to solve the set of equations using fsolve for each value of k. I used a normal loop with k(i) inside the function and received an error message. Is there a way to perform this with fsolve?

1 件のコメント

Torsten
Torsten 2017 年 2 月 9 日
Please include your code and the exact error message you receive.
Best wishes
Torsten.

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

 採用された回答

John D'Errico
John D'Errico 2017 年 2 月 9 日
編集済み: John D'Errico 2017 年 2 月 9 日

1 投票

Of course you got an error. Solve one problem at a time. Put the loop outside of the call to fsolve.
n = numel(k);
X = zeros(n,2);
x0 = ???
for i = 1:n
fun = @(x) [k(i)*exp(-exp(-(x(1)+x(2)))) - x(2)*(1+x(1)^2); ...
x(1)*cos(x(2)) + x(2)*sin(x(1)) - 0.5];
X(i,:) = fsolve(fun,x0);
end
At some point, you might decide to learn how to specify a sparse block diagonal jacobian, in which case you could try to solve multiple problems at a time in one call to fsolve. Walk before you run.

2 件のコメント

Saeid
Saeid 2017 年 2 月 9 日
Hi, I tried it and got this error message:
Error in solve_xyvarcoeff (line 8)
X(i,:) = fsolve(fun,x0);
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot
continue.
Saeid
Saeid 2017 年 2 月 9 日
I guess I found the mistake: x0 should have had the same dimension as that of the X!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

質問済み:

2017 年 2 月 9 日

コメント済み:

2017 年 2 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by