Question

4. Let f(x) e. Compare evenly spaced interpolation with Chebyshev interpolation by plotting degree n polynomials of both type

- solution must be coded in either matlab or Octave.

- must avoid use of symbolic variables such as "syms x." here we are instructed to use the base GNU Octave, that is, without any external packages.

- best of luck, I am rather stuck

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

%Matlab code for interpolating polynomials
clear all
close all
%Function for which interpolation have to do
f=@(x) exp(abs(x));
%all n values
n=[10 20];
%loop for creating interpolating function for all n
for i=1:length(n)
%evenly spaced points for -1:1
xinU=linspace(-1,1,n(i)+1);
%corresponding function value for evenly spaced point
yinU=double(f(xinU));
%Chebyshev points for -1:1
xinC=cos(linspace(-pi,0,n(i)+1));
%corresponding function value for Chebyshev points
yinC=double(f(xinC));
xout=-1:0.01:1;
%creating interpolating function for evenly spaced points
p1=polyfit(xinU,yinU,n(i));
ss=0;
%loop for creating polynomial function for evenly spaced points
syms x
for ii=1:length(p1)
ss=ss+p1(ii)*x.^(n(i)+1-ii);
end
%the polynomial function for evenly spaced points
fun1(x)=ss;

%creating interpolating function for Chebyshev points
p2=polyfit(xinC,yinC,n(i));
ss=0;
%loop for creating polynomial function for Chebyshev points
syms x
for ii=1:length(p2)
ss=ss+p2(ii)*x.^(n(i)+1-ii);
end
%the polynomial function for Chebyshev points
fun2(x)=ss;
%Plotting the interpolating function for evenly spaced points
figure(2*i-1)
hold on
plot(xout,f(xout))
%calling of polynomial function for evenly spaced points
plot(xout,fun1(xout))
title(sprintf('Interpolation plot for evenly spaced grid for n=%d',n(i)))
xlabel('XinU')
ylabel('f(x)')
legend('Actual data','evenly spaced interpolated data')
hold off
%Plotting the interpolating function for Chebyshev points
figure(2*i)
hold on
plot(xout,f(xout))
%calling of polynomial function for Chebyshev points
plot(xout,fun2(xout))
title(sprintf('Interpolation plot for Chebyshev grid for n=%d',n(i)))
legend('Actual data','Chebyshev interpolated data')
xlabel('XinC')
ylabel('f(x)')
hold off
%finding error for interpolated data
err1=norm((f(xout)-fun1(xout)),'inf');
fprintf('Error norm for evenly spaced interpolation for n=%d is %f\n',n(i),err1)
err2=norm((f(xout)-fun2(xout)),'inf');
fprintf('Error norm for Chebyshev interpolation for n=%d is %f\n\n',n(i),err2)
end

Matlab code for interpolating polynomials clear all close all Function for which interpolation have to do -e(x) exp (abs (x))figure (2*i) hold on plot (xout, f( xout)) tcalling of polynomial function for Chebyshev points plot (xout, fun2 (xout)) titlInterpolation plot for evenly spaced grid for n-10 Actual data evenly spaced interpolated data 2E 2 4 2 2 1 8 1.6 1 4 12 n6 0Interpolation plot for evenly spaced grid for n-20 100 Actual data evenly spaced interpolated data 80 60 40 20 H n6 0.8 20 1

Add a comment
Know the answer?
Add Answer to:
- solution must be coded in either matlab or Octave. - must avoid use of symbolic...
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
  • 1) Use the Matlab function polyfit to curve fit the saturation pressure versus temperature data a...

    MATLAB Code for question (2) 1) Use the Matlab function polyfit to curve fit the saturation pressure versus temperature data along the vaporization line for water in the table below with a polynomial of degree n. The Matlab function polyval may be used to evaluate the polynomial at any point. Compare the saturation pressure as calculated by the polynomial with the data given in the table and observe what happens as the degree of the polynomial n is increased. Tabulate...

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