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

조회 수: 4 (최근 30일)
guy tau
guy tau 2023년 2월 26일
댓글: Walter Roberson 2023년 3월 1일
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