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 February 11th, 2009, 03:43 PM
Newbie
 
Join Date: Feb 2009
Posts: 1
Country:
Thanks: 0
Thanked 0 Times in 0 Posts
ajmilano is on a distinguished road
Default Help with Newton's Method in Matlab

Hi, I really need some help setting up a matlab program to solve this problem.

Using MATLAB, calculate and plot the real root of function : f(x) = xtanh(x) + C = 0, for C=1.5, 2.5, 3.5

I understand the basics of the Newton Method, but keep getting errors when I run this code, and don't have enough of an understanding of whats going on to correct it. This is the code I have so far:

c=1.5
n=0;
x=.1;
f=x*tanh(x)-c;
fder=tanh(x)+(x*sech(x)^2);
while n<100
newx= x - (f(x)/fder(x));
n=n+1
x=newx
end
x

If someone could give me any tips or help I would really appreciate it. Thanks in advance for your help.
Reply With Quote
Advertisement
 
  #2  
Old February 11th, 2009, 11:35 PM
CaptainBlack's Avatar
Grand Panjandrum
 
Join Date: Nov 2005
Location: South of England
Posts: 12,260
Country:
Thanks: 776
Thanked 3,982 Times in 3,214 Posts
CaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond repute
Default

Quote:
Originally Posted by ajmilano View Post
Hi, I really need some help setting up a matlab program to solve this problem.

Using MATLAB, calculate and plot the real root of function : f(x) = xtanh(x) + C = 0, for C=1.5, 2.5, 3.5

I understand the basics of the Newton Method, but keep getting errors when I run this code, and don't have enough of an understanding of whats going on to correct it. This is the code I have so far:

c=1.5
n=0;
x=.1;
f=x*tanh(x)-c;
fder=tanh(x)+(x*sech(x)^2);
while n<100
newx= x - (f(x)/fder(x));
n=n+1
x=newx
end
x

If someone could give me any tips or help I would really appreciate it. Thanks in advance for your help.
Please in future give a description of the kind of error you are getting, and any error messages.

f and fder are not functions the way you have written this.

To make them anonymous functions try:

Code:
c=1.5
n=0;
x=.1;
f=@(x) (x*tanh(x)-c);
fder=@(x) (tanh(x)+(x*sech(x)^2));
while n<100
  newx= x - (f(x)/fder(x));
  n=n+1
  x=newx
end
x
Also the loop structure you are using is not ideal, as this should only need ~5 itterations to converge, you need to compare itterates and when the change is less than some threshold exit the loop.

I would also suggest you start further from 0 than 0.1, 1 should be better.

CB
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 02:37 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2010, 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.