# Help Using Solve Function

1 view (last 30 days)
katherine keogh on 28 Sep 2021
Answered: Star Strider on 28 Sep 2021
So I have an equation which I have double checked is right, and I am trying use the solve function to find the answer, I can do it using a table to look up the answer but I can't seem to get MATLAB to solve it without using the table.
function Mu_2=MuM(Mach, Gamma,Theta % The first function finds Mu_1 which in turn finds Mu_2. This functions works fine
Term_1=sqrt((Gamma+1)/(Gamma-1))
Inner=Mach^2-1
Term_2=sqrt(((Gamma-1)/(Gamma+1))*Inner)
Term_3=sqrt(Inner)
Mu_1=Term_1*atan(Term_2)-atan(Term_3)
Mu_2=Mu_1+Theta
Then following this function the output is inputted into another function which is very similar
function [M_2]=FindM_2(Mu_2,Gamma)
syms M_2
Term_1=sqrt((Gamma+1)/(Gamma-1))
Inner=M_2^2-1
Term_2=sqrt(((Gamma-1)/(Gamma+1))*Inner)
Term_3=sqrt(Inner)
equ=(Mu_2==Term_1*atan(Term_2)-atan(Term_3)) % Building an equation with the term M_2 symbolically
solve(equ,M_2,'returnconditions',true) %ask MATLAB to find M_2
This is the script that calls both functions
Gamma=1.4
Mu_2=MuM(2.2, Gamma,10)
[a,equ]=FindM_2(Mu_2,Gamma)
This is the error I get
Warning: Unable to find explicit solution. For options, see help.
> In sym/solve (line 317)
In FindM_2 (line 8)
In Untitled2 (line 2)
The answer I am expecting is roughly 2.62

Star Strider on 28 Sep 2021
Try something like this —
Gamma=1.4
Gamma = 1.4000
Mu_2=MuM(2.2, Gamma,10);
M_2 = FindM_2(Mu_2,Gamma)
No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the value of the function tolerance.
M_2 = 1.7898e+04
M_2 = 1.7898e+04
function [M_2]=FindM_2(Mu_2,Gamma)
Term_1=sqrt((Gamma+1)/(Gamma-1));
Inner = @(M_2) M_2^2-1;
Term_2 = @(M_2) sqrt(((Gamma-1)/(Gamma+1))*Inner(M_2));
Term_3 = @(M_2) sqrt(Inner(M_2));
equ = @(M_2) Mu_2 - Term_1*atan(Term_2(M_2))-atan(Term_3(M_2));
M_2 = fsolve(equ, 1)
end
function Mu_2=MuM(Mach, Gamma,Theta) % The first function finds Mu_1 which in turn finds Mu_2. This functions works fine
Term_1=sqrt((Gamma+1)/(Gamma-1));
Inner=Mach^2-1;
Term_2=sqrt(((Gamma-1)/(Gamma+1))*Inner);
Term_3=sqrt(Inner);
Mu_1=Term_1*atan(Term_2)-atan(Term_3);
Mu_2=Mu_1+Theta;
end
This runs without error (after a fair bit of effort to get it this far), however the solution is several orders-of-magnitude differnt from the expected result. I defer to you to solve that.
The Symbolic Math Toolbox is likely inappropriate here, principallty because it is not intended for iterative applications, and so will be extremely slow. Note that instead of using it, this version of the code uses multiple anonymous functions. If you are not familiar with them, see Anonymous Functions for an extended discussion.
.