Issue finding numerical error (problem with MATLAB's precision?)

9 ビュー (過去 30 日間)
John Alperto
John Alperto 2015 年 3 月 16 日
コメント済み: Andrew Newell 2015 年 3 月 17 日
I'm trying to recreate some code that a TA showed me the other day. Suppose we have the system Ax=b. Let's say A = [2,1,1,0;4,3,3,1;8,7,9,5;6,7,9,8]. And let's say b = [1;1;-1;-3]. I saw a trick in MATLAB that can solve this equation using the "\" symbol. So A \ b should yield the following:
ans =
1.0000
0.0000
-1.0000
-0.0000
If you subtract this from the true solution:
(A \ b) - [1;0;-1;0]
ans =
1.0e-15 *
-0.2220
0.2538
0
-0.0912
Then you can actually see the error in MATLAB's solution versus the actual solution. My issue is that I cannot recreate this. When I do A \ b I get this:
ans =
1
0
-1
0
>> (A \ b) - [1;0;-1;0]
ans =
0
0
0
0
Therefore I cannot see the error, or this answer is just very imprecise-I'm not sure. How can I recreate the correct output? Is there another way I can see numerical error from an approximated solution versus the actual solution in MATLAB?

回答 (2 件)

Andrew Newell
Andrew Newell 2015 年 3 月 16 日
編集済み: Andrew Newell 2015 年 3 月 16 日
That pesky MATLAB - getting more accurate all the time! Here is an example that I got from the blog article Floating Point Comparisons in Matlab:
x = 0.8-0.7;
x-0.1
On my computer, that returns 8.3267e-17 (which is still pretty good!).
  2 件のコメント
John Alperto
John Alperto 2015 年 3 月 17 日
I didnt really see anything in the article that could help me here. I'm still unable to detect the numerical error in the approximated solution versus the true solution.
Andrew Newell
Andrew Newell 2015 年 3 月 17 日
編集済み: Andrew Newell 2015 年 3 月 17 日
Did you try this example from the same article?
K = 7;
s = zeros(K,1);
for k = 1:K
s(k) = sum((10^-k)*ones(10^k,1));
end
s
e = abs(s - 1)

サインインしてコメントする。


Jan
Jan 2015 年 3 月 17 日
編集済み: Jan 2015 年 3 月 17 日
Which format is active in the command window? What do you get with this:
format long g
(A \ b) - [1;0;-1;0]
Are A and B arrays of type double?
  3 件のコメント
John Alperto
John Alperto 2015 年 3 月 17 日
Sorry I forgot to add a new line there before pasting the output.
Andrew Newell
Andrew Newell 2015 年 3 月 17 日
I fixed that for you. You should be able to click on "Edit" and fix it yourself, though.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by