why do I get a negative numbers???

56 次查看(过去 30 天)
Jonah
Jonah 2024-11-29,4:55
评论: Torsten 2024-11-29,20:41
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 个评论
Jonah
Jonah 2024-11-29,5:22
I'm using fsolve, and I'd prefer not to share the code for now at least because it's an assignment that's still open I'm sorry for the trouble. I've been working on it off and on for the past 3-4 days but nothing I do works. I've only just started learning matlab since august and there's still a lot I don't understand.
Jonah
Jonah 2024-11-29,5:24
As far as what I'm doing to get answers I'm doing
Guess = zeros(1,23) Answers = fsolve(function name, Guess)

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2024-11-29,5:49
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 个评论
Walter Roberson
Walter Roberson 2024-11-29,20:18
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
Torsten 2024-11-29,20:41
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

标签

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by