vpasolve with an array

4 ビュー (過去 30 日間)
Sun Kyoo Choi
Sun Kyoo Choi 2020 年 3 月 7 日
コメント済み: Sun Kyoo Choi 2020 年 3 月 9 日
Hi all,
I was wondering if I could solve for p3 with a given array.
For example,
x1 = 1:0.01:14;
% or x1 = linspace(1,14,0.01)
syms p3
S = vpasolve(p3*(1 -((gamma-1)*(p3-1))./(2*gamma*(2*gamma+(gamma+1)*(p3-1))).^.5).^(-2.*gamma./(gamma-1))-1 == x1, p3)
% and with given p3 array, I should get Ms = sqrt((gamma+1)/(2.*gamma))*(p3-1)+1, then I need to plot(x1,Ms) so that
% I could get a continuous curve.
but, it gives me this error (More equations than variables is only supported for polynomial systems.)
What can I possibly fx for this?

採用された回答

Sun Kyoo Choi
Sun Kyoo Choi 2020 年 3 月 8 日
Thank you.
I have successfully solved Ms, but the thing is I have to obtain Mr as well.
I have used the exact same formula that you have provided to me,
x1 = linspace(1,14,1000);
P3G = 3; %initial guess
gamma = 1.4;
S = arrayfun(@(X) fzero(@(p3) p3*(1 -((gamma-1)*(p3-1))./sqrt(2*gamma*(2*gamma+(gamma+1)*(p3-1)))).^(-2.*gamma./(gamma-1)) - X, P3G), x1);
Ms = (((gamma+1)/(2.*gamma))*(S-1)+1).^0.5;
% So far, it is good. I gain S and Ms Matrix.
% But, I need to plug in Ms array to gain Mr.
S1 = arrayfun(@(X) fzero(@(Mr) ((Ms.*(Mr.^2-1))/(Ms.^2-1))*((1+((2.*(gamma-1)*(Ms.^2-1)))./(gamma+1)^2).*(gamma+1./Ms.^2)).^.5 - Mr - X, P3G), Ms);
...
it gives me "Operands to the || and && operators must be convertible to logical scalar values".
%I am trying to find Mr arrays with respect to Ms.
  8 件のコメント
Walter Roberson
Walter Roberson 2020 年 3 月 9 日
One of your right values is nan or infinite . That could happen if Ms is infinite or 1, and possibly under other circumstances.
When you accidentally put all of the right values into a single vector to find the roots of, you were putting that non-finite value into the same vector, and roots() of that extended vector would have been nan or infinite.
arrayfun(@(V) roots([V, -1, V]), right, 'uniform', 0)
will give you a cell array of the roots, one cell for each Ms value.
Or you could use the standard (-b±sqrt(b^2-4ac))/2a formula to calculate the roots in vectorized form. b=-1, a=right, c=-right
Sun Kyoo Choi
Sun Kyoo Choi 2020 年 3 月 9 日
Thank you!
I really appriciate it! :)

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

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2020 年 3 月 7 日
Replace
syms p3
With
p3 = sym('p3_', [1, length(x1)]) ;
This will make p3 into an array of different symbols, p3_1, p3_2 and so on, and will solve for each of those.
This will not necessarily be efficient, but it does satisfy your requirements that vpasolve solve for an array of equation.
Remember that vpasolve attempts to find values for the variables that solve all of the equations simultaneously, so your code was trying to find a single p3 that worked for the entire x1 vector.
My suspicion is that it would be more efficient to arrayfun a vpasolve or fzero or fsolve call.

Sun Kyoo Choi
Sun Kyoo Choi 2020 年 3 月 7 日
Then, what would be a possible way to fix this problem, because when I replace p3 to what you have provided, it gives me this error. How could I use arrayfun for this?
x1 = linspace(1,14,1000);
gamma = 1.4;
p3 = sym('p3_', [1, length(x1)]) ;
S = vpasolve(p3*(1 -((gamma-1)*(p3-1))./(2*gamma*(2*gamma+(gamma+1)*(p3-1))).^.5).^(-2.*gamma./(gamma-1))-1 == x1, p3);
Error using symengine
Dimensions do not match.
  9 件のコメント
Sun Kyoo Choi
Sun Kyoo Choi 2020 年 3 月 7 日
Okay, that was correct. Sorry my mistake.
My final question is that when I plot x1 vs. Ms, where Ms is defined
%Ms = (((gamma+1)/(2.*gamma))*(S-1)+1).^0.5;
%S is p3 an array that I have created.
P3G = 3; %initial guess
S = arrayfun(@(X) fzero(@(p3) p3*(1 -((gamma-1)*(p3-1))./(2*gamma*(2*gamma+(gamma+1)*(p3-1))).^.5).^(-2.*gamma./(gamma-1))-1 - X, P3G), x1);
Ms = (((gamma+1)/(2.*gamma))*(S-1)+1).^0.5;
plot(x1,Ms);
%It give me this shape of graph, which is correct, but is there any way that I can
%start from (1,1)? How could I adjust it? Currently, it starts from (1,1.1595).
%I would like to fix so that it could start from (1,1) instead of (1,1.1595).
Walter Roberson
Walter Roberson 2020 年 3 月 7 日
In order for MS to be 1, S would have to be 1. In order for S to be 1, p3 would have to be 1. You want MS to be 1 when x1 is 1, so you can substitute X = 1 and p3 = 1 into
p3*(1 -((gamma-1)*(p3-1))./(2*gamma*(2*gamma+(gamma+1)*(p3-1))).^.5).^(-2.*gamma./(gamma-1))-1 - X
The answer falls out to -1 no matter what the gamma value is. -1 is never equal to 0, not even for very small values of -1.
Therefore, No, it would be inconsistent for MS to be 1 at x1 = 1, no matter what the gamma value is.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by