Use Newtons method to find the solution for tanh(x)=x/3. This has two parts to it. Solve it using the "While" loop and with a "For" loop.
The Matlab program using while loop is as follows:
clc
clear
x0=1; % assuming initial value of x as 1
y=tanh(x0)-x0/3; %calculates y value at x0=1
dy=1-(tanh(x0))^2-1/3; % calculates dy value at x0=1
x1=x0-y/dy; % calculates next x value
while abs(x1-x0)>0.0001 %checking the error
x0 = x1; % now x1 becomes x0
y=tanh(x0)-x0/3;
dy=1-(tanh(x0))^2-1/3;
x1=x0-y/dy;
end
disp(x1)
The Matlab program using for loop is as follows:
clc
clear
n=99;
x = zeros(n+1,1);
x(1) = 1;
for k=1:100
y=tanh(x(k))-x(k)/3;
dy=1-(tanh(x(k)))^2-1/3;
x(k+1)=x(k)-y/dy;
end
disp(x)
In using for loop, I used 100 iterations and checked the result..I got the solution in the 4th iteration itself..if I did n't get the solution in 100 iterations, I would've increased the no.of iterations
Get Answers For Free
Most questions answered within 1 hours.