Numerical Analysis Lab 6: Syed Muhammad Hussain Rizvi 241736 Bscs-8a
Numerical Analysis Lab 6: Syed Muhammad Hussain Rizvi 241736 Bscs-8a
%Given Data
sprintf('<-----Part 1----->')
t=[0:8];
y=[40.12 66.78 80.17 86.71 80.77 66.78 44.41 10.51 -32.60];
% get polynomial coefficients (pc) of bets fit line
pc = polyfit(t,y,1); % 1 means linear fit
plot(t,y,'ro');
hold on;
plot(t,polyval(pc,t),'b-');
txt = sprintf('Best fit line y=%.2fx + %.2f -> Part-1',pc(1),pc(2));
%1. What is the slope and intercept? Hint: relate to polyfit coefficients
intercept=sprintf('y-intercept : %.2fx',pc(2))
gradient=sprintf('slope : %.2fx',pc(1))
%2. What is the value of y when t=4.5 and t=8.5 Hint: polyval
sprintf('<-----Part 2----->')
plot(4.5,polyval(pc,4.5),'bo');
plot(8.5,polyval(pc,8.5),'bo');
sprintf('Value a t= 4.5 : %.2f',polyval(pc,4.5))
sprintf('Value at t=8.5 : %.2f',polyval(pc,8.5))
interp1(t,y,4.5);
plot(4.5,interp1(t,y,4.5),'go');
plot(8.5,interp1(t,y,8.5),'go');
sprintf('Interpolation at t=8.5 : %.2f',interp1(t,y,4.5))
sprintf('Interpolation at t=8.5 : %.2f',interp1(t,y,8.5))
plot(x2,y2linear,'go');
legend('Data points -> Part-1',txt,'Value at t=4.5 -> Part-2','Value at t=8.5 -> Part-
2','Interpolation at t=4.5 -> Part-3');
1
Graph output:
Ans : This is because in part 2 we use polyval which estimates the value according to the line
draw by polyfit where and the interpl function estimates the values at given points using the
imaginary curve drawn with points as shown above