Running a function in “for” cycle, containing vpasolve()
1 view (last 30 days)
Show older comments
I have some function in my script that solves a system of nonlinear algebraic equations
function Lam = lambdas1(x1, x2, x3, x4, w1, w2, g, tau, eta, zeta, sigma, eps, kap, gam_1, gam_2)
lam = 0.5 * atanh(-2*g / (w1 + w2 - 2*zeta - 2*eta));
mu = cosh(lam); nu = sinh(lam);
Om = -2*zeta - 2*eta*(nu/mu)^2 + 2*g*(nu/mu) + w2 * (nu/mu)^2 + w1;
A1 = mu^2*w1 + nu^2*w2; A2 = nu^2*w1 + mu^2*w2;
B = mu*nu*(w1 + w2); C = g*(mu^2 + nu^2) - 2*mu*nu*(zeta + eta);
F1 = 2*(zeta*mu^2 + eta*nu^2 - g*mu*nu);
F2 = 2*(zeta*nu^2 + eta*mu^2 - g*mu*nu);
Lam = vpasolve(-kap*(x1)^3 - kap*x2*(x1)^2 + 2*mu*tau*Om*(x1)^2 - 0.5*gam_1*x1 + ...
(A1-F1)*x2 + (B-C)*x4 + 0.5*mu*tau*Om == 0, ...
-(kap*(x1)^2*x2 + kap*(x2)^3 + 8*Om*tau^2*(x1)^3 + 4*mu*Om*tau*x1*x2 + ...
(A1+F1+6*tau^2*Om)*x1 + 0.5*gam_1*x2 - (B+C)*x3 - nu*sigma + eps*mu) == 0, ...
(B+C)*x2 - 0.5*gam_2*x3 + (A2+F2)*x4 == 0, ...
-((C-B)*x1 + (A2-F2)*x3 + 0.5*gam_2*x4 + mu*sigma - eps*nu) == 0, ...
[x1, x2, x3, x4]);
end
Where are real symbolic numbers which I define in the script (outside the sunction). I need to run this function in the cycle like this one (I have a vector ov values of zeta and I wish to solve a system for each of them)
syms x1 x2 x3 x4 real
for k = 1 : some_number
lam = 0.5 * atanh(-2*g / (w1 + w2 - 2*zeta(k) - 2*eta));
mu = cosh(lam);
nu = sinh(lam);
Om = -2*zeta(k) - 2*eta*(nu/mu)^2 + 2*g*(nu/mu) + w2 * (nu/mu)^2 + w1;
A1 = mu^2*w1 + nu^2*w2; A2 = nu^2*w1 + mu^2*w2;
B = mu*nu*(w1 + w2); C = g*(mu^2 + nu^2) - 2*mu*nu*(zeta(k) + eta);
F1 = 2*(zeta(k)*mu^2 + eta*nu^2 - g*mu*nu);
F2 = 2*(zeta(k)*nu^2 + eta*mu^2 - g*mu*nu);
My_roots = vpasolve(-kap*(x1)^3 - kap*x2*(x1)^2 + 2*mu*tau*Om*(x1)^2 - 0.5*gam_1*x1 + ...
(A1-F1)*x2 + (B-C)*x4 + 0.5*mu*tau*Om == 0, ...
-(kap*(x1)^2*x2 + kap*(x2)^3 + 8*Om*tau^2*(x1)^3 + 4*mu*Om*tau*x1*x2 + ...
(A1+F1+6*tau^2*Om)*x1 + 0.5*gam_1*x2 - (B+C)*x3 - nu*sigma + eps*mu) == 0, ...
(B+C)*x2 - 0.5*gam_2*x3 + (A2+F2)*x4 == 0, ...
-((C-B)*x1 + (A2-F2)*x3 + 0.5*gam_2*x4 + mu*sigma - eps*nu) == 0, ...
[x1, x2, x3, x4]);
Solution_I_need(k) = My_roots.x1(1) + 1i*My_roots.x2(1);
end
But even for two iterations, it takes an extra-long time. Could you tell me please, how to deal with it properly?
9 Comments
Walter Roberson
on 2 Dec 2021
I am not clear at the moment why there is a problem. If you try 0.4 then it is very slow immediately. But when I use symbolic calculations, the forms I get with general zeta values can be solved fairly quickly numerically for any given zeta value, as it turns out to be just a degree 9 polynomial.
Speaking of degree 9 polynomials: it might be better if you supplied initial values for the vpasolve()
Answers (0)
See Also
Categories
Find more on Calculus 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!