Question

% This program calculates the Lagrange interpolating polynomial of a given % xp and returns the...

% This program calculates the Lagrange interpolating polynomial of a given % xp and returns the value
clc, clear all;
format short;
x=[0 20 40 60 80 100];% This string can be filled by Matlab
f_x=[26.0 48.6 61.6 71.2 74.8 75.2]; % You can create a separate function to fill
f_x automatically
n=length(x)-1;
xp=input('Enter a value xp between 0 and 100: '); P_x=0;
for i=1:n+1
L_k=1;
for j=1:n+1
if j~=i L_k=L_k*(xp-x(j))/(x(i)-x(j));
end end
P_x=P_x+f_x(i)*L_k;
end
disp ('The polynomial P_xp at xp=55 is: '); P_xp=P_x


_______


given function f(x)=cos(x)-x
A. Edit the program in such a way it:

1. Prompts the user to enter a string containing the values of x (x0, x1,
x2,...xn).

2. Calculates the values of f(x) that should be filled in the string called f_x. 3. Returns the value of the P3(x1) (Third order Lagrange InterpolatingPolynomial evaluated at x=x1).

B. Compare the P3(x1) returned by the program to the f(x1) available inside
the string f_x.

Homework Answers

Answer #1

We have modified the the given code in the required form.

OUTPUT

-------------------------------------------MATLAB code-------------------------------------------

clc;
clear all
format short;
fprintf('\n\n')
x=input('Enter the values of x as a vector: ');
f_x=cos(x)-x;
n=numel(x)-1;

% xp=input('Enter a value xp between 0 and 100: ');

xp=x(2);
P_x=0;

for i=1:n+1
L_k=1;
for j=1:n+1
if j~=i
L_k=L_k*(xp-x(j))/(x(i)-x(j));
end
end
P_x=P_x+f_x(i)*L_k;
end
fprintf('\n\n')
disp (['The polynomial P_xp at x1=',num2str(x(2)), ' is: ', num2str(P_x)]);
fprintf('\n\n')
disp (['Absolute error at x1=',num2str(x(2)), ' is: ', num2str(abs(P_x-f_x(2)))]);
fprintf('\n\n')

Know the answer?
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
Similar Questions
Lagrange Interpolation polynomial using python of degree 3 I understand the math for the question I...
Lagrange Interpolation polynomial using python of degree 3 I understand the math for the question I cannot seem to get the correct python code please help with python to solve For f (x) = x ln(x), (1) use appropriate Lagrange interpolating polynomial of degree three to approximate f(8.4). Use the following data: f(8.1) = 16.94410, f(8.3) = 17.56492, f(8.6) = 18.50515, f(8.7) = 18.82091. (2) use appropriate Newton interpolation polynomial again to redo the work. Everything has to be done...
Design a program that calculates the amount of money a person would earn over a period...
Design a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a...
Problem 3 you can use Matlab and also i give u the Problem 1 code its...
Problem 3 you can use Matlab and also i give u the Problem 1 code its on Matlab Using the same initial code fragment as in Problem 1, add code that calculates and plays y (n)=h(n)?x (n) where h(n) is the impulse response of an IIR bandpass filter with band edge frequencies 750 Hz and 850 Hz and based on a 4th order Butterworth prototype. Name your program p3.sce this is the Problem 1 code and the solutin clear; clc;...
Problem 1 ...... you can use Matlan i got one so all what i need is...
Problem 1 ...... you can use Matlan i got one so all what i need is 2, 3 and 4 one of them or all of them .. thanks The following Scilab code generates a 10-second “chirp” with discrete frequencies ranging from 0 to 0.2 with a sampling frequency of 8 kHz. clear; Fs = 8000; Nbits = 16; tMax = 10; N = Fs*tMax+1; f = linspace(0.0,0.2,N); x = zeros(f); phi = 0; for n=0:N-1 x(n+1) = 0.8*sin(phi); phi...
The following algorithm finds the initial substring of y that can be reversed and found in...
The following algorithm finds the initial substring of y that can be reversed and found in y. For example, longestInitialReverseSubstringLength(“aabaa”) = 5, because “aabaa” is the same string forwards and backwards, so the longest initial substring that can be reversed and found in the string is “aabaa”. Also, longestInitialReverseSubstringLength(“bbbbababbabbbbb”) is 6, because “babbbb” can be found in the string (see color-highlighted portions of the string), but no longer initial string exists reversed in any part of the string. longestInitialReverseSubstringLength(String y)...
Modify your Free Fall program below to plot the energy vs. time for a baseball dropped...
Modify your Free Fall program below to plot the energy vs. time for a baseball dropped from a height of 1000 feet until it hits the ground. Now add in the effects of air resistance into your program. This time, on the energy plot, you will plot four quantities: KE (Red line), PE (Green line), Energy lost due to air resistance (purple line), and total Energy (blue circles). Note, you must calculate the energy lost by using the definition of...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
The C++ program steps through the array x[]. For each i, if x[i] < x[i+1], i...
The C++ program steps through the array x[]. For each i, if x[i] < x[i+1], i is saved in the array ascend[], in order. Compile and run the program; it should print 0 4 5. In this exercise, you’ll try to translate the C++ program to MIPS. Some of the more tedious parts are already given in Assignment3.F19.s. You won’t have to write the data allocation sections, some of the initializations, and the output loop at the end. So the...
Write a method that returns the sum of all the elements in a specified column in...
Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) The program should be broken down into methods, menu-driven, and check for proper input, etc. The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT