calculation error in hyperbolic function

2 views (last 30 days)
syms x
syms t
U=zeros(1,2,'sym');
A=zeros(1,2,'sym');
B=zeros(1,2,'sym');
C=zeros(1,2,'sym');
series(x,t)=sym(zeros(1,1));
U(1)=(tanh(x))^2
for k=1:3
A(1)=0;
B(1)=0;
C(1)=0;
for i=1:k
A(1)=simplify(A(1)+diff( U(i),x,1)*diff(U(k-i+1),x,1));
B(1)=simplify(simplify(B(1)+U(i)*diff(U(k-i+1),x,2)));
for j=1:i
C(1)=simplify(C(1)+U(j)*U(i-j+1)*diff(U(k-i+1),x,1));
end
end
U(k+1)=(-2*C(1)+(1/2)*B(1)+A(1))/k;
end
for k=1
series(x,t)=(series(x,t)+U(k)*power(t,k-1));
end
series
row=0
C2=zeros(1);
for r=1
row=row+1;
col=0;
e=(r-11)
for t=1
col=col+1;
f=((t)/1000);
C2(row,col)=series(e,f);
end
end
For k=1 the solution is (tanh(x))^2 and using the loop if i evaluate the value for x=-10 the answer is 0.999999999 .But if directly put the value of x=-10 in (tanh(x))^2 the value is -1 .Why this difference appear?

Answers (1)

David Goodmanson
David Goodmanson on 23 Nov 2022
Edited: David Goodmanson on 23 Nov 2022
one possibility:
(tanh(-10))^2
ans = 1.0000 % correct
although
tanh(-(10)^2) % traveling parenthesis
ans = -1
  2 Comments
yogeshwari patel
yogeshwari patel on 23 Nov 2022
What is traveling parenthesis ? How should I correct it in my MATLAB code
David Goodmanson
David Goodmanson on 23 Nov 2022
Edited: David Goodmanson on 23 Nov 2022
Hi yp,
what I meant was, as I understand the problem the first expression (used in the loop) is correct, and the second expression is incorrect. The second expression is a totally different function because the parenthesis is in a different place. I am speculating about what went wrong, and the second expression might not be what you used, but it does have the "correct incorrect answer" of -1.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!