Vectorization of for loops with symbolic functions inside

2 vues (au cours des 30 derniers jours)
Hi,
I have been using for-loops extensively and they served the prupose well until I had to run a code that requires over 140 thousand iteration. At that point my computer started taking an eternity to process it. So, I tried searching for ways to optimize the code and something I got introduced to is vectorization of the for-loops.
I have tried looking at ways to vectorize the two for loops but I can't seem to figure out a way that would suit my case. In my code, inside each iteration I am using symbolic variables and solving them for their values. Have a look at the simplified version of my code below:
syms t
vec1 = [5*cosd(t); 5*sind(t)];
vec2 = [t^2+2; t^3+5];
start_time = 0;
end_time = 360;
for time1 = start_time:1:end_time-1
for time2 = time1+1:1:end_time
A = double(subs(vec1, t, time1));
B = double(subs(vec2, t, time2));
syms x y %two unknowns
eq1 = transpose(A)*[x;y]
eq2 = transpose(B)*[x;y]
eq3 = [eq1; eq2];
solutions = solve(eq3,[x,y]);
%Output the solutions
x.solution
y.solution
end
end
Is there a way to vectorize the two time loops?
Thanks in advance for your help.

Réponse acceptée

Walter Roberson
Walter Roberson le 23 Mai 2022
Yes, you can vectorize the code completely by just assigning 0 and 0 as the solutions.
You construct eq3 as the sum of two scalar terms each of which has x times something plus y times something. collect x and y to get something times x plus something times y. You solve the scalar sum for x and y (one equation with two unknown). But that has the trivial solution x=0 y=0 no matter what the details of the terms are.
  17 commentaires
Walter Roberson
Walter Roberson le 24 Mai 2022
subs(solutions.x,{t1,t2},{T1 T2} )
Mohammad Zainullah Khan
Mohammad Zainullah Khan le 25 Mai 2022
That's prefect! Thanks.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by