Quote:
Originally Posted by ajmilano 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