Beruflich Dokumente
Kultur Dokumente
Lecture HW 05
Assigned: Due:
Name: insert name here MECH 2220 Lecture HW 05 Linear Least Squares
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
100
80 Tensile Strength
60
40
20
10
20
80
90
100
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
1000
2000
3000
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
1000
2000
3000
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
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
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