How to show exact answers in MATLAB

Hello everyone, I run into this exact answer problem with MATLAB pretty frequently. My problem is right now when I'm trying to do an inverse of a matrix containing square roots, it gives me answers in fractions or in decimals. I want it to show the square roots and other functions. How would I go about doing this? I've googled a while and all they say are precision values and rounding and what not, but nothing to do with these square roots.

댓글 수: 2

As Walter answered, use
>> inv([sqrt(sym(21))])
ans =
21^(1/2)/21
Mr.Gh
Mr.Gh 2019년 12월 14일
편집: Mr.Gh 2019년 12월 14일
Use this code to see the exact answer:
>>A = inv([sqrt(sym(21))]);
>>answer = double(A)
answer = 2.1822e-01

댓글을 달려면 로그인하십시오.

답변 (3개)

Mahdi
Mahdi 2013년 9월 24일

1 개 추천

Why don't you try an approach where you use square the matrix to find out what the numbers are?
For example
MatrixSquared=Matrix.^2;
Then you would be able to see which numbers were square rooted. I would also suggest placing
format rat
before the code.

댓글 수: 1

Hey, I tried your approach, but it only gives me fractions. I want MATLAB to show me square roots and powers. I tried the following:
format rat;
inv([sqrt(21)]);
it shows me fractions, but I want to see square roots

댓글을 달려면 로그인하십시오.

Matt J
Matt J 2013년 9월 24일
편집: Matt J 2013년 9월 24일

1 개 추천

Sounds like you're trying to do a symbolic matrix inverse operation. The Symbolic Math Toolbox will let you do that, see

댓글 수: 6

Hi
I just tried the inv you've talked about, however, when I try the following, I still do not get square roots. I want the answer to be in square roots:
inv([sqrt(21)])
Walter Roberson
Walter Roberson 2013년 9월 25일
inv([sqrt(sym(21))])
I want to get answers in fractional form for operations on fractions like 1/2 + 15/17, 2/5*3/16 etc. Which route is better - sym or format rat? Why?
Walter Roberson
Walter Roberson 2018년 2월 9일
sym as early as possible, before you even do the division. For example sym(15)/sym(17). Otherwise you are losing precision in the fractions. format rat does not return the true fraction corresponding to the internal representation of the floating point numbers: format rat uses a continued fraction approach to try to find the closest fraction within a tolerance. So where true fraction might be 38183/86917, format rat might decide that 38/87 is "close enough" to display
Great clarity, @ Walter Roberson! Thanks.
Karan Gill
Karan Gill 2018년 2월 13일
To add onto Walter's reply, this topic explains differences in arithmetic.

댓글을 달려면 로그인하십시오.

Shashank Prasanna
Shashank Prasanna 2013년 9월 24일

0 개 추천

Use format to reset your display format or choose a different display format:
>> format

댓글 수: 1

Suh-Shin
Suh-Shin 2013년 9월 24일
format doesn't show the answer in square roots.

댓글을 달려면 로그인하십시오.

카테고리

질문:

2013년 9월 24일

편집:

2019년 12월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by