Effacer les filtres
Effacer les filtres

Using Vector instead of LOOP to solve an equation

3 vues (au cours des 30 derniers jours)
Math Tec
Math Tec le 13 Juin 2017
Modifié(e) : Adam le 13 Juin 2017
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 commentaires
Math Tec
Math Tec le 13 Juin 2017
Thank you. I tried. I updated the code with my tries but it gave me different answer.
Adam
Adam le 13 Juin 2017
Modifié(e) : Adam le 13 Juin 2017
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.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements 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!

Translated by