The document describes a computer program that uses Lagrange interpolation to calculate an unknown function value given several known x and y data points. The program:
1) Takes user input of the number of known data points and the x and y values.
2) Takes a value for x to calculate the function value for.
3) Uses the Lagrange interpolation formula to calculate the function value as the weighted sum of the y-values, with weights determined by the x-values.
4) Prints the calculated function value.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
105 views
Fortran Code For
The document describes a computer program that uses Lagrange interpolation to calculate an unknown function value given several known x and y data points. The program:
1) Takes user input of the number of known data points and the x and y values.
2) Takes a value for x to calculate the function value for.
3) Uses the Lagrange interpolation formula to calculate the function value as the weighted sum of the y-values, with weights determined by the x-values.
4) Prints the calculated function value.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4
1b) Write general purpose computer program for Lagrangian interpolation and use it to
solve the problem.
c Lagrange's Method of interpolation dimension x(20),y(20) write(*,50) 50 format(1x,'To find the function value for a particular value ',\) write(*,60) 60 format('of x using Lagrange interpolation formula.') write(*,*) write(*,*)'Enter the number of known function values:' read(*,*)n write(*,*)'Enter value of x and corresponding function values:' DO10 i=1,n read(*,*)x(i),y(i) 10 continue write(*,*)'Enter x for which function value is to be found:' read(*,*)a s=0 DO 20 i=1,n p=1 DO 30 j=1,n IF(i.NE.j) p=p*(a-x(j))/(x(i)-x(j)) 30 continue s=s+p*y(i) 20 continue write(*,*) write(*,40)a,s 40 format(1x,'The function value at x =',F7.2,' is:',F9.3) STOP END
Output To find the function value for a particular value of x using Lagrange interpolation formula.
Enter the number of known function values: 4 Enter value of x and corresponding function values: 321 2.50651 322.8 2.50893 324.2 2.51081 325.0 2.51188 Enter x for which function value is to be found: 323.5
The function value at x = 323.50 is: 2.510 Stop - Program terminated.
Press any key to continue
2b) Write general purpose computer program for Hermitian interpolation and use it to solve the problem. $debug c Hermitian method ofinterpolation dimension x(20),y(20),y1(20),A(20),b(20) open(unit=1,file='input.dat',status='old') open(unit=2,file='B.out',status='unknown') open(unit=3,file='A.out',status='unknown') open(unit=4,file='Y.out',status='unknown') read(1,*)n read(1,*) do i=1,n read(1,*)x(i),y(i),y1(i) end do do 20 i=1,n s1=0 Do 40 j=1,n if(i.ne.j)s1=s1+(1/(x(i)-x(j))) 40 continue b(i)=2*(5-x(i))*s1 write(2,100)b(i) A(i)= y(i)*(1-b(i))+((5-x(i))*y1(i)) write(3,100)A(i) 20 continue s=0 do 50 i=1,n p=1 do 30 j=1,n IF(i.NE.j) p=p*((5-x(j))/(x(i)-x(j)))**2 30 continue s=s+p*A(i) 50 continue write(4,100)s 100 FORMAT(4F10.5) STOP End Output .00000 17.00000 .00000 13.26667 .00000