why do I get a negative numbers???
古いコメントを表示
im in the process of programming for a project and i keep getting funtionally impossible answers for the variables everytime i run the code it says it stops prematurely so is that the main problem? it is currently only going through "2.300000e+03" itterations is there a way to increase that?
4 件のコメント
Jacob Mathew
2024 年 11 月 29 日
Hi Jonah,
Could you share related code to help debug ?
Walter Roberson
2024 年 11 月 29 日
Are you using:
- fminunc
- fmincon
- fsolve
- fzero
- ga
- vpasolve
- something else?
If you are using fmincon() are you using any constraints?
If you are using fzero() are you using a single scalar as the starting point, or are you using a vector of two values?
Jonah
2024 年 11 月 29 日
Jonah
2024 年 11 月 29 日
回答 (1 件)
Walter Roberson
2024 年 11 月 29 日
opts = optimoptions('fsolve', 'MaxFunctionEvaluations', 1e6, 'MaxIterations', 1e6);
Guess = linspace(0,.001, 23);
Answers = fsolve(function_name, Guess, opts)
Notice that the guess is set to something non-zero: it is often a bad choice to start the function off at the all-zero vector.
4 件のコメント
Jonah
2024 年 11 月 29 日
Jonah
2024 年 11 月 29 日
Walter Roberson
2024 年 11 月 29 日
There is no way to force fsolve to give only positive answers.
Generally speaking, the way around that is to instead use fmincon:
fun2 = @(X) function_name(X).^2;
A = []; b = [];
Aeq = []; beq = [];
lb = zeros(1,length(Guess));
ub = inf(1,length(Guess));
nonlcon = [];
opts = optimoptions('fmincon', 'MaxFunctionEvaluations', 1e6, 'MaxIterations', 1e6);
Answers = fmincon(fun2, Guess, A, b, Aeq, beq, nonlcon, opts);
The trick here is that solving for zero is closely approximated by minimizing the square of the function.
Torsten
2024 年 11 月 29 日
There is no way to force fsolve to give only positive answers.
Generally speaking, the way around that is to instead use fmincon
... or to use the variables squared instead of the variables themselves in the equations you are trying to solve.
カテゴリ
ヘルプ センター および File Exchange で Solver Outputs and Iterative Display についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!