Using Vector instead of LOOP to solve an equation
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello,
I am solving an equation using LOOP. Is it possible to eliminate LOOP and use vector instead to solve this equation?
I used below code
clear;
clc;
B=0:1:5;
D=0;
E=3;
N1 = 10*rand(1,5);
N2 = 5*rand(1,5);
for ii=1:size(B,2)-1
A=N1(ii);
C=N2(ii);
C1=(A./(4*E*(A*B).^(3/2)));
C2=(-2*(B.^2+A^2+(D-C).^2));
C20=A*B;
C3=sqrt((C20)./((B+A).^2+(D-C).^2));
C4=(D-C);
C5=ellipticE(((4*A*B)./((B+A).^2+(D-C).^2)));
C6=((B-A).^2+(D-C)^2);
C7=(2./((B-A).^2+(D+C)^2));
C8=(D+C);
C9=(B.^2+A^2+(D+C)^2);
C10=sqrt((A*B)./((B+A).^2+(D+C)^2));
C11=(((4*A*B)./((B+A).^2+(D+C)^2)));
C12=((B-A).^2+(D+C)^2);
C13=( sqrt((A*B)./((B+A).^2+(D-C)^2)));
C14=(D-C);
C15=(((4*A*B)./((B+A).^2+(D-C)^2)));
C16=(((4*A*B)./((B+A).^2+(D+C)^2)));
C17=((C2.*C3.*C4.*C5)./C6);
C18=C8.*C9.*C10;
C19=ellipticE(C11);
BN=C1.*(C17+(C7.*((C18.*C19)+C12.*( C13.*C14.*ellipticK(C15)-C8*C10.* ellipticK(C16)))));
In2(ii,:)=BN;
end
%This is my tries to use vector
A = N1;
C = N2;
C1=(A./(4*E*(A.*B).^(3/2)));
C2=(-2*(B.^2+A.^2+(D-C).^2));
C20=A.*B;
C3=sqrt((C20)./((B+A).^2+(D-C).^2));
C4=(D-C);
C5=ellipticE(((4*A.*B)./((B+A).^2+(D-C).^2)));
C6=((B-A).^2+(D-C).^2);
C7=(2./((B-A).^2+(D+C).^2));
C8=(D+C);
C9=(B.^2+A.^2+(D+C).^2);
C10=sqrt((A.*B)./((B+A).^2+(D+C).^2));
C11=(((4*A.*B)./((B+A).^2+(D+C).^2)));
C12=((B-A).^2+(D+C).^2);
C13=( sqrt((A.*B)./((B+A).^2+(D-C).^2)));
C14=(D-C);
C15=(((4*A.*B)./((B+A).^2+(D-C).^2)));
C16=(((4*A.*B)./((B+A).^2+(D+C).^2)));
C17=((C2.*C3.*C4.*C5)./C6);
C18=C8.*C9.*C10;
C19=ellipticE(C11);
BN1=C1.*(C17+(C7.*((C18.*C19)+C12.*( C13.*C14.*ellipticK(C15)-C8.*C10.* ellipticK(C16)))
댓글 수: 3
Adam
2017년 6월 13일
편집: Adam
2017년 6월 13일
Well, with something that complicated you are going to have to setup some tests around that code and keep checking against them when you make changes.
Regarding different answers, your code uses random numbers so you will need to fix the random seed (or for tests hard code in non-random data) to compare different approaches anyway.
Since just doing what I said above doesn't get ride of any of those loads of intermediate variables it shouldn't be difficult to work out at what stage things go wrong.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!