Numerical analysis. Can't use syms, if not the aproximation is different.

1 view (last 30 days)
So, for my numerical analysis class, I decided to use matlab, and I coded the lagrange interpolation, errors and stuff using syms. However, for larger numbers like n=64, my answers are different than my teacher's, not because they are wrong, but "because they are not numerical 100%, using syms probably adds a different error that you are not taking into account". I'm relatively new to matlab and numerical analysis, so I am not sure how to fix that error. Is there a way to use matlab in a 100% numerical form? I will post here all my code, in case anyone wants to see it. Thank you in advance! :)

Accepted Answer

John D'Errico
John D'Errico on 12 Mar 2021
This is numerical analysis. Hey, you are supposed to know this! Well, maybe the goal of the problem is to teach you these facts.
High order polynomials are basically useless, at least in terms of computational ability in floating point arithmetic. You cannot compute with them in double precision. Think about it. What do you need to do? Raise numbers to huge powers, like 64. Consider what happens if x is 1/2? What is x^64 in that case?
x = [1/2 1 2];
format long g
x.^64
ans = 1×3
5.42101086242752e-20 1 1.84467440737096e+19
Can you effectively add numbers that vary over so many orders of magnitude? No. Consider this "identity", i.e., statement of exact equality, at least when performed in floating point arithmetic:
1 + 1.e-20 == 1
ans = logical
1
And even if you compute a polymomial in a Lagrange form, you are still effectively doing those same operations. It is the same polynomial, just cloaked in different robes.
You see the same problem if you try to evaluate a Taylor series with many dozens of terms in it. Those high order terms become insignificant in terms of double precision arithmetic.
If you insist on using such polynomials, then you will need to perform the computation in either symbolic floating point terms (thus using VPA) or using a tool like my own HPF toolbox. You can download HPF from the file exchange. In either case, BE VERY CAREFUL. Unless you know what you are doing, it is quite easy to mistakenly mix in some double precision computations with either tool, and once you do, you corrupt the ability of those tools to work in high precision.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!