
- 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
%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




- solution must be coded in either matlab or Octave. - must avoid use of symbolic...
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...