Question

programming in python.

Write a python program that plots a given function and an interpolation polynomial. To test this, use the following cases: a)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import numpy as np
import matplotlib.pyplot as plt

nodes = 12;
#nodes = 5;

xmin = -1;xmax = 1;

x = np.linspace(xmin,xmax,nodes)
f = np.exp(-4*x*x)


# computing newton interpolating polynomial upto order 3


#coefficients upto 3rd order
a0 = f[0];
a1 = (f[1] - f[0])/(x[1] - x[0]);
f12 = (f[2] - f[1])/(x[2]-x[1]);
a2 = (f12 - a1)/(x[2]-x[0]);
f23 = (f[3] - f[2])/(x[3]-x[2]);
f123 = (f23-f12)/(x[3]-x[1]);
a3 = (f123 - a2)/(x[3]-x[0]);

#1st,2nd,and 3rd order polynomials pn(x)
p0 = a0;
p1 = lambda xx: p0 + a1*(xx-x[0]);
p2 = lambda xx: p1(xx) + a2*(xx-x[0])*(xx-x[1]);
p3 = lambda xx: p2(xx) + a3*(xx-x[0])*(xx-x[1])*(xx-x[2]);
p3x = p3(x)


plt.figure(1)
plt.plot(x,f,'o',x,p3x,':')
plt.gca().legend(('f(x)','p3(x)'))
plt.ylabel('f,p')
plt.xlabel('x')
plt.grid(True)
plt.title('plot of f(x) and p3(x)')
plt.show()

plot of f(x) and p3(x) 1.0 f(x) p3(x) 0.5 0.0 -0.5 -2.0 -2.5 -3.0 1.00 -0.75 -0.50 -0.25 0.00 0.25 0.50 0.75 1.00

plot of f(x) and p3(x) 101 f(x) p3(x) 8 4 2 0 1.00 -0.75 -0.50 -0.25 0.00 0.25 0.50 0.75 1.00

COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,

IF YOU'RE SATISFIED, GIVE A THUMBS UP

Add a comment
Know the answer?
Add Answer to:
Write a python program that plots a given function and an interpolation polynomial. To test this,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • programming in python. Write a python program that plots a given function and an interpolation polynomial....

    programming in python. Write a python program that plots a given function and an interpolation polynomial. To test this, use the following cases: a) )xp(-4r2), tE-1,1]. As data, use equidistant nodes that include the endpoints and the function values in those nodes. The number of nodes should be 5, then 12.

  • Consider polynomial interpolation of the function f(x)=1/(1+25x^2) on the interval [-1,1] by (1) ...

    Consider polynomial interpolation of the function f(x)=1/(1+25x^2) on the interval [-1,1] by (1) an interpolating polynomial determined by m equidistant interpolation points, (2) an interpolating polynomial determined by interpolation at the m zeros of the Chebyshev polynomial T_m(x), and (3) by interpolating by cubic splines instead of by a polynomial. Estimate the approximation error by evaluation max_i |f(z_i)-p(z_i)| for many points z_i on [-1,1]. For instance, you could use 10m points z_i. The cubic spline interpolant can be determined in...

  • Interpolation Polynomial Coding MATlab

    I have to write a Matlab function called "interpoly(x,y) which calculates the corresponding interpolation polynomial for given support points (xj,fj) with x=[x1,x2,...,xn] and f=[f1,f2,...,fn] and in interval x[min xj, max xj] plotted. Any method can be used to calculate the interpolation polynomial but I think Lagrange would be the best. One rule is that I can not use "polyfit" command.I have to test the programm with values of fj of the functions f(x)=cos(x) and f(x)=1/(1+x^2) each in the interval [-6,6]....

  • Python Program 5. Write a Python program in a file named validTime.py. Include a function named...

    Python Program 5. Write a Python program in a file named validTime.py. Include a function named string parameter of the form hh:mm: ss in which these are numeric validTime that takes a values and returns True or False indicating whether or not the time is valid. The number of hours, minutes, and seconds must two digits and use a between 0 and 9). The number of hours must be between 0 and 23, the number of minutes and seconds must...

  • matlab matlab For this problem you will test a few interpolation approaches for the application of generating interpolated data. We'll do this by interpolating data that is sampled from a k...

    matlab matlab For this problem you will test a few interpolation approaches for the application of generating interpolated data. We'll do this by interpolating data that is sampled from a known mathematical function. The same function can then be used to compute true values at the interpolated points to use in error Consider the following mathematical function (Runge's function): 1+25r2 Write a function mfile that uses this formula to generate a set of data use those points along approaches outlined...

  • write a python program Write a function is_factor(f, n) that passes these tests: test(is_factor(3, 12)) test(not...

    write a python program Write a function is_factor(f, n) that passes these tests: test(is_factor(3, 12)) test(not is_factor(5, 12)) test(is_factor(7, 14)) test(not is_factor(7, 15)) test(is_factor(1, 15)) test(is_factor(15, 15)) test(not is_factor(25, 15)) An important role of unit tests is that they can also act as unambiguous “specifications” of what is expected. These test cases answer the question Do we treat 1 and 15 as factors of 15?

  • Lab 1 Q1. Write a Python program with the following function building block. •All input values...

    Lab 1 Q1. Write a Python program with the following function building block. •All input values are to be taken fro m the user. •Ensure to check for possible error cases like mismatch of data type and input out of range, for every function.       Function 1: countVowels(sentence) – Function that returns the count of vowels in a sentence. Function 2: Sum of all even numbers between two numbers, identify number of parameters   Q2. Write a Python program that reads a...

  • write a C program with a function that returns area of a given circular

    write a C program with a function that returns area of a given circular enclosure if its radius is contained within theboundary [8, 12].▪ The function should return the phrases: “ Radius is below 8” and “ Radius is above 12” respectively for inputs of radiithat fall outside the boundary .▪ Use for your function name: areaBound()▪ test your program with the values of radii: 5.6, 11, 15.04, 8, 8.7▪ use the scanf() function to enter the test values directly...

  • write a python program that reads from a file, scores.txt, a list of test scores. the...

    write a python program that reads from a file, scores.txt, a list of test scores. the program should calculate the average of all the scores, and print to the screen. the program should also have a function, num_passing, that takes as a parameter the list or grades and returns the number of scores that are above 64.

  • Linear Interpolation Write a program that will create a linear interpolation between two pixels. ...

    How to write this code in python? Linear Interpolation Write a program that will create a linear interpolation between two pixels. You are to read in two pixel values and generate a number of pixels specified between them. Input for the program 1 Enter the first pixel value: 0x002222 2 Enter the second pixel values: OxFFDD3:3 3 Enter the number of pixels to generate inbetween: 3 Using the 2 pixels specified it will generate 3 new pixels between the first...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT