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:
programming in python. Write a python program that plots a given function and an interpolation polynomial....
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
  • Write a python program that plots a given function and an interpolation polynomial. To test this,...

    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. 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...

  • 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 Programming 4th Edition: Write a program that asks the user for the number of feet....

    Python Programming 4th Edition: Write a program that asks the user for the number of feet. The program converts those feet to inches by using the formula ’12 * feet’. Hint: Define main () program Get the number of feet from the user Define variables Call a function feet_to_inches(feet) The function receives the number of feet and returns the number of inches in that many feet. The output should look like as follows: Enter the number of feet: If the...

  • Python Simple Programming Write a program in Python that calculates the tip and total for a...

    Python Simple Programming Write a program in Python that calculates the tip and total for a meal at a restaurant. When your program is run it should ... Calculate the tip and total for a meal for a restaurant Print the name of the application "Tip Calculator" Get input from the user for cost of meal and tip percent Print the tip and total amounts. The formula for calculating the tip amount is: tip = cost of meal * (tip...

  • Use Python programming #3 on page 47 of your python programming book: Write a Tipper program...

    Use Python programming #3 on page 47 of your python programming book: Write a Tipper program where the user enters a restaurant bill total. The program should then display two amounts: a 15% tip and a 20% tip. Name your file tipper.py Previous Next.

  • Please write comments with the program. Python programming!! Part III - write student data In this...

    Please write comments with the program. Python programming!! Part III - write student data In this part the program should create a .csv file and fill it with generated student information. The program should: Ask the user for a name for the .csv file (use your own name for the exercise) Create the .csv file with columns named - ID, first-name, GPA Based on the information received in part I, generate and fill students information in the CSV file Tell...

  • 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...

  • C Programming QUESTION 9 Write a program that prompts for and reads four integer input values,...

    C Programming QUESTION 9 Write a program that prompts for and reads four integer input values, then a single character command. Submit your program as a .c file named midterm_prog2.c. Note that, similarly to your programming assignments, some percentage of your grade will depend on proper programming style. Your program will calculate and print a new value based on the command that's entered, as follows: 'A' or 'a': Calculate the average of the four input values 'S' or 's': Calculate...

  • Data Programming l: Python Write a program that calculates how much Mary should pay for buying...

    Data Programming l: Python Write a program that calculates how much Mary should pay for buying 3 KitKat each worth $2 and 2 Twix chocolate each worth $4. Output: Mary should pay $14. Write a program that takes the circle radius and compute the circle area. Use the round function to have at most 2 decimal places in the output. Output: The area of a circle with 4 inches radius is 50.27

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