vpasolve with an array

조회 수: 8 (최근 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 CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by