Newton Interpolating polynomials
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
hey guys, i really need your help with my code for newton interpolating polynomials. I have it all typed out but its not providing the correct answer. here is the code :
function A = NewInt(x,y,xi) n = length(x) - 1; for i = 1:n+1 fdd(i,1) = y(i); end for j = 2:n+1 for i = 1:(n-j+2) fdd(i,j) = (fdd(i+1,j-1)-fdd(i,j-1))/(x(i+j-1)-x(i)) end end xterm = 1 yint(1) = fdd(1,1)
for order = 2:n+1 xterm = xterm*(xi-x(order-1)) yint2 = yint(order-1)+(fdd(1,order)*xterm) ea(order-1) = yint2-yint(order-1) yint(order) = yint2 end end
and here is my function with its inputs: A = NewInt([267.5 1177.5 4525 222.5 1400 1152.5 1525],[.00385 .000957 7.15e-5 .00341 .000627 .00264 .000803],800)
we are given a trendline and when you plug in 800, the answer should be .00127 but my function is giving me .049. can someone please help me with this
1 Kommentar
Jan
am 15 Mär. 2011
Please use the code formatting to make the question readable. You can find a lot of working Newton interpolations, if you spend the time to ask Google.
Antworten (1)
John D'Errico
am 15 Mär. 2011
Surprise, this may be a computer, but you are learning that computers are not all powerful, or infinitely accurate. You are building a 6th degree polynomial. It does things like raise numbers to the 6th power. What is 800 raised to the 6th power?
>> 800^6
ans =
2.6214e+17
Yes, it is larger than the largest number representable EXACTLY in a double precision number. You are adding and subtracting numbers together that are as large or larger than that, but their difference is expected to be less than 1. Surprise! You got numerical garbage. I imagine your instructor designed this problem to expose exactly that.
Can you resolve this problem? Yes, in several ways.
1. Use lower order interpolation methods, so raise numbers to lower powers.
2. Scale (and sometimes translate) your data better, so raising smaller numbers to moderately high (in context) powers.
3. Use a better scheme for interpolation like a spline, where the writer has taken care to treat numerical problems like this carefully. This is likely not an option for you, but high order polynomial interpolation is a poor choice in general. It is best left to the textbooks (and homework assignments) as an example of how to do things poorly.
4. Use higher precision arithmetic than the double class that MATLAB offers. This is surely not necessary, since the first 3 options I have just offered are available to you, and are far better choices.
4 Kommentare
John D'Errico
am 16 Mär. 2011
Without seeing the coefficients of your polynomial, I cannot know. But I am confident that since 800^5 is roughly 3e14, if the coefficients of the polynomial are large enough, the same behavior is still there. High order polynomials are poor things to use. Read my response, and the suggestions I have made. Polyfit allows you to scale and transform the arguments. Use that ability to improve the conditioning of the problem. Better yet, don't use polyfit at all, and certainly don't fit high order polynomial models.
Siehe auch
Kategorien
Mehr zu Polynomials finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!