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:
Newton's Divided Difference Method

Subject: Newton's Divided Difference Method

From: Akmal

Date: 2 Mar, 2012 02:32:24

Message: 1 of 2

I'm writing a code for interpolation using Newton's method. The code works but it gave me a different value. When I enter f(x) = log(x), x0=1, it gave me y=1 which is wrong. Can anyone help me?

syms x
d = input('enter f(x)= \n')
f=inline(d);
z=diff(f(x));
f1=inline(z);
x0=input('inter x0 = \n')
x=x0
for u=0:inf
    y=x
    x=y-(f(x)/f1(x));
    if x==y
        break
    end
end

enter f(x)=
log(x)
 
d =
 
log(x)
 
inter x0 =
1

x0 =

     1


x =

     1

Warning: FOR loop index is too large. Truncating to 9223372036854775807.

y =

     1

>>

Subject: Newton's Divided Difference Method

From: Roger Stafford

Date: 2 Mar, 2012 03:45:17

Message: 2 of 2

"Akmal " <virtuoso_kraken@yahoo.com> wrote in message <jipbfo$4db$1@newscl01ah.mathworks.com>...
> I'm writing a code for interpolation using Newton's method. The code works but it gave me a different value. When I enter f(x) = log(x), x0=1, it gave me y=1 which is wrong. Can anyone help me?
> ......
> if x==y
> ......
> Warning: FOR loop index is too large. Truncating to 9223372036854775807.
- - - - - - - - -
  Why do you say that y = 1 is wrong? That is the solution to log(y) = 0 which is apparently what you were seeking.

  You shouldn't be requiring exact equality in your loop where you write "x==y". That is apparently what caused the warning message. The code never achieved exact equality. It probably eventually began bouncing back and forth between two different values and would have never stopped if it were not for the excessive size of 'u'. You should only require that your value becomes approximately zero to within some tolerance.

  Note: This is not interpolation. Newton's method allows you to solve an equation for an unknown value.

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