how to solve tan(x)-tanh(x)=0

8 views (last 30 days)
Semin Seo
Semin Seo on 1 Jun 2017
Commented: Star Strider on 1 Jun 2017
So I followed the exact directions on this book called Numerical Methods for Engineers and Scientists Using MATLAB and used this code:
>> fun=inline('tan(x)-tanh(x)','x');
>> x=linspace(-2,2,100);
>> I=find(sign(f(2:end))-sign(f(1:end-1)));
>> f=fun(x);
>> for n=1:length(I)
r(n)=fzero(fun, x({I(n) I(n)+1}));
end
but then, for some reasons, MATLAB told me that the 'subsindex' was not defined for the 'cells' value, and then when I proceeded anyways by typing r, it told me that r, for some reasons was not defined. What did I do wrong?

Accepted Answer

Star Strider
Star Strider on 1 Jun 2017
The code needs updating:
fun = @(x) tan(x)-tanh(x);
x=linspace(-2,2,100);
f=fun(x);
I=find(sign(f(2:end))-sign(f(1:end-1)));
for n=1:length(I)
r(n)=fzero(fun, x([I(n), I(n)+1]));
end
I replaced the inline call with an anonymous function, and created a variable range with ‘I(n)’. It now works.
  3 Comments
Semin Seo
Semin Seo on 1 Jun 2017
never mind, I fixed that problem by setting the parameters just a bit wider
Star Strider
Star Strider on 1 Jun 2017
My pleasure!
You chose the correct approach with respect to your solution.

Sign in to comment.

More Answers (1)

Honglei Chen
Honglei Chen on 1 Jun 2017
Edited: Honglei Chen on 1 Jun 2017
Try
fzero(@(x)tan(x)-tanh(x),-2)
This uses anonymous function instead.
HTH

Categories

Find more on 수학 in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!