how to solve tan(x)-tanh(x)=0
8 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
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
Star Strider
on 1 Jun 2017
My pleasure!
You chose the correct approach with respect to your solution.
More Answers (1)
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
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!