How to format the Numbers in Output

4 vues (au cours des 30 derniers jours)
Jacob
Jacob le 8 Avr 2012
Commenté : AKSHAY SAXENA le 27 Jan 2018
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 commentaire
AKSHAY SAXENA
AKSHAY SAXENA le 27 Jan 2018
What is the meaning on graph 3.9e-001,2.5e-001....

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 8 Avr 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.
  2 commentaires
Jacob
Jacob le 8 Avr 2012
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 le 8 Avr 2012
vpa(langrange,5)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interpolation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by