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 Comment

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

Sign in to comment.

 Accepted Answer

Matt J
Matt J on 26 Feb 2019
Edited: Matt J on 26 Feb 2019
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!