equation solver solve() returns empty result

5 ビュー (過去 30 日間)
kairui wang
kairui wang 2019 年 2 月 2 日
コメント済み: Walter Roberson 2019 年 2 月 3 日
I tried using symbolic equation solver to get the results
syms V a b K
eq1 = pi^2*(a+b).*(b-a).^2 == V
eq2 = V == 8000
eq3 = a == K*b
eq4 = K == 0.2:0.1:0.7
a = solve([eq1,eq2,eq3,eq4],a)
And I got
a =
Empty sym: 0-by-1
It should have solutions while it returned nothing. Did I have something wrong?
  3 件のコメント
kairui wang
kairui wang 2019 年 2 月 2 日
Honestly I'm try learning solve(). I want to figure out what's wrong with the codes.
Walter Roberson
Walter Roberson 2019 年 2 月 2 日
編集済み: Walter Roberson 2019 年 2 月 2 日
eq4 = K == 0.2:0.1:0.7
defines a system of 6 equations,
[ K == 1/5, K == 3/10, K == 2/5, K == 1/2, K == 3/5, K == 7/10]
all of which have to be true simultaneously in order for solve() to find a solution.
You need to use symbolic K and substitute in the values later -- or you need to loop with specific numeric K values.
Also, you must solve() for as many variables as your provide equations. With four equations, you need to solve for four variables simultaneously. If you look at the solution I provided, I provide two equations and I solve for two variables.

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

採用された回答

Walter Roberson
Walter Roberson 2019 年 2 月 2 日
V = 8000;
Kvals = 0.2:0.1:0.7;
syms a b K
eq1 = pi^2*(a+b).*(b-a).^2 == V
eq3 = a == K*b;
sol = solve([eq1, eq3], [a, b]);
a = subs(sol.a, K, Kvals)
You will find that the result is 3 (different solutions) per K value. Two of the solutions are complex valued and the third is real valued.
  2 件のコメント
kairui wang
kairui wang 2019 年 2 月 2 日
It seems there're complex solutions, is there a way to get sole real solutions?
Walter Roberson
Walter Roberson 2019 年 2 月 3 日
At the end add
a(all(imag(a)~=0,2), :) = [];
This will delete all rows in which all of the values are complex-valued. If there should happen to be any row in which some of the values are only real and some are complex then this code will not delete such a row. The code could be changed to create a cell array in which each entry had 1 to 3 real-valued solutions (since the number of real-valued could potentially change according to k value).

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Solvers についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by