MATLAB not displaying answer correctly in command window

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);

 采纳的回答

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

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品

版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by