My code is giving only one solution to a function although there are two solutions, what can I do to get all the solutions?
34 ビュー (過去 30 日間)
古いコメントを表示
Jeyamahendran Nithuran
2022 年 6 月 15 日
コメント済み: Walter Roberson
2022 年 6 月 18 日
f(t) = 1.4*exp(0.5*x)-2*x-3 = 0 is the function I tried to solve. Here the variable t indicates time. From plotting the graph I know that there are two roots, one is positive and the other negative. But my code is giving only the negative root which I actually don't need. What should I do to get both the roots?
My Code:
syms x
y2 = 1.4*exp(0.5*x)-2*x-3;
g = y2 == 0;
roots = vpa(solve(y2,x))
0 件のコメント
採用された回答
David Goodmanson
2022 年 6 月 15 日
編集済み: David Goodmanson
2022 年 6 月 15 日
Hi JN,
syms x; assume(x,'positive')
and proceed as before for the second one.
It is not a great idea to use 'roots' as the name of a variable, since it is the name of a Matlab function.
0 件のコメント
その他の回答 (1 件)
Walter Roberson
2022 年 6 月 16 日
vpasolve(y2,x, [0 inf] )
3 件のコメント
Walter Roberson
2022 年 6 月 18 日
In such a case you would not know that one of the values is positive. "positive" is being expressed here as being required to be in the range 0 to infinity.
Walter Roberson
2022 年 6 月 18 日
If you have a function with two roots and they are known to be at least Delta apart but you do not know the order, then
- vpasolve once, getting first root
- vpasolve a second time with range -inf to first minus Delta/2. If you got an answer it is the second root.
- If you did not get an answer then vpasolve range first plus Delta/2 to infinity. If you got an answer it is the second root
- If you did not get an answer earlier second time then perhaps there are not two roots, or perhaps they are closer together than Delta, or perhaps Delta is insignificant compared to the location of the root, or perhaps the equations are too steep for vpasolve, or perhaps the Newton-Raphson projection for a point inside the barrier was outside the barrier in which case vpasolve gives up.
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!