MATLAB not displaying answer correctly in command window

1 visualización (últimos 30 días)
Hi there.
So I am currently working on a project that calculates a mixed triple product with all six permutations. I currently have the answers displaying in the command window. However, when I look at the answers, I do not get a numerical answer on three out of six, even though they are the same code formulas. Also, is there any way to limit the answer to be two digits, instead of 7 and an exponent? I have attached my code and a photo of my answer output.
P = [0, 3, 0];
Q = [0, 0, 5];
S = [2, 0, 0];
%%Cross Products
QS = cross(Q, S);
SP = cross(S, P);
PQ = cross(P, Q);
SQ = cross(S, Q);
PS = cross(P, S);
QP = cross(Q, P);
%%Dot Products
PQS = dot(P, QS);
QSP = dot(Q, SP);
SPQ = dot(S, PQ);
PSQ = dot(P, SQ);
QPS = dot(Q, PS);
SQP = dot(S, QP);
%%Display
fprintf ("The triple product of P*(QxS) is %s\n", PQS);
fprintf ("The triple product of Q*(SxP) is %s\n", QSP);
fprintf ("The triple product of S*(PxQ) is %s\n", SPQ);
fprintf ("The triple product of P*(SxQ) is %s\n", PSQ);
fprintf ("The triple product of Q*(PxS) is %s\n", QPS);
fprintf ("The triple product of S*(QxP) is %s\n", SQP);

Respuesta aceptada

per isakson
per isakson el 30 de Mzo. de 2021
Look up the help on format specifier of fprintf().
Next replace the spec, %s, in your %%Display section by %+7.2f
The triple product of P*(QxS) is +30.00
The triple product of Q*(SxP) is +30.00
The triple product of S*(PxQ) is +30.00
The triple product of P*(SxQ) is -30.00
The triple product of Q*(PxS) is -30.00
The triple product of S*(QxP) is -30.00

Más respuestas (0)

Categorías

Más información sobre App Building en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by