The following table shows the average weight of two samples taken from medical f
ID: 2082434 • Letter: T
Question
The following table shows the average weight of two samples taken from medical filed. Use Lagrange interpolation to approximate the average weight curves for each sample. You are required to write a Matlab function called lagrageP.m with two inputs x and y vectors, and the output to be as follow: a) The approximated polynomials P2 (you will have two of them) b) The L matrices that will help creating the polynomials. (Use for loop to compute L1, L2...etc.) c) Two plots that shows the data points and the approximation curves as shown below: d) Two more plots that compare your resulted polynomials, the given data set points, with the resulted polynomials by using the command polyfit (x, y, n) Use n = 3. e) Combine all of your four plots into single figure using the command subplotExplanation / Answer
Thanks for choosing chegg! here's the matlab code..
X=[0 6 10 13 17 20 28];
Y1=[6.67 17.33 42.67 37.33 30.10 29.31 28.74];
Y2=[6.67 16.11 18.89 15 10.56 9.44 8.89];
%lagrangepoly function takes the data points as arguments and returns the lagrangian function coefficients
f1=lagrangepoly(X,Y1); %f1 has the lagrangian function coefficients for sample one
f2=lagrangepoly(X,Y2);%f2 has the lagrangian function coefficients for sample two
p1=polyfit(X,Y1,3);% p1 and p2 are polynomial approximations for sample 1 and sample 2 respectively
p2=polyfit(X,Y2,3);
subplot(4,1,1)
plot(X,polyval(f1,X),'-^',X,Y1,'--*');
xlabel('X values');
ylabel('Y values');
legend('lagrangian approx', 'original');
title('lagrangian for sample 1');
subplot(4,1,2)
plot(X,polyval(f2,X),'-^',X,Y2,'--*');
xlabel('X values');
ylabel('Y values');
legend('lagrangian approx', 'original');
title('lagrangian for sample 2');
subplot(4,1,3)
plot(X,polyval(p1,X),'-^',X,Y1,'--*');
xlabel('X values');
ylabel('Y values');
legend('polyfit approx', 'original');
title('polyfit for sample 1');
subplot(4,1,4)
plot(X,polyval(p2,X),'-^',X,Y2,'--*');
xlabel('X values');
ylabel('Y values');
legend('polyfit approx', 'original');
title('polyfit for sample 2');
here's the lagrangian function
function f=lagrangepoly(xi,yi)
n=length(xi);
f=zeros(1,n);
Ilog=logical(eye(n));
for i=1:n
Pi=poly(xi(~Ilog(i,:)));
Pi=Pi./polyval(Pi,xi(Ilog(i,:)));
Pi=Pi.*yi(Ilog(i,:));
f=f+Pi;
end
Thank you!! Happy learning!! :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.