Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Secant Function

Subject: Secant Function

From: Ryan

Date: 8 Oct, 2010 17:22:03

Message: 1 of 4

I am trying to program a secant function that will help me solve this function:

(1+p)/2*(p/(1-p+p^2))^21-0.5 = 0

My code:
function x = secant(f,x0,x1,n)
ff=inline('f');
xp=x0;
fp=f(xp);
x=x1;
fx=f(x);
for i=1:n
    xn = x-fx*(x-xp)/(fx-fp);
    xp=x;
    fp=fx;
    x=xn;
    fx=ff(x);
end

doesn't seem to work however. I have to determine to within 10^-3, the p that will help me solve this solution using initial p's p0=p1 and p1=0.9 and obtain p5=0.84230.

I'd really appreciate some help. Thanks in advance.

Subject: Secant Function

From: Roger Stafford

Date: 8 Oct, 2010 23:02:03

Message: 2 of 4

"Ryan " <riley127099@hotmail.com> wrote in message <i8njvr$mv0$1@fred.mathworks.com>...
> I am trying to program a secant function that will help me solve this function:
>
> (1+p)/2*(p/(1-p+p^2))^21-0.5 = 0
>
> My code:
> function x = secant(f,x0,x1,n)
> ff=inline('f');
> xp=x0;
> fp=f(xp);
> x=x1;
> fx=f(x);
> for i=1:n
> xn = x-fx*(x-xp)/(fx-fp);
> xp=x;
> fp=fx;
> x=xn;
> fx=ff(x);
> end
>
> doesn't seem to work however. I have to determine to within 10^-3, the p that will help me solve this solution using initial p's p0=p1 and p1=0.9 and obtain p5=0.84230.
>
> I'd really appreciate some help. Thanks in advance.
- - - - - - - - -
  In what way didn't it "seem to work"? Just what string did you pass to 'secant' for its first argument? Are you quite sure the lines

 fp=f(xp);
 fx=f(x); and
 fx=ff(x);

are each giving you what you expect?

Roger Stafford

Subject: Secant Function

From: Ryan

Date: 8 Oct, 2010 23:32:03

Message: 3 of 4

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i8o7tb$28p$1@fred.mathworks.com>...
> "Ryan " <riley127099@hotmail.com> wrote in message <i8njvr$mv0$1@fred.mathworks.com>...
> > I am trying to program a secant function that will help me solve this function:
> >
> > (1+p)/2*(p/(1-p+p^2))^21-0.5 = 0
> >
> > My code:
> > function x = secant(f,x0,x1,n)
> > ff=inline('f');
> > xp=x0;
> > fp=f(xp);
> > x=x1;
> > fx=f(x);
> > for i=1:n
> > xn = x-fx*(x-xp)/(fx-fp);
> > xp=x;
> > fp=fx;
> > x=xn;
> > fx=ff(x);
> > end
> >
> > doesn't seem to work however. I have to determine to within 10^-3, the p that will help me solve this solution using initial p's p0=p1 and p1=0.9 and obtain p5=0.84230.
> >
> > I'd really appreciate some help. Thanks in advance.
> - - - - - - - - -
> In what way didn't it "seem to work"? Just what string did you pass to 'secant' for its first argument? Are you quite sure the lines
>
> fp=f(xp);
> fx=f(x); and
> fx=ff(x);
>
> are each giving you what you expect?
>
> Roger Stafford

I used f=inline('((x^2-2*x+1)/(x2+1))^21*(x+1)/2')
and then matlab said there was an error.

Subject: Secant Function

From: Roger Stafford

Date: 9 Oct, 2010 00:08:03

Message: 4 of 4

"Ryan " <riley127099@hotmail.com> wrote in message <i8o9lj$m52$1@fred.mathworks.com>...
>
> I used f=inline('((x^2-2*x+1)/(x2+1))^21*(x+1)/2')
> and then matlab said there was an error.
- - - - - - - - -
  I can't really tell what you are doing. In one place you say f and in another place you say ff. My suggestion would be to temporarily write your function as a separate m-file function and see if the algorithm works with that. If it does, then concentrate on writing your inline function correctly. I think your trouble is in the inline function.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us