필터 지우기
필터 지우기

Differentiating gives wrong result

조회 수: 1 (최근 30일)
Richárd Tóth
Richárd Tóth 2019년 8월 12일
답변: Alex Mcaulley 2019년 8월 12일
Hello
One of my functions returns this symbolic expression
(10000*(x2*cos((9*pi)/40) + x1*sin((9*pi)/40))^2)/9801 + (10000*(x1*cos((9*pi)/40) - x2*sin((9*pi)/40))^2)/9801 - 1
If I use the diff function(by x1) on it, it gives wrong result(ignores the variables or I don't know):
(20000*cos((9*pi)/40)*(x1*cos((9*pi)/40) - x2*sin((9*pi)/40)))/9801 + (20000*sin((9*pi)/40)*(x2*cos((9*pi)/40) + x1*sin((9*pi)/40)))/9801
Instead of:
(50706024009129175657667733171308125*x1)/24848487065673752728138376231780352
If I simply run this code, it works, so something is probably wrong with my returned expression from the function
syms x1 x2
f=(10000*(x2*cos((9*pi)/40) + x1*sin((9*pi)/40))^2)/9801 + (10000*(x1*cos((9*pi)/40) - x2*sin((9*pi)/40))^2)/9801 - 1;
diff(f,x1)
  댓글 수: 2
Alex Mcaulley
Alex Mcaulley 2019년 8월 12일
Can you show the complete code that generates the symbolic expression? As you say, it works just executing your code.
Richárd Tóth
Richárd Tóth 2019년 8월 12일
Yes, it's not the same code, that code is much longer but gives the same result.
The function:
function expr = testfunc()
syms x1 x2 d1 d2 d3 center1 center2
expr = (cos(d3)*(center1 - x1) - sin(d3)*(center2 - x2))^2/d1^2 + (cos(d3)*(center2 - x2) + sin(d3)*(center1 - x1))^2/d2^2 - 1;
and the code:
syms x1 x2 lambda d1 d2 d3 center1 center2
d1=0.9900;d2=0.9900;d3=9*pi/40;
center1=0;center2=0;
ell = testfunc;
ell = subs(ell);
g = ell;
diff(g,x1)
Running this displays the wrong value I mentioned in previous post.
If you try this, it works:
syms x1 x2
f = (10000*(x2*cos((9*pi)/40) + x1*sin((9*pi)/40))^2)/9801 + (10000*(x1*cos((9*pi)/40) - x2*sin((9*pi)/40))^2)/9801 - 1;
diff(f,x1)

댓글을 달려면 로그인하십시오.

채택된 답변

Alex Mcaulley
Alex Mcaulley 2019년 8월 12일
I don't know why the result is presented in different way depending on the case, but the result is allways the same:
%Case 1
syms x1 x2
f = (10000*(x2*cos((9*pi)/40) + x1*sin((9*pi)/40))^2)/9801 + (10000*(x1*cos((9*pi)/40) - x2*sin((9*pi)/40))^2)/9801 - 1;
sol = diff(f,x1)
sol =
(50706024009129175657667733171308125*x1)/24848487065673752728138376231780352
double(subs(sol,x1,1))
ans =
2.0406
%Case 2
syms x1 x2 lambda d1 d2 d3 center1 center2
d1=0.9900;d2=0.9900;d3=9*pi/40;
center1=0;center2=0;
ell = testfunc;
ell = subs(ell);
g = ell;
sol = diff(g,x1)
sol =
(20000*cos((9*pi)/40)*(x1*cos((9*pi)/40) - x2*sin((9*pi)/40)))/9801 + (20000*sin((9*pi)/40)*(x2*cos((9*pi)/40) + x1*sin((9*pi)/40)))/9801
sol = simplify(sol)
sol =
(20000*x1)/9801
double(subs(sol,x1,1))
ans =
2.0406

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by