Solving symbolically for variables that are equal to several equations

2 ビュー (過去 30 日間)
Andrew Matteson
Andrew Matteson 2023 年 9 月 13 日
コメント済み: Walter Roberson 2023 年 9 月 14 日
Hello,
I'm trying to solve for any vaiable in a series of equations that are all equal to one another such as in the equation bellow.
In it, I know that "v" has two possible solutions, and I'd like to find a way to have MATLAB recognize this and give me both possible solutions as an array. So far, I have gotten my test program to give me one solution at a time, but not both.
clear all
close all
clc
syms xi v r a mu
spec_eng1 = [xi == (v^2)/2-mu/r, xi == -mu/(2*a)]
solve(spec_eng1, v)
% ans =
% Empty sym: 0-by-1
sol = eng.feval_internal('solve', eqns, vars, solveOptions);
spec_eng2 = xi == (v^2)/2-mu/r == -mu/(2*a)
solve(spec_eng2, v)
% Error using mupadengine/feval_internal
% Invalid argument.
%
% Error in sym/solve (line 293)
% sol = eng.feval_internal('solve', eqns, vars, solveOptions);
% These outputs the solutions, but MATLAB doesn't know their related to
% one another
spec_eng3 = xi == (v^2)/2-mu/r
solve(spec_eng3, v)
% ans =
%
% (2^(1/2)*(mu + r*xi)^(1/2))/r^(1/2)
% -(2^(1/2)*(mu + r*xi)^(1/2))/r^(1/2)
spec_eng4 = -mu/(2*a) == (v^2)/2-mu/r
solve(spec_eng4, v)
% ans =
%
% (mu^(1/2)*(2*a - r)^(1/2))/(a^(1/2)*r^(1/2))
% -(mu^(1/2)*(2*a - r)^(1/2))/(a^(1/2)*r^(1/2))
I don't really know what else to say. Any help with this would be appreicated. Thanks

回答 (2 件)

Torsten
Torsten 2023 年 9 月 13 日
移動済み: Torsten 2023 年 9 月 13 日
syms xi v r a mu
spec_eng1 = [xi == (v^2)/2-mu/r];
solve(spec_eng1, v)
ans = 
spec_eng2 = (v^2)/2-mu/r == -mu/(2*a);
solve(spec_eng2, v)
ans = 

Paul
Paul 2023 年 9 月 13 日
Hmm. Don't know why solve doesn't find a solution,
syms xi v r a mu
spec_eng1 = [xi == (v^2)/2-mu/r, xi == -mu/(2*a)]
spec_eng1 = 
solve(spec_eng1,v,'ReturnConditions',true)
ans = struct with fields:
v: [0×1 sym] parameters: [1×0 sym] conditions: [0×1 sym]
Here's a workaround that for this simple case returns both solutions in an array.
vsol = solve(subs(spec_eng1(1),xi,solve(spec_eng1(2),xi)),v)
vsol = 
Or reverse the order
vsol = solve(subs(spec_eng1(2),xi,solve(spec_eng1(1),xi)),v)
vsol = 
  3 件のコメント
Paul
Paul 2023 年 9 月 14 日
So if we know we want xi to be eliminated from the expression for v, we'd force that by adding to the solved-for variables, I suppose.
syms xi v r a mu
spec_eng1 = [xi == (v^2)/2-mu/r, xi == -mu/(2*a)]
spec_eng1 = 
sol = solve(spec_eng1,[v xi])
sol = struct with fields:
v: [2×1 sym] xi: [2×1 sym]
sol.v
ans = 
sol.xi
ans = 
Walter Roberson
Walter Roberson 2023 年 9 月 14 日
Yes, exactly,
In some cases you can do, for example,
syms xi v r a mu
spec_eng1 = [xi == (v^2)/2-mu/r, xi == -mu/(2*a)]
spec_eng1 = 
arrayfun(@(X) isolate(X, mu), spec_eng1)
ans = 
but not in the case of v because the second expression does not contain v

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

カテゴリ

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

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by