This should be replaced with a else followed by a continue.ģ) When you generate your numer and denom variables you are unintentionally just squaring the new value and storing it to the variable. In lagrange interpolation you should have a summation of n terms, where n is the number of data points you have.Īnd the terms are comprised of a product times the y component of the data point, i.e., yj*∏, where if i=j the term is skipped (this is done to make sure the denominator never is zero)Ģ) Your inner for loop and if statement don't play well together.Īs soon as your script reaches the end of the if statement the for loop is broken and it continues to the next k value.
Some of the small issues you are running into are: P(2)=.įor the most part, what you have developed has lead you on the right path to solving your problem. I've searched everywhere but most code seems to only want to output the value of the Lagrange polynomial for certain numbers i.e.
Matlab symbolic toolbox lagrange polynomial how to#
I think I'm on the right track, but I'm really starting to become stuck with how to extract the data and plot everything nicely. Udata = u(x) %create a list of corresponding values of u(x) L = zeros( 1,int_pts(k) ) %create an array to store Lagrange polynomials
U = 1./(1+x.^2) %the function we are interested inĭenom = 1 %set initial values of numerator and denominator This is the code I have come up with so far: int_pts = %various values for no. Your results should show both the function and the interpolant. data points between, and including, -5 and 5). Write a program to evaluate and plot the Lagrange interpolant Iu(x) of u(x) = 1/(1+x^2) for x between -5 and 5.