{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

HW4Solutions

# HW4Solutions - 12:20 PM F\Matlab Projects\HW4\NMI-IW4numl.m...

This preview shows pages 1–10. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 9/26/08 12:20 PM F:\Matlab Projects\HW4\NMI-IW4numl.m W“ %Homework Number 4 %Prob1em 1 %There are three ways (at least) to solve this problem. %You can use a for loop and put your condition in the for loop %or you can do it all in a while loop or you can do a combo of the two. i = O; f(1) = O; f(2) = 1; sumf = 1; %Initializing sum as one because f(1)+f(2)=l. for i = 3:20 f(i) = f(i-l) + f(i—z); sumf = sumf + f(i); if sumf > 5000 %You don't need to output sumf and i here. In %the case where the sum is less than 5000 for %all 20 elements, the if statement will never %be entered and it wouldn't output sumf and i. f %f will output as a vector %Or you can use just a while loop n = 2; %You start at 2 because you are declaring 1 and 2. sumfib = 1; fib(l) = O; fib(2) = 1; while sumfib < 5000 %You could have started at 3 but then you would put the increment %at the end of the while loop. So the while loop will solve then %increment. This way the while loop increments then solves. The %difference it would jump to 20. n = n+1; fib(n) = fib(n—l) + fib(n—Z); sumfib = sumfib + fib(n); end %We are just making the fibonacci sequence and nothing else. for m = 3:20 1 of 2 9/26/08 12:20 PM F:\Matlab Projects\HW4\NMHW4numl.m 2 of 2 “W fibs(m) = fibs(m—l) + fibs(m—2); end k = 0; %k starts at 0 because the sum starts at f(l). sumfibs = 0; while sumfibs<5000 k = k+1; %Increment BEFORE the sum or else k = 1 intially. sumfibs = sumfibs + f(k); %Calculates the sum for f(l) through f(k) %until 5000 is reached. If 5000 isn't %reached before 20, an error would probably %occur because f(21) isn't defined. end sumfibs 9/26/08 12:20 PM f: Columns 1 through 10 0 l 13 21 34 Columns 11 through 19 55 89 1597 2584 sumf = 6764 i: 19 sumfib = 6764 n: 19 sumfibs = 6764 k: 19 >> 144 MATLAB Command Window 233 377 610 1 of 1 8|! 987K 9/26/08 12:28 PM F:\Mat1ab Projects\HW4\airProps.m 1 of 2 function [rho,cp,mu,k] = airProps(T,p,units) R = 287; %If no arguments are given, T is 25 degrees C and P is atmospheric in Pa. if nargin < 1 T = 25 %Atmospheric conditions p = 1.01325E5 units = 'C Pa'; TK = T + 273.15; %Converts to Kelvin rho = p/(R*TK); [cp,mu,k] = CurveAnalysis(TK); %Subfunction call %If only 1 argument is given, P is assumed to be atmospheric and in Pa. elseif nargin < 2 %Note there is no T. That is because if there is 1 argument given, %it is T because of the way the function is defined: airProps(T,...). p = 1.01325E5 units = 'C Pa'; TK = T + 273.15; rho = p/(R*TK); [cp,mu,k] = CurveAnalysis(TK); %If a temp and pressure are inputed but no units, C and Pa are assumed. elseif nargin < 3 units = 'C Pa'; TK T + 273.15; rho = p/(R*TK); [cp,mu,k] = CurveAnalysis(TK); %If units are given, subfunction decides which units they are. elseif nargin == [TK,pNew] = StringCheck(T,p,units); %Subfunction call rho = pNew/(R*TK); [cp,mu,k] = CurveAnalysis(TK); end %Subfunction for outputting cp, mu, k function [cp,mu,k] = CurveAnalysis(TK) CurveFit = [—2.455322455E-7 2.156954157E-14 —2.486402486E-12; 6.701631702E—4 —5.332634033E-11 —2.871794872E—8; -2.992579643E-l 7.477905983E-8 9.629059829E—5; 1.042503030E3 2.527878788E—7 2.06060606lE-5]; if TK >= 100 & TK <= 600 ' %Checking the temperature range i = O; sumA = O; sumB = 0; sumC = O; for i = 1:4 cofp = CurveFit(i,1)*TK‘(4—i+1); sumA cofp + sumA; muofp = CurveFit(i,2)*TK‘(4—i+1); 9/26/08 12:28 PM F:\Matlab Projects\HW4\airProps.m 2 of 2 W sumB = muofp + sumB; kofp = CurveFit(i,3)*TK‘(4-i+1); sumC = kofp + sumc; end cp = sumA; mu = sumB; k = sumC; else error(sprintf('T = %f (K) is out of range for density curve fits', TK)) end %Subfunction for a string check on the units. function [TK,pNew] = StringCheck(T,p,units) if upper(units) == 'C' ‘ TK = T +273.15; pNew = p; %Keeps pressure in Pa elseif upper(units) = 'F' TK = (T—32)/l.8 + 273.15; pNew = p*6894; %Converts from psi to Pa else error(sprintf('units = "%s" not allowed in airProps’, units)) end 9/26/08 12:29 PM MATLAB Command Window 1 of 2 >> [rho,cp,mu,k] = airProps rho = 1.1841 cp = 3.0004e+005 mu 0.0055 7.7850 >> [rho,cp,mu,k] = airProps(100) rho = 0.9461 cp = 3.7740e+005 0.0082 k: 11.8750 >> [rho,cp,mu,k] = airProps(100,2E5) rho = 1.8675 Cp= 3.7740e+005 9/26/08 12:29 PM - MATLAB Command Window 2 of 2 mu 0.0082 11.8750 >> [rho,cp,mu,k] airProps(lOO,200,'F') rho = 15.4511 7 r x i i I x 1 1 1 GP: 3.13066+005 0.0059 8.4289 >> [rho,cp,mu,k] = airProps(100,200,'R') ??? Error using ==> airProps>StringCheck at 74 units = 'R' not allowed in airProps Error in ==> airProps at 32 [TK,pNew] = StringCheck(T,p,units); %Subfunction call >> [rho,cp,mu,k] = airProps(10000,200,'F') ??? Error using ==> airProps>CurveAnalysis at 62 T = 5810.927778 (K) is out of range for density curve fits ‘ Error in ==> airProps at 34 [cp,mu,k] = CurveAnalysis(TK); 9/26/08 12:26 PM F:\Matlab Projects\HW4\CorvRainData.m function CorvRainData %Homework 4 %Problem 3 from Chapter 3 Number 29 fid = fopen('CorvRain.dat'); linel = fgetl(fid); line2 = fgetl(fid); info = fscanf(fid, '%f'); 1 of 2 A = size(info); B = A(1,1)/13;' %Size(A) gives the length of info. Length also works. rainData = reshape(info, 13, B)‘; fclose(fid); Months = rainData(:,2:l3); %Removes the years %You can do this with for loops. If you do, you are calculating all the %columns so you need to write the for loops as for jrow 1:12 then for %irow = 1:1ength(info)/13. If you have jrow in the inside, you will sum the %rows. The inside for loop will continue until it is done which means irow %would stay 1 while jrow went from 1:12 (hence you would sum the rows not %columns. %Here is an alternative method. Matlab's default for the function sum() is %the columns. So when you have matrix A and you want to find the sum of its %columns, you can write sum(A) and it will print n values for n columns. %If you want to sum rows, you can write it as sum(A,2). The 2 means rows. %You can also increase this number for multidimensional arrays (3—D %etc...). %So this sums over the matrix Months which is my giant matrix for all 12 %months and 105 years. The division by 100 converts the total sum to inches %rather than hundredths of an inch. . monthTotal (sum(Months)/100); %Monthly totals fprintf('The total rainfall in January is %f inches\n',monthTotal(1,1)) fprintf('The total rainfall in February is %f inches\n',monthTota1(1,2)) fprintf('The total rainfall in March is %f inches\n',monthTotal(1,3)) fprintf('The total rainfall in April is %f inches\n',monthTotal(1,4)) fprintf('The total rainfall in May is %f inches\n',monthTotal(1,5)) fprintf('The total rainfall in June is %f inches\n',monthTota1(1,6)) fprintf('The total rainfall in July is %f inches\n',monthTotal(1,7)) fprintf('The total rainfall in August is %f inches\n',monthTotal(l,8)) fprintf('The total rainfall in September is %f inches\n',monthTotal(1,9)) fprintf('The total rainfall in October is %f inches\n',monthTota1(1,10)) fprintf('The total rainfall in November is %f inches\n',monthTota1(1,11)) fprintf('The total rainfall in December is %f inches\n',monthTotal(l,12)) fprintf('\n') %To find the average, take the totals for each month and divide it by the %number of years. monthAverage = (monthTotal/B); %Monthly averages fprintf('The average rainfall in January is %f inches/year\n',monthAverage(1,l)) fprintf('The average rainfall in February is %f inches/year\n',monthAverage(l,2)) 9/26/08 12:26 PM fprintf('The fprintf('The fprintf('The fprintf('The fprintf('The fprintf('The fprintf('The fprintf('The fprintf('The fprintf('The average average average average average average average average average average F:\Mat1ab Projects\HW4\CorvRainData.m rainfall rainfall rainfall rainfall rainfall rainfall rainfall rainfall rainfall rainfall in in in in in in in in in in March is %f inches/year\n',monthAverage(l,3)) April is %f inches/year\n',monthAverage(1,4)) May is %f inches/year\n',monthAverage(l,5)) June is %f inches/year\n',monthAverage(l,6)) July is %f inches/year\n',monthAverage(l,7)) August is %f inches/year\n',monthAverage(1,8)) September is %f inches/year\n',monthAverage(l,9)) October is %f inches/year\n',monthAverage(1,10)) November is %f inches/year\n',monthAverage(l,1l)) December is %f inches/year\n',monthAverage(l,12)) 2 of 2 "TM 9/26/08 12 : 27 PM The The The The The The The The The The The The The The The The The The The The The The The The >> total total total total total total total total total total total total average average average average average average average average average average average average rainfall rainfall rainfall rainfall rainfall rainfall rainfall rainfall rainfall rainfall rainfall rainfall in in in in in in in in in in in in rainfall rainfall rainfall rainfall rainfall rainfall rainfall rainfall rainfall rainfall rainfall rainfall MATLAB Command Window January is 681.880000 inches February is 532.590000 inches March is 448.660000 inches April is 263.290000 inches May is 197.970000 inches ' June is 128.400000 inches July is 40.010000 inches August is 56.480000 inches September is 153.590000 inches October is 327.660000 inches November is 671.980000 inches December is 726.930000 inches in January is 6.494095 inches/year in February is 5.072286 inches/year in March is 4.272952 inches/year in April is 2.507524 inches/year in May is 1.885429 inches/year in June is 1.222857 inches/year in July is 0.381048 inches/year in August is 0.537905 inches/year in September is 1.462762 inches/year in October is 3.120571 inches/year in November is 6.399810 inches/year in December is 6.923143 inches/year 1 of 1 ...
View Full Document

{[ snackBarMessage ]}

### Page1 / 10

HW4Solutions - 12:20 PM F\Matlab Projects\HW4\NMI-IW4numl.m...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online