Please help me to rewrite the following codes

5 views (last 30 days)
%% Calculation of phi and f
a11 = ai(1);
a12 = ((ai(1)*ai(2))^0.5)*(1-bin_PR(1,2));
a13 = ((ai(1)*ai(3))^0.5).*(1-bin_PR(1,3));
a21 = a12;
a22 = ai(2);
a23 = ((ai(2)*ai(3))^0.5)*(1-bin_PR(2,3));
a31 = a13;
a32 = a23;
a33 = ai(3);
sum_xa(1) = x(1)*a11 + x(2)*a12 +x(3)*a13;
sum_xa(2) = x(1)*a21 + x(2)*a22 +x(3)*a23;
sum_xa(3) = x(1)*a31 + x(2)*a32 +x(3)*a33;
sum_ya(1) = y(1)*a11 + y(2)*a12 +y(3)*a13;
sum_ya(2) = y(1)*a21 + y(2)*a22 +y(3)*a23;
sum_ya(3) = y(1)*a31 + y(2)*a32 +y(3)*a33;
phi_L=exp((bi./b_L).*(ZLt-1)-log(ZLt-B_L)-(A_L./(B_L.*(-2*sqrt(2)))).*(log((ZLt+(1-sqrt(2)).*B_L)/(ZLt+(1+sqrt(2)).*B_L)))*((2*sum_xa/a_L)-(bi./b_L)));
phi_G=exp((bi./b_G).*(ZGt-1)-log(ZGt-B_G)-(A_G./(B_G.*(-2*sqrt(2)))).*(log((ZGt+(1-sqrt(2)).*B_G)/(ZGt+(1+sqrt(2)).*B_G)))*((2*sum_ya/a_G)-(bi./b_G)));
------------------------------------
-----------------------------------------
I want to write the above coding like this (below one):
for i=1:3
for j=1:3
el_L(i,j)=x(i).*x(j)*((ai(i).*ai(j)).^0.5).*(1-bin_PR(i,j));
el_G(i,j)=y(i).*y(j)*((ai(i).*ai(j)).^0.5).*(1-bin_PR(i,j));
end
end
a_L=sum(el_L(:));
b_L=sum(x.*bi);
  7 Comments
madhan ravi
madhan ravi on 19 Nov 2018
no you won't if you chane three to twenty you will get the results if and only if the other variables have the compatable sizes
Pulok Deb
Pulok Deb on 19 Nov 2018
so there is no way to write a simple loop or else for calculation of phi and f. right? but my instructor tell me its possible. I am confused now

Sign in to comment.

Accepted Answer

KSSV
KSSV on 19 Nov 2018
YOu need not to use a loop for your code. YOu can achieve it by simple matrix multiplication. Check the below code.
a11 = ai(1);
a12 = ((ai(1)*ai(2))^0.5)*(1-bin_PR(1,2));
a13 = ((ai(1)*ai(3))^0.5).*(1-bin_PR(1,3));
a21 = a12;
a22 = ai(2);
a23 = ((ai(2)*ai(3))^0.5)*(1-bin_PR(2,3));
a31 = a13;
a32 = a23;
a33 = ai(3);
A = [a11 a12 a13 ; a21 a22 a23;a31 a32 a33] ;
b1 = x ; % this should be a 3X1 array
b2 = y ; % this should be a 3X1 array
sum_xa = A*b1 ;
sum_ya = A*b2 ;
phi_L=exp((bi./b_L).*(ZLt-1)-log(ZLt-B_L)-(A_L./(B_L.*(-2*sqrt(2)))).*(log((ZLt+(1-sqrt(2)).*B_L)/(ZLt+(1+sqrt(2)).*B_L)))*((2*sum_xa/a_L)-(bi./b_L)));
phi_G=exp((bi./b_G).*(ZGt-1)-log(ZGt-B_G)-(A_G./(B_G.*(-2*sqrt(2)))).*(log((ZGt+(1-sqrt(2)).*B_G)/(ZGt+(1+sqrt(2)).*B_G)))*((2*sum_ya/a_G)-(bi./b_G)));
  2 Comments
Pulok Deb
Pulok Deb on 19 Nov 2018
I am looking for For loop &/or if-else loop. Thanks though
KSSV
KSSV on 19 Nov 2018
a11 = ai(1);
a12 = ((ai(1)*ai(2))^0.5)*(1-bin_PR(1,2));
a13 = ((ai(1)*ai(3))^0.5).*(1-bin_PR(1,3));
a21 = a12;
a22 = ai(2);
a23 = ((ai(2)*ai(3))^0.5)*(1-bin_PR(2,3));
a31 = a13;
a32 = a23;
a33 = ai(3);
A = [a11 a12 a13 ; a21 a22 a23;a31 a32 a33] ;
sum_xa = zeros(3,1) ;
sum_ya = zeros(3,1) ;
for i = 1:3
sum_xa(i) = A(i,1)*x(1)+A(i,2)*x(2)+A(i,3)*x(3) ;
sum_ya(i) = A(i,1)*y(1)+A(i,2)*y(2)+A(i,3)*y(3) ;
end
phi_L=exp((bi./b_L).*(ZLt-1)-log(ZLt-B_L)-(A_L./(B_L.*(-2*sqrt(2)))).*(log((ZLt+(1-sqrt(2)).*B_L)/(ZLt+(1+sqrt(2)).*B_L)))*((2*sum_xa/a_L)-(bi./b_L)));
phi_G=exp((bi./b_G).*(ZGt-1)-log(ZGt-B_G)-(A_G./(B_G.*(-2*sqrt(2)))).*(log((ZGt+(1-sqrt(2)).*B_G)/(

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!