How to format the Numbers in Output
2 views (last 30 days)
Show older comments
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 Comment
Accepted Answer
Walter Roberson
on 8 Apr 2012
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.
More Answers (0)
See Also
Categories
Find more on Numbers and Precision in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!