Perform for loop and carry out calculation separately
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dear community,
I´m once again asking for your help!
As visible in the code below, I´m trying to perform a for loop in steps of 1:1:823´543 (which corresponds to the length of vector a_1_7a. What I ultimately want to achieve, is that these c- and D-factors are calculated for every single value of the given range (which means that the related value is to be extracted from a previously defined vector, in this case depth_7a_p). After calculating 823´543 values for c and D a related number (823543) of matrices V_7a_p and vectors S_7a_p shall be created. Finally, the same amount of gamma factor vector is to be calculated and extracted from the for loop.
For some reason I don´t understand, this doesn´t work. Hopefully you´re able to help me.
Greetings
David
for n=1:1:length(a_1_7a)
c_1_2=width*G./depth_7a_p(n,1).*10^3;
c_2_3=width*G./depth_7a_p(n,2).*10^3;
c_3_4=width*G./depth_7a_p(n,3).*10^3;
c_4_5=width*G./depth_7a_p(n,4).*10^3;
D_1=pi^2*E_0*width.*depth_7a_p(n,1)./(span).*10^-3;
D_2=pi^2*E_0*width.*depth_7a_p(n,2)./(span).*10^-3;
D_3=pi^2*E_0*width.*depth_7a_p(n,3)./(span).*10^-3;
D_4=pi^2*E_0*width.*depth_7a_p(n,4)./(span).*10^-3;
D_5=pi^2*E_0*width.*depth_7a_p(n,5)./(span).*10^-3;
a_3_7a(n)=0;
V_7a_p=[(c_1_2+D_1)*a_1_7a(n) -c_1_2*a_2_7a(n) 0 0 0; -c_1_2*a_1_7a(n) (c_1_2+c_2_3+D_2)*a_2_7a(n) -c_2_3*a_3_7a(n) 0 0; 0 -c_2_3*a_2_7a(n) (c_2_3+c_3_4+D_3)*a_3_7a(n) -c_3_4*a_4_7a(n) 0; 0 0 -c_3_4*a_3_7a(n) (c_3_4+c_4_5+D_4)*a_4_7a(n) -c_4_5*a_5_7a(n); 0 0 0 -c_4_5*a_4_7a(n) (c_4_5+D_5)*a_5_7a(n)];
S_7a_p=[-c_1_2*(a_2_7a(n)-a_1_7a(n));-c_2_3*(a_3_7a(n)-a_2_7a(n))+c_1_2*(a_2_7a(n)-a_1_7a(n));-c_3_4*(a_4_7a(n)-a_3_7a(n))+c_2_3*(a_3_7a(n)-a_2_7a(n));-c_4_5*(a_5_7a(n)-a_4_7a(n))+c_3_4*(a_4_7a(n)-a_3_7a(n));c_4_5*(a_5_7a(n)-a_4_7a(n))];
gamma_7a_1=(V_7a_p).^-1.*S_7a_p;
end
2 commentaires
Rik
le 16 Nov 2020
You are overwriting most variables in your loop, instead of indexing them.
The variable names look like you are using many numbered variables, which is generally a bad idea. You might be tempted to try to generate the variable names at runtime, instead of using indexing. You are also hiding a lot of information because you don't use descriptive variable names or comments.
Why don't you first try to get your code working and optimize it before you throw almost a million iterations at it. Can you try to find a way to vectorize your code?
Réponses (1)
Shadaab Siddiqie
le 19 Nov 2020
From my understanding you want to accelerate your code. Please vectorize your code. This reference might help you.
2 commentaires
Shadaab Siddiqie
le 20 Nov 2020
Modifié(e) : Shadaab Siddiqie
le 20 Nov 2020
You can still vectorize it. One example would be:
c_1_2=width*G./depth_7a_p_1.*10^3;
c_2_3=width*G./depth_7a_p_2.*10^3;
c_3_4=width*G./depth_7a_p_3.*10^3;
c_4_5=width*G./depth_7a_p_4.*10^3;
%insted of above code you can convert depth_7a_p_1,depth_7a_p_2,..,depth_7a_p_4 into a matrix
C = width*G./D.*10^3;
% Here D = [depth_7a_p_1 ; depth_7a_p_2 ;depth_7a_p_3 ;depth_7a_p_4]
Dont create depth_7a_p_1,depth_7a_p_2,..,depth_7a_p_4 at all create D directly from depth_7a_p.
Voir également
Catégories
En savoir plus sur Number Theory dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!