Question

3. Consider the function f(x) = cos(x) in the interval [0,8]. You are given the following 3 points of this function: 10.5403

x = 0:0.1:8 Note: Use fplot for this part DELIVERABLES: The commands and the resulting plot from MATLAB. (d) (2 points) Make

3. Consider the function f(x) = cos(x) in the interval [0,8]. You are given the following 3 points of this function: 10.5403 2 -0.4161 6 0.9602 (a) (2 points) Calculate the quadratic Lagrange interpolating polynomial as the sum of the Lo(x), L1(x), L2(x) polynomials we defined in class. The final answer should be in the form P)a2 bx c, but with a, b, c known. DELIVERABLES: All your work in constructing the polynomial. This is to be done by hand not MATLAB (b) (2 points) Plot f(x) cos() in the interval [0, 8] by creating vectors x 0:0, 1:8; y cos (x) ; Use the MATLAB hold on command to retain the plot and plot the three data points provided above as stars. There is a similar example in the Lecture Notes for lecture 15. To plot the first star for example use plot(1, 0.5403, *) DELIVERABLES: The commands and the resulting plot from MATLAB (c) (2 points) Plot each polynomial Lo(x)/(20),L,(z)/(x),L2(x)/(#2) for these specific values using the same axis as part (b)
x = 0:0.1:8 Note: Use fplot for this part DELIVERABLES: The commands and the resulting plot from MATLAB. (d) (2 points) Make a new figure with f(x) and the points as stars as well as the sum of Lo(x)f (xo), Li()f(x), L2(x)f(r2) using the same x = 0:0.1:8 but without showing Lo(x), L1 (x), L2(x) DELIVERABLES: The commands and the resulting plot from MATLAB.
0 0
Add a comment Improve this question Transcribed image text
Answer #1


mkupolotio polyn Lio) 17 20-M Hesu 2 0.4 16 I L20n G)-2) 0- S403Matlab code for Lagrange polynomial clear all close all %All given data point for x and y xx-[1 2 6]; yy-[0.5403 -0.4161 0.96plot (x1,yl, Linewidth,2) plot (xx, yy,r* ) plot (x1,y2, Linewidth,2) legendActual function, given data point, LagrangL(x) vs. x plot 2.5 -LI(x) 1.5 0.5 0.5 xvs。t(x) plot Actual function given data point nterpolation

%%Matlab code for Lagrange polynomial
clear all
close all
%All given data point for x and y
xx=[1 2 6];
yy=[0.5403 -0.4161 0.9602];

%answering question b.

%function for which interpolation have to do
f=@(x) cos(x);
%all given x data and y data
x1=0:0.1:8;
y1=f(x1);

%plotting data
hold on
plot(x1,y1,'Linewidth',2)
plot(xx,yy,'r*')
xlabel('x')
ylabel('f(x)')
title('f(x) vs. x plot')
legend('Actual data','Given data point')
%answering question c.
%function for all L(x)
L0=@(x) ((x-2).*(x-6))./5;
L1=@(x) -((x-1).*(x-6))./4;
L2=@(x) ((x-1).*(x-2))./20;

figure(2)
hold on
plot(x1,L0(x1).*f(xx(1)),'Linewidth',2)
plot(x1,L1(x1).*f(xx(2)),'Linewidth',2)
plot(x1,L2(x1).*f(xx(3)),'Linewidth',2)
legend('L0(x)','L1(x)','L2(x)')
xlabel('x')
ylabel('L(x)')
title('L(x) vs. x plot')

%answering question d.
syms x
summ(x)=L0(x)*f(xx(1))+L1(x)*f(xx(2))+L2(x)*f(xx(3));
for i=1:length(x1)
    y2(i)=double(summ(x1(i)));
end

%displaying Lagrange function
fprintf('displaying Lagrange function\n')
disp(vpa(summ,2))

figure(3)
hold on
plot(x1,y1,'Linewidth',2)
plot(xx,yy,'r*')
plot(x1,y2,'Linewidth',2)
legend('Actual function','given data point','Lagrange interpolation')
title('x vs. f(x) plot')

%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%

Add a comment
Know the answer?
Add Answer to:
3. Consider the function f(x) = cos(x) in the interval [0,8]. You are given the following 3 point...
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
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