Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
28 views
13 pages
NC Assignment 2
Assignment Python based working for solving Numerical Computing problems like newton forward difference and interpolation etc.
Uploaded by
saadahmed.21.22.2003
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save NC ASSIGNMENT 2 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
28 views
13 pages
NC Assignment 2
Assignment Python based working for solving Numerical Computing problems like newton forward difference and interpolation etc.
Uploaded by
saadahmed.21.22.2003
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save NC ASSIGNMENT 2 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save NC ASSIGNMENT 2 For Later
You are on page 1
/ 13
Search
Fullscreen
2610812024, 18:13 1224345 Saad Ahmed - Copy of Session_2 Interpolation ipynb - Colaboratony Practice to make a polynmial in numpy. import numpy as np x=[1,2,3] ‘np. poly1d(x) import numpy as np x=[1,2,3] # here 1,2,3 are coefficients of the polynomial in descending order Polysnp.poly1d(x) print(Poly1) Poly2=np.polyid(x,True) #another format to print polynomial print (Poly2) Code to read data from a CSV file import pandas as pd import numpy as np 4 Read data from CSV file df = pd.read_csv(‘data.csv') Convert data to numpy arrays x = éf["x values’ ].values y = df['y values" ].values Function for getting Lagrange Polynmial htips:ifcola. research google.comidrvel 7GGWSUmFaHeMBhUS9CExGBINSTwAE2ro7authuser=2HscrolTo=Wirgrco2OWS8prinMode=trus 1/132610372028, 18:13 224348 Saad Ahmed -Copy of Session_2 Interpolation ipynb - Colaboratory x = [0, 20,40,60, 80, 100] y = [26.0, -48.6, 61.6, -71.2, 74.8, -75.2] # Function to calculate Lagrange polynomial def lagrange_poly(x, y): n = len(x) P = np.poly1d(@.@) for i in range(n): L = np.polyid(y(i]) for j in range(n): ij lea: L = np.polyid( (1.0, -x(3]]) / (xi) - x{3]) paek return p # Calculate Lagrange polynomial p = lagrange_poly(x, y) print(p) 5 4 3 2 -5.329e-06 x + 0.001313 x - 0.1132 x + 3.985 x - 47.81 x + 26 For Interpolating at a specific point # Interpolate at a specific point point = float(input("Enter x-coordinate to interpolate: ")) interp_value = p(point) # Print Lagrange polynomial and interpolated value print(""Lagrange polynomial is:") print(p) print("Interpolated value at x » point, "isi", interp value) Enter x-coordinate to interpolate: 45 Lagrange polynomial is: 5 4 3 2 -5.329e-06 x + 0.001313 x - 0.1132 x + 3.985 x - 47.81 x + 26 Interpolated value at x = 45.0 is: 31.29079589843832 v TASK #01 Solve the above problem manually (Hand written & verify the polynomial & Interpolated value at x = 50 Show all the necessary steps and submitted in the form of PDF along with the project/.ipynb file at GCR Plotting of Lagrange Polynomial htips:ifcola. research google.comidrvel 7GGWSUmFaHeMBhUS9CExGBINSTwAE2ro7authuser=2HscrolTo=Wirgrtco2OWS8prinModemrus 2/132108/2024, 18:13 1224345 Saad Ahmed - Copy of Session_2 Interpolation ipynb - Colaboratony import matplotlib.pyplot as plt winds yi=31.29079589843832 p = lagrange_poly(x[@:6], y[0:6]) print(p) xpenp. Linspace(@,x[5], 100) yp=p(xp) plt.plot(xp, yp, label='Lagrange Poly’) plt.plot(xi, yi, ‘bo', label="Interpolated Point’) plt.plot(x[9:6], y[@:6], ‘ro’, label='Data Points’) plt.xlabel('x') plt.ylabel(*y') plt.legend() plt.show() 5 4 3 2 -5.329e-06 x + 0.001313 x - 0.1132 x + 3.985 x - 47.81 x + 26 — Lagrange Poly ae Interpolated Point © Data Points 100 0 > 0 50 100 150 fig = plt.figure(figsize = (10,8)) [@, 20,40,68, 80, 100] (26.0, -48.6, 61.6,-71.2, 74.8, -75.2] i in range(1,n42,1) Lagrange_poly(x[@:i+1], y[@:i+1]) xpenp. Linspace(@,x[i], 100) yp=p(xp) plt.plot(xp, yp, label = #*L{i}") plt.plot(x,y, ‘ro’ ,label="Data Points") plt.plot(xi,yi, 'bo' ,label="Interpolated Points") plt.xlabel('x') plt.ylabel(‘y') pit. legend() plt.grid() plt.show() htips:ilcola. research google.comidrvel 7GGWSUmFaHeMShUS9CbxGBINSTwAE2ro authuser=2AscrollTo=Wirgrsco2OWS8printMode=trus 332108/2024, 18:13 150 - Scipy Implimentation of Lagrange Polynomial 1224345 Saad Ahmed - Copy of Session_2 Interpolation ipynb - Colaboratony u QB eel] GB “ & Data Points Interpolated Points AY Instead we calculate everything from scratch, in scipy, we can use the lagrange function directly to interpolate the data. Let's see the above example htips:ilcola. research google.comidrvel 7GGWSUmFaHeMShUS9CbxGBINSTwAE2ro authuser=2AscrollTo=Wirgrsco2OWS8printMode=trus ans2108/2024, 18:13 1224345 Saad Ahmed - Copy of Session_2 Interpolation ipynb - Colaboratony import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import lagrange # Define the data points X = np.array([@, 20, 48, 62, 86, 100]) y = np.array([26.0, -48.6, 61.6, -71.2, 74.8, -75.2]) # Define the Lagrange Polynomial = lagrange(x, y) # Find P(50) by evaluating the polynomial at x=50 pas = (45) print ("P(45) '» P45) # Plot the Lagrange Polynomial and the data points x_new = np.linspace(@, 102, 100) fig = plt.figure(figsize = (10,8)) plt.plot(x_new, f(x_new), 'b', x, y plt.plot(45, p_45, “go', markersize= plt.title(‘Lagrange Polynomial") plt.grid() plt.xlabel(‘x') plt.ylabel(*y') plt.show() P(45) = 31,29079589843832 Lagrange Polynomial 150 100 180 htips:ifcola. research google.comidrvel 7GGWSUmFaHeMBhUS9CbxGBINSTwAE2ro7authuser=2HscrolTo=Wirgrtco2OWS8prinModenrus 5/132108/2024, 18:13 1224945 Saad Ahmed - Copy of Session_2 Interpolation ipynb - Colaboratory v Task # 02 Use the below code and apply the following alteration and show them along with plot (i) Take input from user and show interpolation at that point along with its plot. i) Also add a code that will display the polynomial too. Code for Newton divided difference Method import nunpy as np def divided _difference_table(x, y): n= len(x) F = [[0] * n for 4 in range(n)] for i in range(n): F(AI[@] = yli] for j in range(1, n): for i in range(j, n): FCAD(G] = (FLAN(G-2] ~ FLG-2](5-2]) / (xEa] - xfi-41) return F def newton_div_dif_poly(x,y,xi): livided_difference_table(x,y) # Saving divided difference in a variable F n=len(x) -poly1d(1) ip-poly1d(F[0][0]) for i in range(1,n): p-poly1d(x[:i], True) Ne=np.poly1d(FLi][i]*(prod.c)) print(N) print (N(Qxi)) return x = [0, 20,40,60, 80, 100] y = [26.0, -48.6, 61.6, -71.2, 74.8, -75.2] newton_div_dif_poly(x, y,45) 5 4 3 2 -5.329e-6 x + @.001313 x - @.1132 x + 3.985 x - 47.81 x + 26 31. 29079589843672 ¥ TASK 2 SOLUTION htips:ifcola. research google.comidrvel 7GGWSUmFaHeMBhUS9CExGBINSTwAE2ro7authuser=2HscrolTo=Wirgrtco2OWS8prinMode=rus 61132108/2024, 18:13 1224345 Saad Ahmed - Copy of Session_2 Interpolation ipynb - Colaboratory import numpy as np import matplotlib.pyplot as plt def divided_difference_table(x, y): n= len(x) F = [[0] * n for i in range(n)] for i in range(n): F(i][@] = y[i] for j in range(1, n) for i in range(j, n): FEAJ(S] = (FLAN(5-2] - FEa-2)[5-2]) / (xi) - xfi-5)) return F def newton_div_dif_poly(x, y, xi): F = divided_difference_table(x, y) n= len(x) prod = np.polyld([1], True) N = np.poly1d(F[@][]) for i in range(1, n): prod *= np.polyld([1, -x[i-1]], True) N += F[i][i] * prod print(“Newton Divided Difference polynomial is:") print(N) print("Interpolated value at x =", xi, “is:", N(xi)) x_new = np.linspace(@, 100, 100) fig = plt.figure(figsize=(10, 8)) plt.plot(x_new, N(x_new), 'b', x, y, ‘ro') plt.plot(xi, N(xi), "go", markersize=10) plt.title( ‘Newton Divided Difference polynomial’) plt.grid() plt.xlabel(‘x') plt.ylabel('y') plt.show() point = float(input(“Enter x-coordinate to interpolate: “)) x= [@, 20, 40, 60, 80, 100] y = [26.0, -48.6, 61.6, -71.2, 74.8, -75.2] newton_div_dif_poly(x, y, point) htips:ifcola. research google.comidrvel 7GGWSUmFaHeMBhUS9CExGBINSTwAE2ro7authuser=2HscrolTo=Wirgrtco2OWS8prinModentrus 7/132108/2024, 18:13 1224345 Saad Ahmed - Copy of Session_2 Interpolation ipynb - Colaboratony Enter x-coordinate to interpolate: 5 Newton Divided Difference polynomial is uw 18 9 8 7 6 5.329e-06 x = 0.001034 x - 0.06804 x - 1.671 x - 7.838 x + 98.5 x 5 4 3 2 - 319 x + 515 x - 465.1 x + 228 x - 47.81 x + 26 Interpolated value at x = 5.0 is: -596987,7¢00000003 ael7 Newton Divided Difference polynomial 00} oe * * ° ° 05 10 a5 20 25 ° 20 0 60 80 100 v_ Task # 03 (A) Use the above code by adding divided difference table code i.e. it will show divided difference table. Task # 03 (B) htips:ifcola. research google.comidrvel 7GGWSUmFaHeMBhUS9CExGBINSTwAE2ro7authuser=2HscrolTo=Wirgrco2OWS8prinModentrus 8132erorz02s 18:18 4228245 Saad Aimed - Copy of Sesion_2 Interpolation pnb Colaboratony With the help of "pandas" as shown at the starting of this lab session, read code from provided csv file & Write a code for Newton's forward divided difference. Print the polynomial and plot the interpolating point too. Task # 03 (C) Do part 3(8) manually (Mentioned all steps and verify the result. TASK 3 SOLUTION va) htips:ifcola. research google.comidrivel 7GGWSUmFaHeMShUS9CExGBINSTwAE2ro7authuser=2HscrolTo=Wirgrtco2OWS8prinModenrus SIN2108/2024, 18:13 1224945 Saad Ahmed - Copy of Session_2 Interpolation ipynb - Colaboratory import numpy as np import matplotlib.pyplot as plt def divided_difference_table(x, y): n= len(x) F = [[@] * n for i in range(n)] for i in range(n): F(i][@] = y[i] for j in range(1, n) for i in range(j, n): FEAJ(5] = CFLAN(5-2] - FEi-2)[5-2]) / (xi) - xfi-5)) return F def newton_div_dif_poly(x, y, xi): F = divided_difference_table(x, y) # Saving divided difference in a variable F n= len(x) prod = np.polyid(1) N = np.poly1d(F[@][]) for i in range(1, n): prod = np.polyid(x[@:i], True) N += np.poly1d(F[i][i]*(prod.c)) # Print Newton Divided Difference polynomial and interpolated value print ("Newton Divided Difference polynomial is:") print (N) print("Interpolated value at x =", xi, “is:", N(xi)) # Plot the Newton Divided Difference polynomial and the data points x_new = np.linspace(@, 100, 100) fig = plt.figure(figsize=(10, 8)) plt.plot(x_new, N(x_new), 'b’, x, y, ‘ro') plt.plot(xi, N(xi), ‘go’, markersize=10) plt.title( ‘Newton Divided Difference polynomial’) plt.grid() plt.xlabel('x") plt.ylabel(‘y") plt.show() return # Interpolate at a specific point point = float(input(“Enter x-coordinate to interpolate: ")) x = [@, 20, 40, 60, 80, 100] y = [26.@, -48.6, 61.6, -71.2, 74.8, -75.2] newton_div_dif_poly(x, y, point) htips:ifcola. research google.comidrvel 7GGWSUmFaHeMBhUS9CExGBINSTw4E2ro7authuser=2HscrolTo=Wirgrtco2OWS8prinMode=trus 10/132108/2024, 18:13 1224345 Saad Ahmed - Copy of Session_2 Interpolation ipynb - Colaboratony Enter x-coordinate to interpolate: 6 Newton Divided Difference polynomial is: 5 4 3 2 -5.329e-06 x + 0.001313 x - 0.1132 x + 3.985 x - 47.81 x + 26 Interpolated value at x = 6.0 is: -140.18998555e00002 Newton Divided Difference polynomial 100 150 » b) htips:ifcola. research google.comidrvel 7GGWSUmFaHeMBhUS9CExGBINSTwAE2ro7authuser=2HscrolTo=Wirgrtco2OWS8prinModemtrus 11/132108/2024, 18:13 1224345 Saad Ahmed - Copy of Session_2 Interpolation ipynb - Colaboratory import numpy as np import pandas as pd import matplotlib.pyplot as plt import math from google.colab import files uploaded = files.upload() File_name = list(uploaded. keys())(0] df = pd.read_csv(file_name) df['x"] values y = df['y'].values def divided_difference_table(x, y): n = len(x) F = [[@] * n for 4 in range(n)] for i in range(n): FAIL] = yLi] for j in range(1, n): for i in range(j, n) PLACES] = (FES]EJ-2] - FE4-2) (5-2) return F def newton forward div dif_poly(x, y, xi): divided _difference_table(x, y) Jen(x) x1] - x[@] prod = np.poly1d(1) N = np.poly1d(F[@][0]) for i in range(1, n prod *= np.poly1d([1, (xi - x[i-1]) / h - 1], True) N += (F[A][i] / math.factorial(i)) * prod print ("Newton Forward Divided Difference polynomial is:") print(N) print ("Interpolated value at x xi, "isi", N(xi)) x_new = np.linspace(min(x), max(x), 100) fig = plt.figure(figsize=(10, 8)) plt.plot(x_new, N(x_new), 'b’, x, y, ‘ro') plt.plot(xi, N(xi), ‘go’, markersize=10) plt.title( ‘Newton Forward Divided Difference polynomial’ ) plt.grid() plt.xlabel('x") plt.ylabel('y") plt.show() return point = 55 newton_forward_div dif _poly(x, y, point) htips:ifcola. research google.comidrvel 7GGWSUmFaHeMBhUS9CbxGBINSTwHE2ro7authuser=2HscrolTo=Wirgrtco2OWS8prin’Mode=trus 12/132108/2024, 18:13 1224345 Saad Ahmed - Copy of Session_2 Interpolation ipynb - Colaboratony (224246 Sa. .polaionesv k224345 Saad Ahmed - Copy of interpolation - k224345 Saad Ahmed - Copy of interpolation.csv(text/csv) - 55 bytes, last modified: 26/03/2024 - 100% done Saving 224345 Saad Aimed - Copy of interpolation ~ k224345 Saad Ahmed - Copy of in newton Forward Divided Difference polynomial. is: 16 3 8 7 6 5 0.3183 x = 0.4438 x ~ 0.4406 x + 4,292 x - 5.428 x ~ 5.479 x a 3 2 + 15.04 x» 2.875 x + 6.286 x = 36.9 x + 58.65 Interpolated value at x = 55 1s) 2.789911627388175er16 1e19 Newton Forward Divided Difference polynomial 10 os 08 oa 02 0.0} —¢——#__+__@-4— . . 0 20 40 60 80 100 Double-click (or enter) to edit htips:ifcola. research google.comidrvel 7GGWSUmFaHeMBhUSSCExGBINSTwAE2ro7authuser=2HscrolTo=Wirgrico2OW38prinMode=trus 13/13
You might also like
18.1 - Newton's Divided-Difference Interpolating
PDF
No ratings yet
18.1 - Newton's Divided-Difference Interpolating
23 pages
m2 Math Lab
PDF
No ratings yet
m2 Math Lab
32 pages
Python Computer Scince
PDF
No ratings yet
Python Computer Scince
34 pages
Lab 8
PDF
No ratings yet
Lab 8
5 pages
Numerical Computing Python
PDF
No ratings yet
Numerical Computing Python
5 pages
U9-Newton Interpolation
PDF
No ratings yet
U9-Newton Interpolation
13 pages
Numerical
PDF
No ratings yet
Numerical
6 pages
22BCS12528 Sitanshu Python 3
PDF
No ratings yet
22BCS12528 Sitanshu Python 3
2 pages
Interpolation Newton Python
PDF
No ratings yet
Interpolation Newton Python
3 pages
Data Interpolation
PDF
No ratings yet
Data Interpolation
16 pages
Unit 2 Interpolation P2
PDF
No ratings yet
Unit 2 Interpolation P2
2 pages
Nmoup Assignment Pankaj
PDF
No ratings yet
Nmoup Assignment Pankaj
5 pages
Interpolation
PDF
No ratings yet
Interpolation
8 pages
Lec. 3 Interpolation
PDF
No ratings yet
Lec. 3 Interpolation
26 pages
13 Interpolation - Lagrange NDD
PDF
No ratings yet
13 Interpolation - Lagrange NDD
23 pages
5b Curve Fitting Interpolation
PDF
No ratings yet
5b Curve Fitting Interpolation
71 pages
Computer Programming and Application: 3 Interpolation and Curve Fitting
PDF
No ratings yet
Computer Programming and Application: 3 Interpolation and Curve Fitting
43 pages
HW3MN
PDF
No ratings yet
HW3MN
7 pages
Interpolation
PDF
No ratings yet
Interpolation
20 pages
Lecture 4
PDF
No ratings yet
Lecture 4
21 pages
Assignment 2
PDF
No ratings yet
Assignment 2
5 pages
Newtons Backward and Lagrange Interpolation
PDF
No ratings yet
Newtons Backward and Lagrange Interpolation
6 pages
Informe
PDF
No ratings yet
Informe
3 pages
Labooratory 8
PDF
No ratings yet
Labooratory 8
7 pages
1.1 3 - Interpolation - and - Curve - Fitting PDF
PDF
No ratings yet
1.1 3 - Interpolation - and - Curve - Fitting PDF
15 pages
Common Program
PDF
No ratings yet
Common Program
17 pages
L6 Interpolation Lagrange Method
PDF
No ratings yet
L6 Interpolation Lagrange Method
29 pages
Maths Final Report
PDF
No ratings yet
Maths Final Report
31 pages
Module1 Part3
PDF
No ratings yet
Module1 Part3
29 pages
Lecture 21
PDF
No ratings yet
Lecture 21
24 pages
w6 EEF311E Interpolation 2
PDF
No ratings yet
w6 EEF311E Interpolation 2
52 pages
Lab 2
PDF
No ratings yet
Lab 2
5 pages
Lecture 13
PDF
No ratings yet
Lecture 13
20 pages
CH 5 Interpolation - Fall - 24 25
PDF
No ratings yet
CH 5 Interpolation - Fall - 24 25
10 pages
Nisha NT
PDF
No ratings yet
Nisha NT
10 pages
Chapter 3
PDF
No ratings yet
Chapter 3
30 pages
CH-6, MATH-5 - LECTURE - Summer - 22-23
PDF
No ratings yet
CH-6, MATH-5 - LECTURE - Summer - 22-23
16 pages
Interpolation
PDF
No ratings yet
Interpolation
11 pages
CH-5, Math-5 - Lecture - Note
PDF
No ratings yet
CH-5, Math-5 - Lecture - Note
11 pages
1 Motivation For Newton Interpolation
PDF
No ratings yet
1 Motivation For Newton Interpolation
5 pages
L8 CurveFitting (Interpolation)
PDF
No ratings yet
L8 CurveFitting (Interpolation)
60 pages
5 Ders
PDF
No ratings yet
5 Ders
7 pages
Interpolation
PDF
No ratings yet
Interpolation
12 pages
CH-6, Math-5 - Lecture - Note
PDF
No ratings yet
CH-6, Math-5 - Lecture - Note
16 pages
Numerical Methods With Applications
PDF
No ratings yet
Numerical Methods With Applications
29 pages
ECE421 - The Newton Polynomial Interpolation
PDF
No ratings yet
ECE421 - The Newton Polynomial Interpolation
36 pages
EEE 3104labsheet Expt 03
PDF
No ratings yet
EEE 3104labsheet Expt 03
7 pages
Slide16 Interpolation
PDF
No ratings yet
Slide16 Interpolation
37 pages
CH6 (1) Interpolation
PDF
No ratings yet
CH6 (1) Interpolation
22 pages
Interpolation
PDF
No ratings yet
Interpolation
19 pages
Newton Polynomials
PDF
No ratings yet
Newton Polynomials
3 pages
Interporation
PDF
No ratings yet
Interporation
20 pages
Scientific Computing: Interpolation - Polynomial Interpolation
PDF
No ratings yet
Scientific Computing: Interpolation - Polynomial Interpolation
19 pages
SauerNumAnalyISM 286855 04 Ch3
PDF
No ratings yet
SauerNumAnalyISM 286855 04 Ch3
26 pages
Chapter 8 - Interpolation
PDF
No ratings yet
Chapter 8 - Interpolation
14 pages
Lab 4 Newton Divided Difference Lagrange Interpolation: Objectives
PDF
No ratings yet
Lab 4 Newton Divided Difference Lagrange Interpolation: Objectives
9 pages
Lab 06 Divided Difference and Lagrange Interpolation
PDF
No ratings yet
Lab 06 Divided Difference and Lagrange Interpolation
8 pages
Course No.: EEE 212 Group No.:06 Course Title: Numerical Technique Laboratory
PDF
No ratings yet
Course No.: EEE 212 Group No.:06 Course Title: Numerical Technique Laboratory
5 pages