Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Does anyone know how to do any of these programs?
Date: Sun, 30 Nov 2008 02:24:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 93
Message-ID: <ggstg1$nae$1@fred.mathworks.com>
References: <8659463.1227927779634.JavaMail.jakarta@nitrogen.mathforum.org> <ggqdus$bu7$1@fred.mathworks.com> <ggs6oh$hrq$1@fred.mathworks.com> <ggsovl$svb$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1228011841 23886 172.30.248.35 (30 Nov 2008 02:24:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 30 Nov 2008 02:24:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1621609
Xref: news.mathworks.com comp.soft-sys.matlab:503861


"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <ggsovl$svb$1@fred.mathworks.com>...
> "john " <john@yahoo.com> wrote in message <ggs6oh$hrq$1@fred.mathworks.com>...
> > .........
> > Ok, thank you for informing me, that is no problem. I am new to matlab and a lot of the simple things that i want to do get me stuck, and i get frustrated since then it doesnt even matter if i know what to do, I cant do it cause i am not familiar with the process and the language. 
> > For the first problem: Implement the bisection method in a computer code, and compute the roots of the quadratic equation
> > x2 &#8722; 2 x + 0.9 = 0. Prepare and discuss a graph of the error against the iteration count, k.
> > 
> > I have written the following the code:
> > clc; clear all; close all;
> > y(x)=(x^2)-(2*x)+(0.9);
> > X1=2;
> > X2=-2;
> > Xm=(0.5)*(y(X1)+y(X2));
> > d=X2-X1;
> > Xnewm=Xm+(0.5*d);
> > disp(Xnewm);
> > 
> > result:
> > ??? Undefined function or variable 'x'.
> > 
> > Error in ==> hw5p1 at 2
> > y(x)=(x^2)-(2*x)+(0.9);
> > 
> > this is probably a stupid question but how do i plug in X1 and X2 into the y(x) equation, what format do i have to use for that? 
> > thanks you
> -----------
>   Since you have attempted some work on it, I will help you to the following extent on problem 4.3.1.
> 
>   In the bisection method you want to make sure you start out with two values of x that produce opposite signs for the corresponding values of y.  Your x1 = 2 and x2 = -2 constitute an unfortunate choice for that since they both give positive values for y.  Make a better selection.  In fact notice that there are two solutions to the equation, so some pairs of x1, x2 should surround one of these and other pairs should be grouped around the other root.
> 
>   You first want to start with a pair of x1 and x2 for which you have computed the corresponding values of y as y1 and y2 and these must be of opposite sign.  Then enter into a 'while' loop inside of which you would do something like the following:
> 
>  xm = (x1+x2)/2; % Find the midpoint between x1 and x2
>  ym = xm^2-2*xm+.9; % Compute the corresponding y value
>  if sign(ym) ~= sign(y1) % Ask if ym and y1 are of opposite signs
>   x2 = xm; % If so, alter x2 and y2 to the new values, xm and ym
>   y2 = ym;
>  else % Otherwise ym and y2 must be of opposite signs
>   x1 = xm; % So alter x1 and y1 to the new values
>   y1 = ym;
>  end
> % No matter which alternative was selected, the new y1 and y2 
> % will still be of opposite sign
>  err = abs(y2-y1); % Check the new difference in y values
> 
> At this point you now have the desired root pinned between the new values of x1 and x2 which are half as far apart as in the last trip through the 'while' loop.  Make the 'while' loop quit whenever the error (err) becomes acceptably small.
> 
>   You will have to augment this stuff by gathering the iteration count and error in some array for later graphing to satisfy the requirements of the problem.
> 
>   If this conditional looping process still puzzles you, make a point of carefully studying how the 'while' command works.  It is absolutely fundamental to the operation of many, many Matlab programs.
> 
> Roger Stafford
Thanks a lot for your response and help. I wrote the program before and then changed it based on your advice but somehow i can not get an answer from it.
Original version:
clc; clear all; close all;
y=inline('(x^2)-(2*x)+(0.9)');

X1=1;
X2=-2;
Xm=(0.5)*(X1+X2);
d=X2-X1;
Xnewm=X1+(0.5*d);

if y(X1)+y(Xm)<y(X1)
    X2=Xm;
else
    if y(X2)+y(Xm)<y(X2)
        X1=Xm;
    elseif y(Xm)==0
        disp(Xm);
    end
end
Changed version:
clc; clear all; close all;
y=inline('(x^2)-(2*x)+(0.9)');

X1=1;
X2=-2;
Xm=(0.5)*(X1+X2);
d=X2-X1;
Xnewm=X1+(0.5*d);
if sign(y(X1)) ~= sign((Xm))
    X2=Xm;
else
    if sign(y(X2)) ~= sign((Xm))
        X1=Xm;
    elseif y(Xm)==0
        disp(Xm);
    end
end

when i run it, it does not display Xm at the end.