Math Help Forum

Math Help Forum Feed Site Feed

Go Back   Math Help Forum > Math Resources > Mathematics Software Discussion
Reply
 
Thread Tools Display Modes
  #1  
Old October 16th, 2009, 12:40 PM
Newbie
 
Join Date: Oct 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
ballin24 is on a distinguished road
Default mathlab- root finding

Hey im trying to find the secant method.
I am trying to modify my newton's code, but am not getting anywhere.
Here is the code I am using for newton's.

>> format long
>> c=1
c =
1
>> n=0;
>> x=1.5;
>> f=@(x) ((x)^4-c);
>> fder=@(x) (4*(x)^3);
>> while n<100
newx=x-(f(x)/fder(x));
n=n+1
x=newx
end

The secant formula is xn+1=xn-(xn-xn-1)*f(xn)/f(xn)-f(xn-1).
Thus I need two points. Can someone please write out a nice matlab code that I can use (with say two points being 1.5, and 1.3)?

thanks
Reply With Quote
Advertisement
 
  #2  
Old October 16th, 2009, 07:17 PM
Member
 
Join Date: Mar 2007
Posts: 104
Country:
Thanks: 17
Thanked 14 Times in 14 Posts
elbarto is on a distinguished road
Default

Maybe something like this would work for you:

Code:
function [x err iter]  = mhfSecant(f,x1,x2)
maxerr = 1e-6;
maxiter = 100;
iter = 0;
while abs(x1-x2) > maxerr && iter < maxiter
    xnew = x2-(x2-x1)/(f(x2)-f(x1))*f(x2);
    x1 = x2;
    x2 = xnew;
    iter = iter +1;
end
x = (x1+x2)/2;
err = f(x);
Code:
EDU>> f=@(x) ((x)^4-1);
EDU>> [x e i] = mhfSecant(f,1.5,1.3)

x =

    1.0000


e =

  6.9803e-009


i =

     7

EDU>>
Im not very familiar with this method so I cant guarantee this is the right solution.


Regards Elbarto
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -7. The time now is 12:24 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
©2005 - 2009 Math Help Forum


Math Help Forum is a community of maths forums with an emphasis on maths help in all levels of mathematics.
Register to post your math questions or just hang out and try some of our math games or visit the arcade.