Thursday, December 10, 2015

matlab homework 14

Contents

Problem #1 - Interpolation

clear,clc

V = [1 2 3 4 5 6];
P = [2494 1247 831 623 499 416];

Linear_P = interp1(V,P,3.8)
Spline_P = interp1(V,P,3.8,'spline')
Linear_V = interp1(P,V,1000)
Linear_V = interp1(P,V,1000,'spline')
Linear_P =

  664.6000


Spline_P =

  657.4373


Linear_V =

    2.5938


Linear_V =

    2.4779

Problem #2 - Curve Fitting

clear,clc
V = [1 2 3 4 5 6];
P = [2494 1247 831 623 499 416];

%polyfit gives you the coefficients.
%polyval plugs in values for 'x' and solves for each.
Y1 = polyval(polyfit(V,P,1),V);
Y2 = polyval(polyfit(V,P,2),V);
Y3 = polyval(polyfit(V,P,3),V);
Y4 = polyval(polyfit(V,P,4),V);
plot(Y1)
hold on
plot(Y2)
plot(Y3)
plot(Y4)
plot(V,P,'o')
new_V = 1:0.2:6; %higher data precision for V
int_P = interp1(V,P,new_V); %interpolate in order to get values in between V's
plot(new_V,int_P,'g')
legend('1st Order','2nd Order','3rd Order','4th Order','Actual','Interpolated')
title('Pressure vs. Volume')
xlabel('Volume (m^3)')
ylabel('Pressure (kPa)')
% 4th order polynomial seems to do the best job of fitting the data

Problem #3 - Curve Fitting II

clear,clc

V = [1 2 3 4 5 6];
P = [2494 1247 831 623 499 416];

Y = polyfit(1./V,P,1)
T = Y(1)/8.314

new_V = 1./V;
plot(new_V,P)
title('Pressure vs. Inverse Volume')
xlabel('1/V (m^-3)')
ylabel('Pressure (kPa)')
Y =

   1.0e+03 *

    2.4940   -0.0000


T =

  299.9715



No comments:

Post a Comment