How to format the Numbers in Output

4 ビュー (過去 30 日間)
Jacob
Jacob 2012 年 4 月 8 日
コメント済み: AKSHAY SAXENA 2018 年 1 月 27 日
Hi Everyone. I have written a short code to perform Langrange Interpolation. The code works fine in giving the numerical results; however, I am wondering if one may help on how to format how the Langrange function appears on the results. I wish it doesnt display the number in such long format as shown below in the code:
clear all
clc
format short e
syms xx x
fx = sin(exp(x)-2);
x = linspace(0,2,7);
y = subs(fx,x);
n =6;
sum = 0;
for i = 1:n+1
product = y(i);
for j = 1:n+1
if i~=j
product = product*(xx-x(j))/(x(i)-x(j));
end
end
sum = sum+product;
end
langrange = sum
f = subs(langrange,x);
E = y-f;
Results = [x' y' f' E']
Here is what I get as results. Notice that the numerical part is fine:
langrange =
(133340683792790313*xx*(xx - 1)*(xx - 2)*(xx - 1/3)*(xx - 2/3)*(xx - 4/3))/144115188075855872 + (2134614566129670693*xx*(xx - 1)*(xx - 2)*(xx - 1/3)*(xx - 2/3)*(xx - 5/3))/144115188075855872 - (914760085722758757*xx*(xx - 1)*(xx - 2)*(xx - 1/3)*(xx - 4/3)*(xx - 5/3))/1152921504606846976 + (1243774911652968393*xx*(xx - 1)*(xx - 2)*(xx - 2/3)*(xx - 4/3)*(xx - 5/3))/360287970189639680 - (56882977377095097*xx*(xx - 1)*(xx - 1/3)*(xx - 2/3)*(xx - 4/3)*(xx - 5/3))/72057594037927936 - (480132804261592377*xx*(xx - 2)*(xx - 1/3)*(xx - 2/3)*(xx - 4/3)*(xx - 5/3))/36028797018963968 - (27*((11368945240871781*xx)/4503599627370496 - 3789648413623927/4503599627370496)*(xx - 1)*(xx - 2)*(xx - 2/3)*(xx - 4/3)*(xx - 5/3))/80
Results =
0 -8.4147e-001 -8.4147e-001 0
3.3333e-001 -5.6826e-001 -5.6826e-001 1.1102e-016
6.6667e-001 -5.2242e-002 -5.2242e-002 1.3878e-017
1.0000e+000 6.5809e-001 6.5809e-001 0
1.3333e+000 9.7527e-001 9.7527e-001 -2.2204e-016
1.6667e+000 -1.5230e-001 -1.5230e-001 5.5511e-017
2.0000e+000 -7.7966e-001 -7.7966e-001 0
Any help would be appreciated. Thank you for your time.
  1 件のコメント
AKSHAY SAXENA
AKSHAY SAXENA 2018 年 1 月 27 日
What is the meaning on graph 3.9e-001,2.5e-001....

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

採用された回答

Walter Roberson
Walter Roberson 2012 年 4 月 8 日
Perhaps
vpa(langrange)
By the way, please do not name your variable "sum": that conflicts with the name of the routine "sum", and conflicts like that often lead to trouble that is difficult to diagnose.
  2 件のコメント
Jacob
Jacob 2012 年 4 月 8 日
I had the method working but is there a way of reducing the number of decimal points? I tried to do "format shortE" at the beginning to display every number with only 4 decimal numbers; however, it works ony for the numerical results, for the Langrange functions it displays up to 30 decimal points.
Walter Roberson
Walter Roberson 2012 年 4 月 8 日
vpa(langrange,5)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by