Can't figure out what's wrong with my code

조회 수: 2 (최근 30일)
Kevin Krause
Kevin Krause 2015년 11월 1일
댓글: Walter Roberson 2020년 9월 18일
One of the main problems is that there will never be a sign change on the interval so it'll always execute the first part of the if statement. I commented out the err>errtol part for testing. I don't know what i'm doing wrong for calculating using the bisection method. I have to use while loops.
% A scale is made with two springs, and when an object is attached to the
% two springs, the springs stretch and the ring at the end of the two
% springs is displaced downwards a distance x.
% The relationship between the weight of the object and the distance x is:
% W=(2k/l)(l-lo)(b+x)
% The initial spring length
% lo = sqrt(a^2+b^2)
% The stretched spring length
% l = sqrt(a^2+(b+x)^2)
% for the given scale a=8.0 in. b=5.0 m. and the spring constant k=16.0 lb/in.
% b=5.0 m.=196.85 in.
% Trying to find the deflection x.
% Have the user input lower and upper values of the starting
% interval (xl, xu), as well as the weight of the object
% Initialize the error tolerance (errtol)
% and the maximum number of iterations (maxiter)
% Check the interval to see if there's a sign change
% while (iterations < maxiter) and (err > errtol)
% Calculate midpoint of the interval xr=(xl+xu)/2
% Calculate error estimate (err)
% Evaluate the function at the midpoint f(xr)
% If the sign of f(xr) is equal to the sign of f(xl)
% if f(xr)*f(xu)>0
% then xr becomes the new xl.
% else xr becomes the new xu.
% end while loop
% If err <= errtol
% then solution found, output result
% else display ‘root not found within tolerance
W = input('\nWhat is the weight of the object attached to the ring in pounds (lb)? ');
xl = input('\nWhat is the lower initial bracket estimate in inches? '); %always 0
xu = input('\nWhat is the upper initial bracket estimate in inches? '); %always 10
a = 8.0;
b = 5.0 ;
lo = sqrt(a^2+b^2);
k = 16.0;
errtol = 0.005;
xr = (xl+xu)/2;
maxiter = 10;
iterations = 0;
f = @(x) (2*k/(sqrt(a^2+(b+x)^2)))*((sqrt(a^2+(b+x)^2))-lo)*(b+x);
err = abs((xu-xl)/xu);
xrold = abs((xu-xl)/xu);
while (iterations<maxiter) %|| (err>errtol)
err = abs((xr-xrold)/xr)*100;
if f(xr)*f(xl)>0
xu = xr;
xl = xr;
xrold = xr;
  댓글 수: 4
saja mk
saja mk 2020년 9월 18일
where is the vector x ?
Walter Roberson
Walter Roberson 2020년 9월 18일
The vector x is right here:
f = @(x) (2*k/(sqrt(a^2+(b+x)^2)))*((sqrt(a^2+(b+x)^2))-lo)*(b+x);
This syntax means that f is to become the handle to an anonmous function, and that when the function body (given there) is executed, that the value passed in as the first parameter is to be known as x in the execution of the body.

댓글을 달려면 로그인하십시오.

답변 (1개)

Mischa Kim
Mischa Kim 2017년 11월 6일
편집: Mischa Kim 2017년 11월 6일
Kevin, I see two problems with your algorithm/function f(x):
if f(xr)*f(xl)>0
needs to be
if f(xr)*f(xl)<0
and the function f(x) does not (I believe) have any roots for positive x and for the set of parameters you chose. In other words, the starting xl needs to be negative for the algorithm to converge to a root. According to your comments, xl is always 0.


Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by