Matlab Question: Given code how would you answer part below? syms x; % Constants
ID: 3912652 • Letter: M
Question
Matlab Question: Given code how would you answer part below?
syms x;
% Constants
a=10;
b=7;
k=20;
% User input
w = input('Enter Weight: ');
xl = input('Enter bracket xl: ');
xu = input('Enter bracket xu: ');
% Calcuations
l0 = sqrt(a^2 + b^2);
l = sqrt(a^2 + (b+x)^2); % Length as a function of x
W = 2*k*(l-l0)*(b+x)/l; % Relating W value to x for the main equation
% Default initialization
xr_old = xl;
xr_new = xu;
% Iteration counter
iter=1;
fprintf(' Solution By: Name');
fprintf(' Date : Date');
fprintf(' W = %f lb',w);
fprintf(' Iteration Xl(in) Xu(in) Xr(in) error(%c)','%');
fprintf(' -------------------------------------------------------------------------------------');
while(1)
xr_new = (xl + xu)/2; % Bisecting the interval
err = abs((xr_new-xr_old)/xr_new); % Calculating error with respect to old root
fprintf(' %d %f %f %f %f',iter,xl,xu,xr_new,err*100);
if(err<0.005) % Checking relative error percentage
solution = xr_new;
break;
else
if(eval(subs(W,xr_new))*eval(subs(W,xl))<0) % Checking if bisection point has differen sign that the upper/lower brackets
xu = xr_new;
else
xl = xr_new;
end
xr_old = xr_new; % Assigning earlier calculated root to become old root
iter=iter+1; % Increasing iteration count
end
end
fprintf(' -------------------------------------------------------------------------------------');
fprintf(' Solution: x = %f (in) ',solution);
Explanation / Answer
I think your given parameters or your code is not correct .
Irrespective of weights the program is outputting a result 7.50000 and error is 100%
As of it I can't make a correct answer.But,I have a method to solve this problem.
This solution has two steps:
First step is easy correct your code and run it with different weight values.
Second step:
->From your code a=10,b=7,k=20,xl=0,xu=15 these are constant in our scenario
->l0=sqrt(a^2 +b^2) which is a constant since a and b does'nt change
so, l0=12.20656(approx.)
-> l=sqrt(a^2 + (b+x)^2 ) which is Lenght function in term of x (this x is the value we obtained after the program is executed).
-> W= 2*k*(l-l0)*(b+x)/l
this is the weight which we should find using the x(solution) obtained earlier while executing
for example
if w=5, x =7.5
then l=sqrt(a^2 + (b+x)^2 )
will be l=sqrt(a^2 + (b+7.5)^2 ) ,after substituting a =10 b=7
we get l=sqrt(10^2 +(14.5)^2) which is approximately 17.61395
Now we need to find W
W= 2*k*(l-l0)*(b+x)/l (in here we substitute l= 17.61395 b=7 k=20 x=7.5 l0=12.20656)
after substitution we get W=178.0569 lb
similarly we can find W for different w values.
Hope I helped!