Question

Use MATLAB or Scilab to solve the following problems 1. Create a MATLAB subroutine called Lagrange.m that receives two set data points, x and y and plots the curve by interpolating the missing points (h-X-x-1) using Lagrange interpolation form

2. Create a main program that calls the subroutine created on problem 1 and compare results using the following data sets: b. (1 5), (0, 8), (3,-10) C. (-10.2), ( 4,5), (7, 3), (12, 20)

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

1) Below code is subroutine lagrange, Save this subroutine as lagrange.m. and call the function in the main program.

explanation of the code is in the comments.

function y=lagrange(x,pointx,pointy)
%% In this function x is the points to be calculted, y is the interpolated value
%% calculate the input size
n=size(pointx,2);

%% Generate the initial values of the x
L=ones(n,size(x,2));

%% Calcaulate the missing points using legranges interpolation formule
for i=1:n
for j=1:n
if (i~=j)

L(i,:)=L(i,:).*(x-pointx(j))/(pointx(i)-pointx(j));
end
end
end
y=0;
for i=1:n
y=y+pointy(i)*L(i,:);
end

%%Sorting X and Y
X=[x pointx];
Y=[y pointy];
[sortedX, sortIndex] = sort(X)
sortedY = Y(sortIndex)
%% Plot the graph
plot(pointx,pointy,'Color','red');
hold on;
plot(sortedX,sortedY,'Color','blue');
hold off;
legend('original','after Inter polation');

2) Data sets are compared using the below program

close all;
clc;
clear all;
%% Given input data points
pointX=[1,4];
pointY=[3,7];

%% Missing points
x=[2,4];

%% Interpolating using the Sub routine
y=lagrange(x,pointX,pointY);

%% Given input data points
pointX=[-1,0,3];
pointY=[5,8,-10];

%% Missing points
x=[1,2];
figure;
%% Interpolating using the Sub routine
y=lagrange(x,pointX,pointY);

%% Given input data points
pointX=[-10,4,7,12];
pointY=[-2,5,3,20];

%% Missing points
x=[-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,5,6,8,9,10,11];
figure;
%% Interpolating using the Sub routine
y=lagrange(x,pointX,pointY);

Below plots shows the data set before interpolation and after interpolation

a) Below plot is for data point (1,3) (4,7)

original after Inter polation 6.5 4.5 3.5 3 1.5 2.5 3.5

b) Below plot is for data point (-1,5) (0,8)(3,-10)

origina after Inter polation -4 -6 -8 -10 1 0.5 0.5 2 2.5

c) below is the plot for(-10,-2) (4,5) (7,3) (12,20)

20 origina after Inter polation 15 10 10

Add a comment
Know the answer?
Add Answer to:
Use MATLAB or Scilab to solve the following problems 1. Create a MATLAB subroutine called Lagrange.m...
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
  • must be done in MATLAB 2. Create a main program that calls the subroutine created on problem 1 and compare resu...

    must be done in MATLAB 2. Create a main program that calls the subroutine created on problem 1 and compare results using the following data sets: b. (1 5), (0, 8), (3, -10) С. (-10,-2). ( 4,5), (7, 3), (12, 20) Output: (copy and paste the output in the following box) Use MATLAB or Scilab to solve the following problems 1. Create a MATLAB subroutine called Lagrange.m that receives two set data points, x and y and plots the curve...

  • 2. Create a main program that calls the subroutine created on problem 1 and compare results using the following data sets: b. 1 5), (0, 8), (3,-10) С. (-10,-2), (45), (73), (12, 20) t: (c...

    2. Create a main program that calls the subroutine created on problem 1 and compare results using the following data sets: b. 1 5), (0, 8), (3,-10) С. (-10,-2), (45), (73), (12, 20) t: (copy and paste the output in the following box) Use MATLAB or Scilab to solve the following problems 1. Create a MATLAB subroutine called Lagrange.m that receives two set data points, x and y and plots the curve by interpolating the missing points (hEX-Xi-1) using Lagrange...

  • in put and output done in computer please Use MATLAB or Scilab to solve the following...

    in put and output done in computer please Use MATLAB or Scilab to solve the following problems 1. Create a MATLAB subroutine called Lagrange.m that receives two set data points, x and y and plots the curve by interpolating the missing points (h=x-X.1) using Lagrange interpolation form.

  • USE MATLAB 3. Use MATLAB or Scilab to implement cubic spline, and plot the points and...

    USE MATLAB 3. Use MATLAB or Scilab to implement cubic spline, and plot the points and the curve for the following data sets. x-[018 12 27) and y=[12345) Input: (copy and paste the MATLAB or Sclla command in the following box) Output: (copy and paste the output in the following box)

  • IN MATLAB Write a MATLAB function that can be used by a user to perform polynomial...

    IN MATLAB Write a MATLAB function that can be used by a user to perform polynomial interpolation using Lagrange Interpolation Method on a set of data. You should name your function as YourInitials_Lagrange_interpolation. 1. FUNCTION INPUT: . The input data pairs x and f(x) should be expressed as vectors and are used to construct the interpolation polynomials The interpolating data points/value The order of interpolating polynomials to be used. For this project you code should handle first order, second order...

  • Problem 2. Given the data points (xi. yi), with xi 2 02 4 yil 5 1 1.25 find the following interpo...

    Problem 2. Given the data points (xi. yi), with xi 2 02 4 yil 5 1 1.25 find the following interpolating polynomials, and use MATLAB to graph both the interpolating polynomials and the data points: a) The piecewise linear Lagrange interpolating polynomialx) b) The piecewise quadratic Lagrange interpolating polynomial q(x) c) Newton's divided difference interpolation pa(x) of degree s 4 Problem 2. Given the data points (xi. yi), with xi 2 02 4 yil 5 1 1.25 find the following...

  • i got it incomplete sorry I put this by mistake. can this be canceled? 4. Use MATLAB or Scilab to implement linea...

    i got it incomplete sorry I put this by mistake. can this be canceled? 4. Use MATLAB or Scilab to implement linear regression, and plot the points and the curve for the following data sets. x [018 12 27) and y [12345 Input: (copy and paste the MATLAB or Sclab commandin the folowing bax) cic X1-101 8 12 271: Y1-11 234 5] Y-transpose(Y1) for n-1:5 X(n,2)-X1(n) end format long ycalc-S(2) X+S(1); scatter(X1,Y1) hold on plot(X,ycalc) xlabel(X) ylabel(Y) title('Linear Regression Relation...

  • matlab The error function is a mathematical function that frequently arises in probability and statistics. It also c...

    matlab The error function is a mathematical function that frequently arises in probability and statistics. It also can show up in the solution to some partial differential equations, particularly those arising in heat and mass transfer applications. The error function is defined as 2 e-t dt picture attached This function is actually built-in to MATLAB as the command erf, and here we'll use that function to compute a "true value" with which we can compare results of two interpolation approaches....

  • 4. For the following table, answer the questions. (1) Find the cubic Newton’s interpolating polynomial using...

    4. For the following table, answer the questions. (1) Find the cubic Newton’s interpolating polynomial using the first four data points and estimate the function value at x=2.5 with the interpolating polynomial. (2) Find the quartic Newton’s interpolating polynomial using the five data points and estimate the function value at x=2.5 with the interpolating polynomial. (3) Find the bases functions of Lagrange interpolation, Li(x) (i=1,2,…,5), and estimate the function value at x=2.5 with the Lagrange interpolating polynomial. 3 5 1...

  • Matlab Matlab Matlab Matlab, Please solve this problem Problem 4 : Runge's phenomenon For this proble,...

    Matlab Matlab Matlab Matlab, Please solve this problem Problem 4 : Runge's phenomenon For this proble, you wil interpolate the function h(x) = tanh(10x) in I [a, b, through n datapoints (xi, hx with equidistant nodes for several values of n, and observe the behavior of the interpolating polynomial as n increases. You should use the pair of functions polyfit and polyval In your prob40: (a) Interpolate the function tanh(10x) in [-1,1] using a uniform grid of n nodes, where...

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