MATLAB not displaying answer correctly in command window

2 views (last 30 days)
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);

Accepted Answer

per isakson
per isakson on 30 Mar 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

More Answers (0)

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!