How to skip vpasolve empty sym inside a loop

5 views (last 30 days)
Hello,
I have the below code which numerically solves my equation in hand. The vpasolve breaks down when no solution is found. This is due to both a lower or higher values of B. When I change the range of B to let say 30:1:180, the loop runs without problems. However, I need to know the values of B that leads to no solution in addition to the solution. I want the loop to save this particular value to zero and continue to the end. Below is the code:
clear
clc
syms Re Nu Cd
B = 1:1:200;
h = 0.1;
Cd = (-1.9151e-08*((Re^3)*h)) + (2.4399e-09*(Re^3)) - (5.5447e-05*((Re^2)*(h^2))) + (5.7480e-05*((Re^2)*h)) - (6.1862e-06*(Re^2)) - (0.2113*(Re*(h^3))) + (0.1933*(Re*(h^2))) - (0.0695*Re*h) + (0.0057*Re) + (403.4544*(h^4)) - (258.2577*(h^3)) + (45.9552*(h^2)) + (1.4336*h) + (0.9849);
Nu = (-1.1182e-05*(Re^2)) + (0.0024*Re*h) + (0.0299*Re) - (79.0558*(h^2)) + (55.9753*h) - (5.8551);
sol_Re = zeros();
for i = 1:length(B)
sol_Re(i) = vpasolve(Nu*Cd*Re==(2.*B(i).^2)./pi,[100 1000],'Random',true);
end

Accepted Answer

Walter Roberson
Walter Roberson on 24 Feb 2021
Store the result of vpasolve into a variable. Test to see if it is empty before storing into the matrix.
  3 Comments
Walter Roberson
Walter Roberson on 24 Feb 2021
Edited: Walter Roberson on 24 Feb 2021
Store the result of vpasolve into a variable. Test to see if it is empty before storing into the matrix.
for i = 1:length(B)
thissol = vpasolve(Nu*Cd*Re==(2.*B(i).^2)./pi,[100 1000],'Random',true);
if isempty(thissol)
sol_Re(i) = nan;
else
sol_Re(i) = thissol;
end
end
BNoSol = B(isnan(sol_Re));
Sultan Alshareef
Sultan Alshareef on 24 Feb 2021
Thanks very much Walter. Worked as I wanted.

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!