Loop for issue in me script
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi, i need help with my loop for. I can't make it work if some one can help me
clear;
clc;
A=[1 0 0 -1 0 0; 1 0 0 0 0 0; 0 1 0 0 -1 0; 0 3 0 0 -1 -1; 0 0 1 0 0 -2; 0 -1 1 -2 0 0];
n=1;
for i=1
B=[0; i; 0; 4*i; 0; -2*i];
D=A\B;
a1=D(1,:);
b1=D(2,:);
c1=D(3,:);
d1=D(4,:);
f1=D(5,:);
g1=D(6,:);
ae=a1-round(a1);
be=b1-round(b1);
ce=c1-round(c1);
de=d1-round(d1);
fe=f1-round(f1);
ge=g1-round(g1);
if ae==0 && be==0 && ce==0 && de==0 && fe==0 && ge==0
e=i;
break;
else
i=i+1;
end
end
fprintf('a=%f b=%f c=%f d=%f e=%f f=%f g=%f',a1,b1,c1,d1,i,f1,g1);
fprintf('\n a=%f b=%f c=%f d=%f e=%f f=%f g=%f',ae,be,ce,de,i,fe,ge);
2 Commenti
Risposta accettata
Matt J
il 19 Feb 2023
Modificato: Matt J
il 19 Feb 2023
clear;
clc;
A=[1 0 0 -1 0 0; 1 0 0 0 0 0; 0 1 0 0 -1 0; 0 3 0 0 -1 -1; 0 0 1 0 0 -2; 0 -1 1 -2 0 0];
i=1;
while 1
B=[0; i; 0; 4*i; 0; -2*i];
D=A\B;
if all( abs(D-round(D)) < 1e-8)
e=i;
break;
else
i=i+1;
end
end
a1=D(1,:);
b1=D(2,:);
c1=D(3,:);
d1=D(4,:);
f1=D(5,:);
g1=D(6,:);
ae=a1-round(a1);
be=b1-round(b1);
ce=c1-round(c1);
de=d1-round(d1);
fe=f1-round(f1);
ge=g1-round(g1);
e,
fprintf('a=%f b=%f c=%f d=%f e=%f f=%f g=%f',a1,b1,c1,d1,i,f1,g1);
fprintf('\n a=%f b=%f c=%f d=%f e=%f f=%f g=%f',ae,be,ce,de,i,fe,ge);
5 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!