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);
a=1.000000 b=2.666667 c=2.666667 d=1.000000 e=2.000000 f=2.666667 g=1.333333
fprintf('\n a=%f b=%f c=%f d=%f e=%f f=%f g=%f',ae,be,ce,de,i,fe,ge);
a=0.000000 b=-0.333333 c=-0.333333 d=0.000000 e=2.000000 f=-0.333333 g=0.333333

2 comentarios

Matt J
Matt J el 19 de Feb. de 2023
Editada: Matt J el 19 de Feb. de 2023
It seems to run to completion without error messages, judging from the output in your post.
Xavier Fleury
Xavier Fleury el 19 de Feb. de 2023
maybe it's not my loop then, because I want all my values ​​of a, b,... to be integers

Iniciar sesión para comentar.

 Respuesta aceptada

Matt J
Matt J el 19 de Feb. de 2023
Editada: Matt J el 19 de Feb. de 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,
e = 3
fprintf('a=%f b=%f c=%f d=%f e=%f f=%f g=%f',a1,b1,c1,d1,i,f1,g1);
a=3.000000 b=8.000000 c=8.000000 d=3.000000 e=3.000000 f=8.000000 g=4.000000
fprintf('\n a=%f b=%f c=%f d=%f e=%f f=%f g=%f',ae,be,ce,de,i,fe,ge);
a=0.000000 b=0.000000 c=0.000000 d=0.000000 e=3.000000 f=-0.000000 g=0.000000

5 comentarios

Xavier Fleury
Xavier Fleury el 19 de Feb. de 2023
that's exaclty what i want thanks a lot but can you explain me what the ligne if does i don't understand
Matt J
Matt J el 19 de Feb. de 2023
Editada: Matt J el 19 de Feb. de 2023
that's exaclty what i want thanks a lot
You're welcome, but please Accept-click the answer to indicate that.
but can you explain me what the ligne if does i don't understand
Yes, if you tell me which line(s) you don't understand, I will explain them.
Xavier Fleury
Xavier Fleury el 19 de Feb. de 2023
the line 8 the "if"
Matt J
Matt J el 19 de Feb. de 2023
Editada: Matt J el 19 de Feb. de 2023
That line tests if all D(i) are nearly integers (within 1e-8). The all(tf) command will test if all elements of tf are true.
Note that you cannot expect D to be exactly integer-valued, because it is the result of floating point computations.
Xavier Fleury
Xavier Fleury el 19 de Feb. de 2023
Nice thanks a lot for your help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 19 de Feb. de 2023

Editada:

el 19 de Feb. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by