0
>> K=[6,31,221;31,221,1801;221,1801,15665]

K =

       6          31         221
      31         221        1801
     221        1801       15665

>> f=[31;197;1543]

f =

      31
     197
    1543

>> lambda=inv(K)*f

lambda =

2.1413
0.5472
0.0054

I even use K\f option. But it gives the same result. But this is not correct right? the correct answer should be lambda = 2.1728 0.6070 -0.0102

  • 5
    How did you determine the correct answer? – Benoit_11 Jan 14 '15 at 14:59
  • 1
    And did you check that you don't have any typos in your `K` and `f`? – horchler Jan 14 '15 at 15:00
  • 3
    `K*lambda-f` gives gives a result of size 10^-11. I am quite interested to learn how you came up with the other answer since that seems to be incorrect. – patrik Jan 14 '15 at 15:08
  • yes.but i dont knw if i am wrong. when i take the inv of K and multiply it with f it gives me the answer i have put. Can you please check it by directly multiplying the inversed matrix of K with f? –  Jan 14 '15 at 15:44
  • Lesson learned - Make sure you include as many digits of precision as possible when dealing with floating point operations. – rayryeng Jan 21 '15 at 22:25

3 Answers3

3

So let's check if it's MATLAB that doesn't know how to do matrix operations. If lambda = inv(K)*f, then K*lambda = f. Is this correct?

lambda = K\f
lambda =
       2.1413
      0.54725
     0.005374

K*lambda
ans =
           31
          197
         1543

Let's see what we get if we assume that lambda = [2.1728; 0.6070; -0.0102] is correct:

f = K*lambda
f =
         29.6
       183.13
       1413.6

I would put my money on MATLAB being correct.

Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
  • yes.but i dont knw if i am wrong. when i take the inv of K and multiply it with f it gives me the answer i have put. Can you please check it by directly multiplying the inversed matrix of K with f? –  Jan 14 '15 at 15:43
2

I think I got this!

To get the other (wrong) answer, I think you did:

>> K=[6,31,221;31,221,1801;221,1801,15665];

>> inv(K)

ans =

1.5647   -0.6276    0.0501
-0.6276    0.3235   -0.0283
0.0501   -0.0283    0.0026

>> iK=[  1.5647   -0.6276    0.0501;
-0.6276    0.3235   -0.0283;
0.0501   -0.0283    0.0026];

>> f=[31;197;1543];

>> iK*f

ans =

2.1728
0.6070
-0.0102

This does not work because when you manually copied the value of inv(K), you did not take the exact value computed by matlab (which is not exact anyway but close enough) instead you took the values displayed, so with only 4 digits accuracy!

yoh.lej
  • 1,104
  • 6
  • 14
  • thank u. I thought that may be the reason though but the last value of the ans comes from my way is negative but matlab gives a positive value. Can it has a big difference like that? –  Jan 14 '15 at 15:56
  • if you have an error `e` on `x`, you'll have an error `2e` on `2x` no matter the value of `x` and for instance an error `e` on `x+y` no matter the value of `x` and `y`, but now try to see what happens for `1/x`... if you have `1/(x-e)` instead of `1/x`, it can make a huge difference .. depending on the value of `x`. this is not a real proof, just a hint to show you a small error can translate into a big error, mainly because inverting a matrix is not a linear operation (whereas addition and multiplication are linear operations) – yoh.lej Jan 14 '15 at 18:11
1

computation on wolfram alpha

wolfram alpha agrees with MATLAB,...2 against 1. Do you actually have a reason for believing that the result should be different?

computation on wolfram alpha

yoh.lej
  • 1,104
  • 6
  • 14
  • Interesting. I would put a screenshot or something more visual to support your answer; since now it looks a bit dry and would not be valid if the link is broken somehow. Thanks! – Benoit_11 Jan 14 '15 at 15:18
  • Also: You might want to use the hyperlink functionality when using long links. It improves the readability =) – Stewie Griffin Jan 14 '15 at 15:20
  • yes.but i dont knw if i am wrong. when i take the inv of K and multiply it with f it gives me the answer i have put. Can you please check it by directly multiplying the inversed matrix of K with f? –  Jan 14 '15 at 15:39
  • did u find what's the problem? may there is something wrong with my logic. but can't figure it out by myself –  Jan 14 '15 at 15:47
  • http://www.wolframalpha.com/input/?i={{1.5647%2C-0.6276%2C0.0501}%2C+{-0.6276%2C+0.3235%2C+-0.0283}%2C+{0.0501%2C+-0.0283%2C+0.0026}}+.{{31}%2C{197}%2C{1543}}} @ yoh.lej check this out –  Jan 14 '15 at 15:48
  • I found your mistake, check my other answer – yoh.lej Jan 14 '15 at 15:53