Solving an eq. using a large table by vpasolve is to slow

4 次查看(过去 30 天)
Hi,
I'm trying to use the solve function of MATLAB using a relatively large table of data (~30000 rows).
My eq. uses some of the variables from the table to predict and compare to a known sampled variable.
Yet, the running time for solving the eq. by using a loop yields such large running time.
I imagine that their some easier and faster way to solve this eq, and I hope I reached the right place.
First, I used the next code:
syms X
for i = 1:height(a)
Equation = ((a.var1(i) -c1*(X)^4)- (c2*a.var2(i)*(6.105.*exp((c3*X)./(x+c4)) -a.var3(i))) - (c5*(x-a.var4(i)))) = 0;
solution(i) = double(vpasolve(eqn(i),Tw));
end
where
c1,c2,c3,c4 and c5 are constants
and a.var1 to a.var 4 are the variables from the table
Later, I saw some further solutions online, that convert the code to:
ThemeCopy
syms a.var1 a.var2 a.var3 a.var4 i X
Equation = ((a.var1(i) -c1*(X)^4)- (c2*a.var2(i)*(6.105.*exp((c3*X)./(x+c4)) -a.var3(i))) - (c5*(x-a.var4(i)))) = 0;
tsym = solve(simplify(Equation), X);
tfun = matlabFunction(tsym, 'vars', {'a.var1', 'a.var2', 'a.var3', 'a.var4','i'});
for i = 1:height(a)
t(i) = tfun(a.var1,a.var2,a.var3,a.var4, i);
end
Unfortunately, I got the following error back
Error using sym/subsindex
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.
Error in indexing (line 1075)
R_tilde = builtin('subsref',L_tilde,Idx);
Related documentation
I assume the problem is related to the index table, but i'm struggling to find the solution
If other solution pop's to your mind that will be great as well.
Tnx in advance.
  2 个评论
Torsten
Torsten 2023-2-26
Please supply the equation(s) you are trying to solve and the variable(s) you are trying to solve for in a mathematical notation.
I cannot recover this information from your code.
guy tau
guy tau 2023-2-26
移动:Walter Roberson 2023-3-1
The eq. takes the following form
Where the a's are values with know data from the table (i.e., along the loop, =##).
and the c's are constant (i.e., remain as is with each loop).
and the X represent the value I wish to solve at each iteration.
Hope it is clearer now

请先登录,再进行评论。

采纳的回答

Torsten
Torsten 2023-2-26
移动:Torsten 2023-2-26
syms A.var1 A.var2 A.var3 A.var4 X
Equation = ((A.var1 -c1*X^4)- (c2*A.var2*(6.105.*exp((c3*X)./(X+c4)) -A.var3)) - (c5*(X-A.var4))) == 0;
for i = 1:height(a)
X_num(i) = double(vpasolve(subs(Equation,[A.var1 A.var2 A.var3 A.var4],[a.var1(i) a.var2(i) a.var3(i) a.var4(i)]),X))
end
  4 个评论
Walter Roberson
Walter Roberson 2023-2-26
On thing that can help is to feed the previous result as the initial guess. vpasolve(expression, guess) in the single variable case. This assumes that the solution for this new case is probably "close" to the previous one. The performance difference can be a lot.
Walter Roberson
Walter Roberson 2023-3-1
syms A.var1 A.var2 A.var3 A.var4 X
You cannot 'syms' a dot-indexed variable. The closest you can get would be
A.var1 = sym('var1'); A.var2 = sym('var2'); A.var3 = sym('var3');
But I think you would be better off using
syms var [1 4]
syms X
X_num(i) = double(vpasolve(subs(Equation, var, [a.var1(i) a.var2(i) a.var3(i) a.var4(i)]),X))

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by