MATLAB not displaying answer correctly in command window
조회 수: 1 (최근 30일)
이전 댓글 표시
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);
댓글 수: 0
채택된 답변
per isakson
2021년 3월 30일
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
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!