Вы находитесь на странице: 1из 6

1.

For the function:


function f = ladder(tetha,alpha) w1=2; %m w2=2; %m f = (w1/sin(tetha))+(w2/sin(pi-alpha-tetha));

For the optimization:


clear;clc;clf alpha=((45*pi)/180):(pi/90):((135*pi)/180); %angles are in radians ladderlength=zeros(1,numel(alpha)); %creating a matrix for the max length of the ladder at diff alpha values for runs=1:numel(alpha) [minangle(runs) , ladderlength(runs)] = fminsearch(@(tetha) ladder(tetha,alpha(runs)), pi/6); % The anonymous function is used in this case. The optimum tetha is found % for each alpha. After experimenting with different initial guesses, pi/6 % was chosen. end alphadeg=(alpha*180)/pi; %to convert alpha to degree mode plot(alphadeg,ladderlength, 'bo-'); xlabel('alpha (degrees)'); ylabel('Max length of ladder (m)'); title('A graph of max length of ladder versus alpha') set(gca, 'XTick', [45:10:135]);

We see that as alpha increases, the maximum length of the ladder that is able to negotiate the corner increases too. This makes sense because as alpha approaches 180 degrees, the hallway becomes straight, and will be able to accommodate longer lengths of ladders.

2. a) Objective function
function F = lab08a(k) % This function lab08a takes in a vector k =[k1 k2 k3], where k is a 1 by 3 matrix % containing the values of k1, k2 and k3. It will calculate the model y % values using the input k. Following which, it will calculate the sum of % square deviation F as an output of this function. if ~(size(k)==[1 3]) error('Wrong input matrix dimensions') % this is to prevent any input of data that does not have the correct % dimensions end %% Loading experimental data load lab08data %% Calculating model y values for n=1:numel(x) ymodel(n)=(k(1)*exp(k(2)*x(n)))+k(3); % calculating model y values using input k's and equation(1) for all % tabulated values of x end %% Calculating sum of square deviation F F=0; for m=1:numel(x) F=F+(y(m)-ymodel(m))^2; end

b) Optimum values: k1=5.0000, k2=-0.2000 and k3=7.0000


clc;clear;clf %% Finding optimum values for k1, k2 and k3 [koptimum]=fminsearch(@lab08a,[1 1 1]); % Initial guess is 1 for all k. k1opt, koptimum contains the % optimum k1, k2 and k3 values. fprintf('Optimum values of k: k1=%.4f k2=%.4f k3=%.4f\n', koptimum); %% Creating a list of ymodel values based on optimum k load lab08data for n=1:numel(x) ymodel(n)=(koptimum(1)*exp(koptimum(2)*x(n)))+koptimum(3); % calculating model y values using optimum k's and equation(1) for all % tabulated values of x end %% Plotting data and model values plot(x,ymodel,'b-',x,y,'r^');

xlabel('x'); ylabel('y'); title('A graph of y versus x, with optimum values of k1, k2 and k3'); legend('Model values','Experimental values');

c) Objective function
function F = lab08c(k) % This function lab08a takes in a vector k =[k1 k2 k3], where k is a 1 by 3 matrix % containing the values of k1, k2 and k3. It will calculate the model y % values using the input k. Following which, it will calculate the sum of % square deviation F as an output of this function. if ~(size(k)==[1 3]) error('Wrong input matrix dimensions') % this is to prevent any input of data that does not have the correct % dimensions end %% Loading experimental data load lab08data %% Calculating model y values for n=1:numel(x) ymodel(n)=(k(1)*x(n)^2+k(2)*x(n)+k(3));

% calculating model y values using input k's and equation(2) for all % tabulated values of x end %% Calculating sum of square deviation F F=0; for m=1:numel(x) F=F+(y(m)-ymodel(m))^2; End

Optimum values: k1=0.0400, k2=-0.8129 and k3=11.8961


clc;clear;clf %% Finding optimum values for k1, k2 and k3 [koptimum]=fminsearch(@lab08c,[1 1 1]); % Initial guess is 1 for all k. k1opt, koptimum contains the % optimum k1, k2 and k3 values. fprintf('Optimum values of k: k1=%.4f k2=%.4f k3=%.4f\n', koptimum); %% Creating a list of ymodel values based on optimum k load lab08data for n=1:numel(x) ymodel(n)=((koptimum(1)*x(n)^2)+(koptimum(2)*x(n))+koptimum(3)); % calculating model y values using optimum k's and equation(1) for all % tabulated values of x end %% Plotting data and model values plot(x,ymodel,'b-',x,y,'r^'); xlabel('x'); ylabel('y'); title('A graph of y versus x, with optimum values of k1, k2 and k3'); legend('Model values','Experimental values');

d) Using polyfit(x,y,2): k1=0.0400, k2=-0.8129 and k3=11.8961 These values obtained from polyfit agree with the values from 2(C). Using the polyval function: polyval([0.0400 -0.8129 11.8961], 4.3) = 9.1392

Вам также может понравиться