vpasolve with an array
Show older comments
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?
Accepted Answer
More Answers (2)
Walter Roberson
on 7 Mar 2020
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
on 7 Mar 2020
9 Comments
Walter Roberson
on 7 Mar 2020
Remember to change the * to .* and the / to ./
Sun Kyoo Choi
on 7 Mar 2020
Walter Roberson
on 7 Mar 2020
My suspicion is that it would be more efficient to arrayfun a vpasolve or fzero or fsolve call.
Sun Kyoo Choi
on 7 Mar 2020
Edited: Sun Kyoo Choi
on 7 Mar 2020
Walter Roberson
on 7 Mar 2020
P3G = 2; %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);
Sun Kyoo Choi
on 7 Mar 2020
Walter Roberson
on 7 Mar 2020
What is your goal here? Is it to find P3 to more than 16 decimal places accuracy such that vpasolve() is needed for extended precision? Is it to find P3 to double precision but you must have the last couple of bits as accurate as possible so that is why you are using vpasolve to get the extended digits that you can then truncate with double()? Is it to write the simplest program possible and you find vpasolve to be easier to read than the numeric alternatives? Is it to solve to reasonable double precision accuracy in which case double() is fine without using vpasolve? Is performance an issue?
My arrayfun solution was created around the most common need: reasonable double precision accuracy and reasonable performance combined. It uses the numeric solver fzero to find P3. fzero happens to need an initial guess.
If you feel strongly about the matter I could recode it in terms of the higher precision but slower vpasolve, which does not require an initial guess (but usually is faster if you supply a guess.) If you do not pass an initial guess to vpasolve it generates its own initial guess, probably 0 or 1.
Sun Kyoo Choi
on 7 Mar 2020
Walter Roberson
on 7 Mar 2020
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.
Categories
Find more on Number Theory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
