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

3 次查看(过去 30 天)
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.

回答(1 个)

Walter Roberson
Walter Roberson 2022-10-17
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.)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by