I would like to solve more than 100 algebra questions with vpasolve; how do i use loop to achieve it?

4 views (last 30 days)
Hello, currently I tried to use matlab for solving the equation based on van Genuchten model
I would like to find alpha and n value by inputting hundred 141 sets of theta and psi values
What i hope to do is to compare two sets of inputs theta and psi values.The equation I created becomes the following:
table_a = zeros(1,141) %I created this the matrix for storing the first coefficient, a
table_n = zeros(1,141) %I created this the matrix for storing the second coefficient, n
for c = 1:141
theta_r = min(agws05(1:end,c));
theta_s = max(agws05(1:end,c));
tetra1 = agws05(1,c);
tetra2 = agws05(2,c);
psi1=agwo05(1,c);
psi2=agwo05(2,c);
syms a n
F = [theta_r+((theta_s-theta_r)./(1+(a*psi1).^n).^(1-(1/n))) - tetra1 == 0, theta_r+((theta_s-theta_r)./(1+(a*psi2).^n).^(1-(1/n))) - tetra2 == 0];
S = vpasolve(F,a,n);
table_a(1,c) = S.a;
table_n(1,c) = S.n;
end
However, the result shows that the loop stopped at c = 4. The answer of S started becoming void and show message:
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is
0-by-1.
Please help me to make this code work. Thank you.

Answers (1)

Walter Roberson
Walter Roberson on 17 Oct 2022
create a 2 x 1 variable that contains the current initial guess. Initialize it outside of the loop. Inside of the loop, pass the current guess as the next parameter after the variables: the list of variables tells it which value corresponds to which guess.
Inside the loop test isempty(S). If it is not empty then update the current guess to be the result of the vpasolve and record the solution in the arrays. But if isempty then do not update the current guess and save sym(nan) as the outputs.
Sometimes vpasolve fails because the starting guess it used traps it in a false minimum. Using the previous solution as the initial guess can make the algorithm faster and more likely to find a solution.
But sometimes there just are no solutions. Or the equation might turn very steep and the algorithm cannot converge (if so increasing digits() sometimes helps.)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!