You are on page 1of 14

Name: insert name here MECH 2220 Lecture HW 05 Linear Least Squares

Lecture HW 05

Linear Regression and Linear Interpolation


February 19, 2013 February 26, 2013 (11:00 a.m.)

Assigned: Due:

Name: insert name here MECH 2220 Lecture HW 05 Linear Least Squares

Problem Statement #1:


Write a user written function, called linear_least_square, that returns the coefficients by performing a least-squares fit to a straight line. The first coefficient should be the slope of the linear and the second coefficient should be the y-intercept. Do not use any built in Matlab functions including sum and polyfit. Use the user written function to solve problem 13.20 in your text. Be sure to plot the data and resulting fits.

Code for problem #1:

function [slope,int] = linear_least_square(x,y) %y=a1x+a0 n=length(x); sumxi=0; i=1; while i<=n sumxi=x(i)+sumxi; i=i+1; end sumxiyi=0; i=1; while i<=n sumxiyi=x(i)*y(i) + sumxiyi; i=i+1; end sumxisqd=0; i=1; while i<=n sumxisqd=x(i)^2 + sumxisqd; i=i+1; end sumyi=0;

Name: insert name here MECH 2220 Lecture HW 05 Linear Least Squares

i=1; while i<=n sumyi=y(i) + sumyi; i=i+1; end slope=(n*sumxiyi-sumxi*sumyi)/ (n*sumxisqd-sumxi^2); ybar=sumyi/n; xbar=sumxi/n; int=ybar-slope*xbar; end %Problem 1 t=[10,15,20,25,40,50,55,60,75];%x TS=[5,20,18,40,33,54,70,60,78]; %Basically,y [slope,int]=linear_least_square(t,TS); %TS_pred=slope*t+int; t_plot=0:10:100; TS_pred=int+t_plot*slope; TS_t32=int+32*slope; fprintf('Estimated tensile str. at time 32 is %2.3f\n',TS_t32); plot(t,TS,'b^',t_plot,TS_pred,'g--',32, TS_t32,'*k') xlabel('Time for Heat Treating[[min]]') ylabel('Tensile Strength') legend('Original Data','Linear Regression','Str. at 32min')

Name: insert name here MECH 2220 Lecture HW 05 Linear Least Squares

Solution for problem #1:


120 Original Data Linear Regression Str. at 32min

100

80 Tensile Strength

60

40

20

10

20

30 40 50 60 70 Time for Heat Treating[[min]]

80

90

100

Estimated tensile str. at time 32 is 34.705

Problem Statement #2:


Under and Over 7 is a simple dice casino game played with two dice. The players may bet that the sum of the dice rolled will be over 7, exactly 7, or under 7. For winning bets, the over and under bets pay $1 for each dollar bet. For winning bets, the exactly 7 bet pays back 5 dollars for each 1 dollar bet. Write a user written function that will simulate the roll of arbitrary number of dice. The user written function must expect an integer representing the number of dice to be rolled. The user written function must return a vector containing the number rolled for each die. Write a script that simulates the game and uses a regression to compute the average rate of return the house should expect (dollars gained per dollar bet). To perform the simulation assume that the house starts off with $10,000 and consider three cases: bets are only placed on under, bets are only placed on exactly 7, bets are only placed on over.

Code for problem #2:

Name: insert name here MECH 2220 Lecture HW 05 Linear Least Squares

%Problem 2 maxbets=10000; %Simulate player bets under 7. house(1)=10000; rollvector=rollDice(maxbets); i=1; while i<=maxbets if rollvector(i)<7 house(i+1)=house(i)-1; else house(i+1)=house(i)+1; end i=i+1; end betnumber=0:maxbets; [slope,int]=linear_least_square(betnumb er,house); plotlineys=slope*[0,maxbets]+int; %slope*betnumber+int for fast computer figure(1) plot([0,maxbets],plotlineys,'m--',betnu mber,house,'c') xlabel('Bet number') ylabel('House value,$') title('Player bet under 7') legend('Predicted Return over Time','Actual Return') fprintf('Average rate of return is %1.3f[[$/bet]]\n',slope) %Simulate player bets over 7. house(1)=10000;

Name: insert name here MECH 2220 Lecture HW 05 Linear Least Squares

rollvector=rollDice(maxbets); i=1; while i<=maxbets if rollvector(i)>7 house(i+1)=house(i)-1; else house(i+1)=house(i)+1; end i=i+1; end betnumber=0:maxbets; [slope,int]=linear_least_square(betnumb er,house); plotlineys=slope*[0,maxbets]+int; %slope*betnumber+int for fast computer figure(2) plot([0,maxbets],plotlineys,'m--',betnu mber,house,'c') xlabel('Bet number') ylabel('House value,$') title('Player bet over 7') legend('Predicted Return over Time','Actual Return') fprintf('Average rate of return is %1.3f[[$/bet]]\n',slope) %Simulate player bets equal 7. house(1)=10000; rollvector=rollDice(maxbets); i=1; while i<=maxbets if rollvector(i)==7

Name: insert name here MECH 2220 Lecture HW 05 Linear Least Squares

house(i+1)=house(i)-5; else house(i+1)=house(i)+1; end i=i+1; end betnumber=0:maxbets; [slope,int]=linear_least_square(betnumb er,house); plotlineys=slope*[0,maxbets]+int; %slope*betnumber+int for fast computer figure(3) plot([0,maxbets],plotlineys,'m--',betnu mber,house,'c') xlabel('Bet number') ylabel('House value,$') title('Player bet exactly 7') legend('Predicted Return over Time','Actual Return') fprintf('Average rate of return is %1.3f[[$/bet]]\n',slope)

function out_vector = rollDice(rolls) i=1; out_vector=[]; while i<=rolls out_vector(i)=ceil(random('uniform',0,6 ))+ceil(random('uniform',0,6)); i=i+1; end

Name: insert name here MECH 2220 Lecture HW 05 Linear Least Squares Solution for problem #2:

Name: insert name here MECH 2220 Lecture HW 05 Linear Least Squares
1.18 1.16 1.14 1.12 House value,$ 1.1 1.08 1.06 1.04 1.02 1 x 10
4

Player bet over 7 Predicted Return over Time Actual Return

1000

2000

3000

4000 5000 6000 Bet number Player bet under 7

7000

8000

9000 10000

1.18 1.16 1.14 1.12 House value,$ 1.1 1.08 1.06 1.04 1.02 1 0.98

x 10

Predicted Return over Time Actual Return

1000

2000

3000

4000 5000 6000 Bet number

7000

8000

9000 10000

Name: insert name here MECH 2220 Lecture HW 05 Linear Least Squares
Player bet exactly 7 10100 10050 10000 9950 House value,$ 9900 9850 9800 9750 9700 9650 9600 Predicted Return over Time Actual Return

1000

2000

3000

4000 5000 6000 Bet number

7000

8000

9000 10000

Average rate of return is 0.169[[$/bet]] Average rate of return is 0.162[[$/bet]] Average rate of return is -0.040[[$/bet]]

Name: insert name here MECH 2220 Lecture HW 05 Linear Least Squares

Problem Statement #3:


Do problem 13.29 in your text (second edition). Fit the data to the model p=C1*exp(C2*t). a. Apply a transformation to the data and utilize a linear regression to estimate C1 and C2. b. Create a table, graph and exponential fit in Microsoft Excel. Compare the results. c. Without transforming the data use fminsearch to estimate C1 and C2. d. Plot the data and the two fits on the same graph. Be sure to use a legend to identify the fits.

Code for problem #3:

global t p t=0:5:20; p=[100,200,450,950,2000]; [slope,int]=linear_least_square(t,log(p )); t_plot=0:.5:25; C1=exp(int); C2=slope; p_pred=C1*exp(C2*t_plot); plot(t,p,'^r',t_plot,p_pred,'--g') fprintf('Regression equation by transform is %2.4f*exp(%2.4f*t_plot)\n',C1,C2) %FMINSEARCH TIME

Name: insert name here MECH 2220 Lecture HW 05 Linear Least Squares

myresidualfunction = @myresids; coefficient=fminsearch(myresidualfuncti on,[95,.14]); p_pred_fmins=coefficient(1)*exp(coeffic ient(2)*t_plot); hold on plot(t_plot,p_pred_fmins) legend('Given Data','Transformed Regression','Fminsearch') xlabel('Years') ylabel('Population') fprintf('Regression equation by fminsearch is %2.4f*exp(%2.4f*t_plot)\n',coefficient( 1),coefficient(2)) function rms_resid = myresids(c_vector) global t p rms_resid = sqrt(sum((pc_vector(1)*exp(c_vector(2)*t)).^2)); end
Solution for problem #3:
Regression equation is 97.9148*exp(0.1510*t_plot)

Name: insert name here MECH 2220 Lecture HW 05 Linear Least Squares
4500 4000 3500 3000 2500 2000 1500 1000 500 0

10

15

20

25

Name: insert name here MECH 2220 Lecture HW 05 Linear Least Squares
4500 4000 3500 3000 Population 2500 2000 1500 1000 500 0 Given Data Transformed Regression Fminsearch

10 Years

15

20

25

You might also like