take home
matlab
posted: Apr, 27th 2012 | jump to bottom
clc; clear; %Initializing variables to be used later answer = 500; error = 100; %Opens File named VLEdata.txt and stores data from the file fid = fopen('VLEdata.txt','r'); headers = textscan(fid,'%s',3,'delimiter', '\n'); headers{1,1}(1); headers{1,1}(2); headers{1,1}(3); data = textscan(fid,'%f %f %f','delimiter','\n'); X=data{1,1}(:,1); Y=data{1,2}(:,1); T=data{1,3}(:,1); %Makes a matrix Z with the deminsions of X sizeXYT=size(X); rowsXYT=sizeXYT(1,1); columnsXYT=sizeXYT(1,2); Z=zeros(rowsXYT,columnsXYT); %Divides matrix X and Y by 100 to get them in Mole Fractions X=X./100; Y=Y./100; %for loop from 1 to rowsXYT that makes each index in Z equal to its %corresponding indexes in Y and X to Y - X for k=1:1:rowsXYT Z(k,1)=Y(k,1)-X(k,1); end %for loop from 1 to rowsXYT that takes the inverse of all Z indexes which %are not equal to 0. Done to avoid divison by 0 for i.html">i=1:1:rowsXYT %If the Z index is not equal to 0 then proceed if Z(i.html">i,1)~=0 %take the inverse of the Z index Z(i.html">i,1)=Z(i.html">i,1).^(-1); end end %Makes a matrix A which shows the indexes in Z which are nonzero A=find(Z); %Finds the size of Matrix A D=size(A); %Sets variable l equal to the number of columns in Matrix A l=D(1,1); %Creates a zero matrix of the same size as Matrix A M = zeros(l,1); N = zeros(l,1); %for loop from 1 to the number of columns of matrix A for k=1:1:l %Assigns num to the number contained in the given index in Matrix A num=A(k,1); %Uses num to assign matrix M the non-zero numbers from Z M(k,1)=Z(num,1); %Uses num to assign corresponding X values to N N(k,1)=X(num,1); end %Uses polyfit create a polynomial regression of N and M (non-zero Z values %and their corresponding X values p=polyfit(N,M,10); %Plots the given data and the polynomial regression t2=0:.01:1; y2=polyval(p,t2); plot(N,M,'o',t2,y2) hold on; title(headers{1,1}(1)) xlabel('x') ylabel('1/Y-X') grid() %Sets the variable r equal to Xo r=.8; %Sets the function F equal to the polynomial regression found above F=p(1,1)*r^10+p(1,2)*r^9+p(1,3)*r^8+p(1,4)*r^7+p(1,5)*r^6+p(1,6)*r^5+p(1,7)*r^4+p(1,8)*r^3+p(1,9)*r^2+p(1,10)*r+p(1,11); %Sets the variable Finitial equal to the value of F at Xo Finitial=F; %Sets the variable LHS equal to the left hand side of the equation %log(n/n0) LHS = log(25/50); %While loop that will excecute so long as error is greater than or equal to .0001 while error >= .0001 %Decreases the value of r by .0001 per iteration from its starting value of %Xo r=r-.0001; %Sets F equal to the value of the polynomial regression function at the new %r value F=p(1,1)*r^10+p(1,2)*r^9+p(1,3)*r^8+p(1,4)*r^7+p(1,5)*r^6+p(1,6)*r^5+p(1,7)*r^4+p(1,8)*r^3+p(1,9)*r^2+p(1,10)*r+p(1,11); %Uses trapezodial approximation to find the area under the curve RHS = ((r-.8)*((Finitial)+(F)))/2; %Calculates and error between the area under the curve and the Left Hand Side of the %equation error = abs(RHS - LHS); end %Caluclates the Y value at the found X value using 1/(Y-X) = F(X) Yvalue = (1/F)+r; %fprintf to print out the compositions of liquid and vapor phases in the %still when the remaining quanitity in the still is 25 moles fprintf('LHS: %.4f\nRHS: %.4f\nError: %.4f\nLiquid Phase Quantity:%.4f\nVapor Phase Quantity: %.4f\n\n',LHS,RHS,error,r,Yvalue);
101 views




