Speeding up solve() function in a for loop

I am trying to write a bit of code to solve for a system of 4 equations inside a rather large while loop, this leads to the solve() function being performed multiple times and drastically slows down the run time. Is there a way to preallocate the symbolically solved variables (so as to only input the new number) or is there a way to have matlab solve the system with matrices.
The code:
for d_t = 0:100
burn_back = d_t*5e-3;
x_5 = 0.0710;
x_3 = 0.4397;
y_5 = 0.5487;
y_3 = 0.3358;
syms alpha beta x_intct y_intct;
[alpha,beta,x_intct,y_intct] = solve(...
x_intct == x_3 + burn_back*cos(alpha),...
x_intct == x_5 + burn_back*cos(beta),...
y_intct == y_3 + burn_back*sin(alpha),...
y_intct == y_5 + burn_back*sin(beta),...
[alpha beta x_intct y_intct]);
alpha = double(alpha);
beta = double(beta);
x_intct = double(x_intct);
y_intct = double(y_intct);
end
Any help would be greatly apreciated.

1 个评论

inside a rather large while loop,
Your code shows a for-loop, not a while loop.

请先登录,再进行评论。

 采纳的回答

Matt J
Matt J 2019-2-26
编辑:Matt J 2019-2-26
If a solution exists, it has the analytical form,
x_intct=(x_3+x_5)/2;
y_intct=(y_3+y_5)/2;
alpha=atan2((y_intct-y_3)/burn_back, (x_intct-x_3)/burn_back);
beta=alpha+pi;
Instead of looping, you could just vectorize the above.

更多回答(0 个)

类别

Community Treasure Hunt

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

Start Hunting!

Translated by